题解分享
题解分享简介
阶乘求和(结果填空) - 题解
题目要求后9位数字,直接运算会超出long类型限制,故可用模运算 即为当运算数字或者某数阶乘大于九位数字时取模1000000000。保证每次运算都在九位数以内即可得出答案。
```
public class Main {
static long N = 1000000000;
static long f(long x){
if(x == 1)
return 1;
long ans = x * f(x - 1);
if(ans > N){
ans = ans % N;
}
return ans;
}
public static void main(String[] args) {
long sum = 0;
for (int i = 1; i <= 2023L ; i++) {
sum += f(i);
if(sum > N){
sum = sum % N;
}
}
System.out.println(sum);
}
}
```
查看全文
0
0
0
4
阶乘求和(结果填空) - 题解
import java.math.BigInteger;
public class Main {
```
public static void main(String[] args) {
BigInteger jc=new BigInteger("1");
BigInteger ans=new BigInteger("0");
BigInteger cs=new BigInteger("1");
while (cs.compareTo(new BigInteger("90"))==-1) {
jc=jc.multiply(cs);
ans=ans.add(jc);
cs=cs.add(new BigInteger("1"));
}
System.out.println(ans.mod(new BigInteger("1000000000")));
}
```
}
查看全文
0
0
0
1
阶乘求和(结果填空) - 题解
```
#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
#define N 1000000000
typedef long long ll;
ll f(int x)
{
ll s=1;
for(int i=1;i<=x;i++)s=s*i%N;
return s;
}
int main()
{
ll sum=0;
for(ll x=1;x<=40;x++)
sum+=(f(x)%N);
cout<<sum%N<<endl;
}
```
查看全文
0
0
-2
2



