这个题目呢,就2条计算长度和计算最后500位。
一个数计算长度,可以用log10为底,求所求的对数然后去掉分数后加一就是长度了。
证明就不深究了。10^n就会长度为n+1
然后就是计算
2
p
−
1
2^p-1
2p−1的最后500位了。
数字特别大,所以我们可以用BigInteger
然后取500位我们可以用modpow最后减去1就可以了。
可惜是jdk8,很多api都不能用。
不然书写更加方便。
import java.io.*;
import java.math.BigInteger;
public class Main {
static final PrintWriter print = new PrintWriter(System.out);
static final StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
public static int nextInt() throws IOException {
st.nextToken();
return (int) st.nval;
}
//打印生成的
static String s = "100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000";
public static void main(String[] args) throws IOException {
//jdk9不能用
// BigInteger ans = BigInteger.TWO.pow(nextInt()).subtract(BigInteger.ONE);
// BigInteger ans = BigInteger.valueOf(2).modPow(nextInt(),“1”+“0”.repeat(500)).subtract(BigInteger.valueOf(1));
// StringBuilder sb = new StringBuilder(ans.toString());
int i = nextInt();
int v = (int)(Math.log10(2) * i)+1;
System.out.println(v);
BigInteger ans = BigInteger.valueOf(2)
.modPow(BigInteger.valueOf(i), new BigInteger(s))
.subtract(BigInteger.valueOf(1));
//jdk15
// print(new StringBuilder("%0500d".formatted(ans)));
print(new StringBuilder(String.format("%0500d", ans)));
}
public static void print(StringBuilder s) {
for (int i = 0; i < 10; i++) {
print.println(s.substring(i * 50, (i + 1) * 50));
}
print.flush();
}
}
python就写的更简单了
import math
a = int(input())
len = int(math.log10(2)*a)+1
print(len)
b=pow(2,a,100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000)
b = b-1
c ='%0500d' %b #格式化数字补0,转为字符串
for j in range(10):
print(c[j*50:(j+1)*50])