我的第29篇博客
【输入格式】
输入的第一行包含一个整数 n。
【输出格式】
输出一行,包含一个整数,表示答案。
【样例输入】 2
【样例输出】 257
【样例输入】 987654
【样例输出】 43636805
思路:
八次方最后的数字很大,很容易就溢出。因此采用 Biginteger
public class 八次求和 {
public static void main(String[] args) {
BigInteger modBig = BigInteger.valueOf(123456789);
BigInteger sumBig = BigInteger.valueOf(0);
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for (int i = 1; i <= n; i++) {
BigInteger tempBig = BigInteger.valueOf(i);
sumBig = sumBig.add(tempBig.multiply(tempBig).multiply(tempBig).multiply(tempBig).multiply(tempBig).multiply(tempBig).multiply(tempBig).multiply(tempBig));
}
BigInteger sum = sumBig.mod(modBig);
System.out.println(sum);
}
}
BigInteger的用法 详见
:点击👉 BigInteger的用法