题目链接:hdoj2940-Hex Factorial
在此转到关于本书博客ACM大学生程序设计竞赛在线题库最新精选题解(赵端阳)部分解析
原书和网上大多数程序都是从0到200最高位不断加乘,然后判断前导零位置。实际上阶乘是递增记录的,可以每次记录原数组长度,这样不必要原程序常数级复杂度。
#include<iostream>
#include<cstdio>
using namespace std;
const int maxn = 200;
int res[maxn], num[maxn];
void solve() {
res[1] = 1;
int len = 1;//len记录数组开销
for (int i = 2; i <= 100; i++) {
int c = 0;
for (int j = 1; j <= len; j++) {
c = res[j] * i + c;
res[j] = c % 16;
c = c / 16;
}
while (c) {
//把未除尽的数分到len前
res[++len] = c % 16;
c = c / 16;
}
for (int j = 1; j <= len; j++) if (res[j] == 0)num[i]++;//0的个数
}
}
int main(){
solve();
int n;
while (scanf("%d", &n) && n>=0) {
printf("%d\n", num[n]);
}
return 0;
}