2020牛客国庆集训派对day2 F题 Java大数处理
題目:
鏈接:https://ac.nowcoder.com/acm/contest/16913/F
來源:牛客網
The following code snippet calculates the sum of the areas of all the sub rectangular grid in a rectangular grid from (0,0) to (N,N)\ .(N,N) . Find an efficient way to compute that.
sum = 0
for r1 = 0 to N-1
for c1 = 0 to N-1
for r2 = r1+1 to N
for c2 = c2+1 to N
sum = sum + (r2-r1)*(c2-c1)
print(sum)
輸入描述:
Input starts with T the number of test cases. Each test case consists of a single integer N.
輸出描述:
For each test output the sum (as computed above). Please note that even though W and H will fit into 64 bit integer, the sum may not be.
示例1
輸入
5
1
2
3
4
1000
輸出
1
16
100
400
27944805889000000
分析:
- 比賽最后不到一個小時,把規律推出來了,但由于沒有整理大數板子,就直接c模擬手敲大數加乘除,結果很慘烈,結束后調了一個小時,還是有bug,崩了。。。。down了一會直接用Java調用,就出來了,emmmm,就離譜,抱緊自己
AC代碼:
import java.math.BigInteger; import java.util.Scanner;public class Main {public static void main(String[] args) {Scanner scanner = new Scanner(System.in);int t=scanner.nextInt();while(0!=t--) {long n;n = scanner.nextLong();BigInteger a = BigInteger.valueOf(n);//valueOf(long val) 返回一個BigInteger,其值等于指定的 long 。 BigInteger b= BigInteger.valueOf(n+1);BigInteger c = BigInteger.valueOf(n+2);BigInteger e = new BigInteger("6");BigInteger d=a.multiply(b);//multiply(BigInteger val) 返回值為 (this * val) 。 d=d.multiply(c);d=d.divide(e);//divide(BigInteger val) 返回值為 (this / val) 。 System.out.println(d.multiply(d));}} }總結
以上是生活随笔為你收集整理的2020牛客国庆集训派对day2 F题 Java大数处理的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 怎么减肥增肌
- 下一篇: Java Number Math 类