Uva--10061 (数学,推导)

2014-06-11 23:59:05

题意&思路:求n!在b进制下的后缀零个数以及总位数,解决方法在总结中写过。(本来想用Stirling,后来WA了,看来精度不够呢)

#include <cstdio>
#include <ctime>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <string>
#include <iostream>
#include <algorithm>
using namespace std;

#define getmid(l,r) ((l) + ((r) - (l)) / 2)
#define MP(a,b) make_pair(a,b)
#define PB(a) push_back(a)

typedef long long ll;
typedef pair<int,int> pii;
const double eps = 1e-8;
const int INF = (1 << 30) - 1;

int n,b;
double sum[2000010];

void Pre(){
    double LB = log(1.0 * b);
    for(int i = 1; i <= 2000000; ++i)
        sum[i] = sum[i - 1] + log(i);
}

int main(){
    Pre();
    while(scanf("%d%d",&n,&b) != EOF){
        int cnt1,ans = INF;
        int num = sum[n] / log(b) + 1;
        for(int i = 2; i <= b; ++i) if(b % i == 0){
            cnt1 = 0;
            while(b % i == 0) b /= i,cnt1++;
            int cur = 0;
            for(int j = i; j <= n; j *= i) cur += n / j;
            ans = min(ans,cur / cnt1);
        }
        printf("%d %d\n",ans,num);
    }
    return 0;
}

 

posted @ 2014-06-12 00:00  Naturain  阅读(146)  评论(0编辑  收藏  举报