hdu 5047 Sawtooth 組合數學 高精度

題目鏈接:點擊打開鏈接

題意:略

思路:被卡的心力交瘁。。不願多說,主要是記錄一下java的快速讀寫,防止下次被這樣的無良出題人卡。

cpp版:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct Bignb{
    long long high,low;
    void print(int& cas)
    {
        if(high<=0)
        {
            printf("Case #%d: %I64d\n",++cas,low);
        }
        else {
            printf("Case #%d: %I64d%012I64d\n",++cas,high,low);
        }
    }
};
const long long mod1=1e6;
const long long mod2=1e12;
int main()
{
    int T,cas=0;
	//  freopen("data.in","r",stdin);
    scanf("%d",&T);
    while (T--)
    {
        long long tp;
		scanf("%I64d",&tp);
        if(tp<1e9){
            printf("Case #%d: %I64d\n",++cas,tp*(8*tp-7)+1);
        }
        else {
            long long tp1=tp%(mod1),tp2=tp/(mod1),temp=16*tp1*tp2-7*tp2;
            Bignb ans;
            ans.high=8*tp2*tp2;
            ans.low=8*tp1*tp1-7*tp1+1;
            ans.low+=(mod1)*(temp%mod1);
            ans.high+=(temp/mod1);
            if(ans.low<0){
                ans.low+=mod2;
                ans.high--;
            }
            else {
                ans.high+=ans.low/mod2;
                ans.low=ans.low%mod2;
            }
            ans.print(cas);
        }
    }
    return 0;
}


java版:

import java.util.*;
import java.io.*;
import java.math.*;
public class test {

	
	

	public static void main(String[] args) throws IOException{
    	BigInteger zero=BigInteger.valueOf(0);
    	BigInteger data1,data2;
    	
 
    	int T;
		 
    	Scanner cin = new Scanner(new BufferedInputStream(System.in));
        PrintWriter cout = new PrintWriter(new BufferedOutputStream(System.out));
        T=cin.nextInt();
        for(int cas=1;cas<=T;cas++){
        	data1=zero;
        	data2=zero;
        	data1=cin.nextBigInteger();
       		data2=data1.multiply(data1);
       		data2=data2.multiply(BigInteger.valueOf(8)).subtract(data1.multiply(BigInteger.valueOf(7))).add(BigInteger.ONE);
       		cout.printf("Case #%d: ",cas);
       		cout.println(data2);
       	//	System.out.println("Case #"+cas+": "+data2);
        }
        cin.close();// cout.flush();
        cout.close();

      }
        
       
}
	


發佈了76 篇原創文章 · 獲贊 7 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章