51nod 1005 1027 1029 高精度
Java大数用法参考:https://www.cnblogs.com/jin-nuo/p/5313205.html
1005 大数加法:
import java.util.*; import java.io.*; import java.math.*; public class Main{ public static void main(String[] args) { Scanner cin=new Scanner(System.in); BigInteger a=cin.nextBigInteger(); BigInteger b=cin.nextBigInteger(); System.out.println(a.add(b)); } }
1027 大数乘法:
import java.util.*; import java.io.*; import java.math.*; public class Main{ public static void main(String[] args) { Scanner cin=new Scanner(System.in); BigInteger a=cin.nextBigInteger(); BigInteger b=cin.nextBigInteger(); System.out.println(a.multiply(b)); } }
1029 大数除法 取模:
import java.util.*; import java.io.*; import java.math.*; public class Main{ public static void main(String[] args) { Scanner cin=new Scanner(System.in); BigInteger a=cin.nextBigInteger(); BigInteger b=cin.nextBigInteger(); System.out.println(a.divide(b)); System.out.println(a.remainder(b)); } }
牛客小白月赛12 B 求A^B mod P
题目链接:https://ac.nowcoder.com/acm/contest/392/B
import java.util.*; import java.io.*; import java.math.*; public class Main{ public static void main(String[] args){ Scanner cin=new Scanner(System.in); int T=cin.nextInt(); while(T>0){ T--; BigInteger a=cin.nextBigInteger(); BigInteger b=cin.nextBigInteger(); BigInteger p=cin.nextBigInteger(); BigInteger res=BigInteger.valueOf(1); while(b.compareTo(BigInteger.valueOf(0))!=0){ if(b.mod(BigInteger.valueOf(2)).compareTo(BigInteger.valueOf(1))==0) res=res.multiply(a).mod(p); a=a.multiply(a).mod(p); b=b.divide(BigInteger.valueOf(2)); } System.out.println(res); } } }