写了快一个小时写不出来
错误代码
public class test {
static long NINE=1000000000L;
public static void main(String[] args) {
long length = 202320232023L;
long sum=0;
for (long i = 0; i <length ; i++) {
sum+=getFactorialEnd(i)%NINE;
}
System.out.println(sum);
/*for (int i = 1; i < 16; i++) {
System.out.println(i + ":" + getFactorial(i));
System.out.println(i + ":" + getFactorialEnd(i));
System.out.println();
}
*/
}
public static int getFactorialEnd(long i) {
if (i == 1) return 1;
i = i ;
return (int) ((i * (getFactorialEnd(i - 1) % NINE)) % NINE);
}
public static long getFactorial(int i) {
if (i == 1) {
return 1;
} else {
return (long) (i * getFactorial(i - 1));
}
}
}
最终题解
public class Main {
public static void main(String[] args) {
long n = 202320232023L;
long m = 1000000000L;
long b = 1;
long S = 0;
for (long i = 1; i <= n; i++) {
// 计算第i项的阶乘,并取余c
b = (b * i) % m;
// 对结果进行判断
if (b == 0) {
// 输出结果
System.out.println(S);
return;
} else {
S = (S % m + b % m) % m;
}
}
}
}