sgu111-112大數運算

111原題鏈接http://acm.sgu.ru/problem.php?contest=0&problem=111

112原題鏈接http://acm.sgu.ru/problem.php?contest=0&problem=112


這裏我超級偷懶,用了 JAVA的大數類~~~


sgu111,二分查找

import java.io.*;
import java.math.*;
import java.util.*;

public class Solution {
	public static void main(String[] args) {
		BigInteger l = new BigInteger("1");
		BigInteger r = (new BigInteger("10")).pow(500);
		BigInteger one = new BigInteger("1");
		Scanner cin = new Scanner(new BufferedInputStream(System.in));
		BigInteger x = cin.nextBigInteger();
		BigInteger mid,ans;
		ans = new BigInteger("1");
		while(true){
			mid = (l.add(r)).shiftRight(1);
			if( (mid.pow(2)).compareTo(x) > 0){
				r = mid.subtract(one);
			}else{
				ans = mid;
				l = mid.add(one);
			}
			if(l.compareTo(r) > 0)
				break;
		}
		System.out.println(ans);
	}
}


sgu112

import java.io.*;
import java.math.*;
import java.util.*;

public class Solution {
	public static void main(String[] args) {
		Scanner cin = new Scanner(new BufferedInputStream(System.in));
		int x = cin.nextInt();
		int y = cin.nextInt();
		BigInteger bigx = BigInteger.valueOf(x);
		BigInteger bigy = BigInteger.valueOf(y);
		BigInteger result = bigx.pow(y).subtract(bigy.pow(x));
		System.out.println(result);
	}
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章