洛谷P1045 麦森数 高精度快速幂 计算位数
第一问: 我们知道10的n次位数是n+1,2的p次等于10的log(10)2p ,所以结果就是log(10)2p+1 ,直接用库函数即可
第二问 考虑用大数快速幂对最后500位计算答案即可
此题学到的知识点 java大数取模,pow,快速幂模板
import java.util.*; import java.math.*; public class Main { static Scanner in=new Scanner(System.in); final static BigInteger MOD=BigInteger.TEN.pow(500),Mod=new BigInteger("2"); static BigInteger qucikPower(int b) { BigInteger ans=BigInteger.ONE,base=Mod; while(b!=0) { if(b%2!=0) ans=ans.multiply(base); base=base.multiply(base); ans=ans.mod(MOD); base=base.mod(MOD); b>>=1; } ans=ans.mod(MOD); return ans; } public static void main(String[] args) { int p; p=in.nextInt(); BigInteger s=qucikPower(p).subtract(BigInteger.ONE); System.out.println((int)(p*Math.log10(2)+1)); for(int i=499;i>=0;i--) { if(i%50==0) { if(s.divide(BigInteger.TEN.pow(i)).mod(BigInteger.TEN)==BigInteger.ZERO) System.out.println("0"); else System.out.println(s.divide(BigInteger.TEN.pow(i)).mod(BigInteger.TEN)); } else { if(s.divide(BigInteger.TEN.pow(i)).mod(BigInteger.TEN)==BigInteger.ZERO) System.out.print("0"); else System.out.print(s.divide(BigInteger.TEN.pow(i)).mod(BigInteger.TEN)); } } in.close(); } }