(数论)阶乘问题
https://www.luogu.org/problemnew/show/P1313
#include <bits/stdc++.h>
#define ll long long
using namespace std;
inline int re() {
char q = getchar();
int x = 0;
while (q < '0' || q > '9') q = getchar();
while ('0' <= q && q <= '9')
x = (x << 3) + (x << 1) + q - (3 << 4), q = getchar();
return x;
}
const int maxn = 1e4 + 10;
int main() {
ll n;
scanf("%lld", &n);
ll ans = 1;
for (register int i = 1; i <= n; i++) {
ll tmp = ans * i;
while(tmp) {
if(tmp % 10 != 0) {
ans = tmp % 10;
break;
}
tmp /= 10;
}
}
printf("%d\n", ans);
}
第一次以为每次相乘只会影响各位,结果提交29.
分析逻辑发现在x15的时候发生问题,程序是2x15=30,提出3
结果应该还有前一位的影响为8
#include <bits/stdc++.h>
#define ll long long
using namespace std;
inline int re() {
char q = getchar();
int x = 0;
while (q < '0' || q > '9') q = getchar();
while ('0' <= q && q <= '9')
x = (x << 3) + (x << 1) + q - (3 << 4), q = getchar();
return x;
}
const int maxn = 1e4 + 10;
int main() {
ll n;
scanf("%lld", &n);
unsigned ll ans = 1;
for (register int i = 2; i <= n; i++) {
unsigned ll tmp = ans * i;
while (tmp) {
if (tmp % 10 != 0) {
ans = tmp % 100000000;
break;
}
tmp /= 10;
}
}
printf("%llu\n", ans % 10);
}
将模数改到n的范围即可