输入正整数n, 计算S = 1!+2!+…+n!的末6位(不含前导0). 这里1<=n<=109.
输入样例:
例如输入:
20
输出样例:
输出:
820313
- 注意题目的模数
mod=1000000
,当n>=1000000
后都是加零 - 所以我们只要暴力前面的
[1, 1000000]
, 后面的都不会改变 - 注意开
long long
int n, m, Q, K;
ll a[MAXN] = { 0 }, mod = 1000000;
signed main() {
#ifdef debug
freopen("test.txt", "r", stdin);
clock_t stime = clock();
#endif
read(n);
n = min(n, 1000000);
a[1] = 1;
for(int i=2; i<=n; i++) {
a[i] = (a[i-1] * i ) % mod;
}
for(int i=2; i<=n; i++) {
a[i] += a[i-1];
a[i] %= mod;
}
printf("%lld\n", a[n]);
#ifdef debug
clock_t etime = clock();
printf("rum time: %lf 秒\n",(double) (etime-stime)/CLOCKS_PER_SEC);
#endif
return 0;
}