uva 10061 - How many zero's and how many digits ?

题目很清晰,想法也很简单,可是我做了两天。。。

其实考的就是因数分解跟斯特林公式。。

- -。。不过不能直接用斯特林公式,要逐个逐个算。。我就是因为这样,WA了两天。。。悲剧啊

//求数字位数,可用斯特林公式n! = sqrt(2 * PI * n) * (n / e) ^ n //求后缀0的个数: //先求出B前的所有素数,并记下a * b = B的所有b,算出n!中因子b的个数 #include <stdio.h> #include <string.h> #include <cmath> #include <map> #include <algorithm> using namespace std; const double PI = 3.141592654; int A, B; long long zeros; double digits; bool isPrime[801]; int prime[801]; int primeB[801]; int pn = 0; int pnB = 0; map<long long, long long> mapA; map<long long, long long> mapB; map<long long, long long>::iterator iterA; map<long long, long long>::iterator iterB; void init() { memset(isPrime, true, sizeof(isPrime)); isPrime[0] = isPrime[1] = false; for(int i = 2; i <= 800; i++) if(isPrime[i]) { prime[pn++] = i; for(int j = i * 2; j <= 800; j += i) isPrime[j] = false; } } long long cal() { long long ans = 2147483648; for(iterB = mapB.begin(); iterB != mapB.end(); iterB++) { iterA = mapA.find(iterB->first); if(iterA == mapA.end()) return 0; ans = min(ans, iterA->second / iterB->second); if(ans == 0) return 0; } return ans; } long long findigit() { double sum = 0, tmp; for(int i = 2; i <= A; ++i) { tmp = i; sum += log(tmp); } tmp = B; return ceil(sum / log(tmp) + 1e-10); } //#define TEST int main() { #ifdef TEST freopen("input.txt", "r", stdin); #endif init(); while(scanf("%d%d", &A, &B) == 2) { if(A == 1) { printf("0 1/n"); continue; } mapA.clear(); mapB.clear(); pnB = 0; int tmp = B; //B分解因数 for(int i = 0; i < pn; i++) { if(tmp == 1) break; if(tmp % prime[i] == 0) { primeB[pnB++] = prime[i]; while(tmp % prime[i] == 0) { mapB[prime[i]]++; tmp /= prime[i]; } } } //A分解因数 for(int i = 2; i <= A; i++) { tmp = i; for(int j = 0; j < pnB; j++) { if(tmp == 1) break; while(tmp % primeB[j] == 0) { mapA[primeB[j]]++; tmp /= primeB[j]; } } } zeros = cal(); //digits = (0.5 * log(2 * PI * A) + A * log((double)A) - A) / log((double)B); printf("%lld %lld/n", zeros, findigit()); } return 0; }  

你可能感兴趣的:(uva 10061 - How many zero's and how many digits ?)