1.题目
题目就是最后画圈的一句
2.知识加油站
这种一看就是大数,用long是不行的,只能用处理大数的api来处理:BigInteger
package 常用API;
import java.math.BigInteger;
public class 大数类Big {
public static void main(String[] args) {
BigInteger a = new BigInteger("11111111111111111111");//构造方法赋值
BigInteger b = BigInteger.valueOf(1234567891011121314L);//超过int时要在数后面加L
//1.add加法
BigInteger c = a.add(b);
System.out.println("a+b="+c);
//2.subtract减法
c = a.subtract(b);
System.out.println("a-b="+c);
//3.multiply乘法
c = a.multiply(b);
System.out.println("a*b="+c);
//4.divide除法
c = a.divide(b);
System.out.println("a/b="+c);
//5.mod取余
c = a.mod(b);
System.out.println("a%b="+c);
//6.gcd最大公因数
c = a.gcd(b);
System.out.println("a和b最大公因数="+c);
//7.modPow先平方再取余
BigInteger mod = new BigInteger("100000000");
c = a.modPow(b, mod);
System.out.println("(a^b)%mod="+c);
//8.compareTo比较大小
int d = a.compareTo(b);
System.out.println("a.compareTo(b)="+d);
/**
* 进制转换
*/
//9. x -> 10
BigInteger e = new BigInteger("1111",2);
System.out.println("(111)2 -> ("+e+")10");
//10. 10 -> x
String f = e.toString(8);
System.out.println("("+e+")10 -> ("+f+")8");
}
}
3.题解
package 刷题记录;
import java.math.BigInteger;
public class 梅森素数_721 {
public static void main(String[] args) {
BigInteger b1 = new BigInteger("2");
BigInteger b2 = new BigInteger("1");
BigInteger b3 = b1.pow(11213).subtract(b2);// ==> 2^11213 - 1
BigInteger b4 = new BigInteger("10");
BigInteger b5 = b4.pow(100);// 10^100
BigInteger b6 = b3.mod(b5);//取余,得到最后100位的数据
System.out.println(b6);
}
}
4.总结
需要明确题目考察的方向,也就是问题的类型或算法类别。这可以帮助你了解应该使用哪种算法或技巧来解决问题。
5.期望
算法题是需要一定时间和精力来解决的,不要急于求成。在解题过程中,遇到困难或挫折时,要保持积极的心态,相信自己的能力,坚持下去。
关注我,共同刷题、分享技巧,一起成长!