java,70pts,#2 9 10MLE求调!

P1045 [NOIP2003 普及组] 麦森数

vacilating @ 2024-06-10 18:52:40


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;

public class Main{
    public static void main(String[] args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        int b1 = Integer.parseInt(bf.readLine());
        BigInteger b2 = new BigInteger("2");
        int len = (int) (Math.log10(2)*b1+1);
        String res = "";
        System.out.println(len);
        if(len>500){
        res = b2.pow(b1).subtract(new BigInteger("1")).toString().
                substring(len-500);
        for(int i = 0;i<10;i++){
            System.out.println(res.substring(50*i,50*i+50));
        }
        }else{
            res = b2.pow(b1).subtract(new BigInteger("1")).toString();
            int remain = 500 - len;
            for(int i = 0,j=0;i<500;i++){
                if(i%50==0&&i!=0) System.out.println();
                if(i<remain) System.out.print(0);
                else System.out.print(res.charAt(j++));
            }
        }
    }
}

by Peter_Li @ 2024-07-05 13:09:38


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.math.BigInteger;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
        int b1 = Integer.parseInt(bf.readLine());
        int len = (int) (Math.log10(2) * b1 + 1);
        System.out.println(len);
        // 计算2^P - 1的最后500位数字
        BigInteger b2 = BigInteger.valueOf(2);
        BigInteger mod = new BigInteger("10").pow(500);
        BigInteger b3 = b2.modPow(BigInteger.valueOf(b1), mod).subtract(BigInteger.ONE);
        String res = b3.toString();
        // 补零
        int length = res.length();
        if (length < 500) {
            int remain = 500 - length;
            String zero = "0".repeat(remain);
            res = zero + res;
        }
        // 输出每50位一行
        for (int i = 0; i < 10; i++) {
            System.out.println(res.substring(50 * i, 50 * i + 50));
        }
    }
}

|