日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

2020牛客国庆集训派对day2 F题 Java大数处理

發布時間:2023/12/4 java 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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大数处理的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。