数据量太大,直接用BigInteger运算,这个题目就是算区间内所有数的平方和,直接套公式
1平方+2平方+...+加到n平方=n(n+1)(2n+1)/6
import java.util.*; import java.math.BigInteger; public class Main { public static void main(String[] args) { // TODO Auto-generated method stub //平方和公式,1*1+2*2+n*n=n(n+1)(2n+1)/6 Scanner sc=new Scanner(System.in); int q=sc.nextInt(); String x="1000000007"; BigInteger two=new BigInteger("2"); BigInteger one=new BigInteger("1"); BigInteger six=new BigInteger("6"); BigInteger three=new BigInteger("3"); BigInteger mod=new BigInteger(x); for(int i=0;i<q;i++) { String l=sc.next(); String r=sc.next(); BigInteger ll=new BigInteger(l); ll=ll.subtract(one); BigInteger rr=new BigInteger(r); BigInteger len=new BigInteger(rr.subtract(ll).toString()); ll=ll.multiply((ll.add(one))).multiply(ll.multiply(two).add(one)).divide(six); //System.out.println(ll.toString()); rr=rr.multiply((rr.add(one))).multiply(rr.multiply(two).add(one)).divide(six); //System.out.println(rr.toString()); rr=rr.subtract(ll).multiply(three).add(len); System.out.println(rr.mod(mod).toString()); } } }