SUM OF SUB RECTANGLE AREAS
题意:
给你一个范围,要你求所有的子矩形的面积。
思路:
找规律,可以发现对于
n=1
ans=(1)*(1)
n=2
ans= (2+1 + 1)*(2+1 +1)
n=3
ans = (3+2+1 + 2+1 + 1)*(3+2+1 + 2+1 +1)
那么根据上面,就可以知道一个结论,每个答案其实是一个以等差数列的前n项和
为通项的数列。
式子就是 ( n ) ( n + 1 ) ( n + 2 ) 6 \frac{(n)(n+1)(n+2)}{6} 6(n)(n+1)(n+2)
最后对答案平方就可以了
ps:
和平方的求和公式有点像
n(n+1)(2n+1)/6
AC
import java.io.*;
import java.util.*;
import java.math.*;
public class Main{
public static void main(String[] args){
int tt;
Scanner cin = new Scanner(System.in);
tt = cin.nextInt();//cin>>tt;
while(true){
tt--;
long n;
n = cin.nextLong();
BigInteger a = BigInteger.valueOf(n);
BigInteger res = BigInteger.valueOf(n);
res = res.multiply(a.add(BigInteger.valueOf(1)));
res = res.multiply(a.add(BigInteger.valueOf(2)));
res = res.divide(BigInteger.valueOf(6));
res = res.multiply(res);
System.out.println(res);
if(tt==0)break;
}
}
}