Trailing Zeros of Factorials |
Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB |
Total submit users: 30, Accepted users: 16 |
Problem 13294 : No special judgement |
Problem description |
If n is a positive integer, then n factorial, written as n!, is the product of all the integers from n down to 1. For example, |
Input |
There are several test cases, each test case in one line contains 2 positive integers seprated by a space:n and b.(n≤109 , 2≤b≤104.) |
Output |
For each the case, output an integer indicate the number of trailing zeros of the factorial n! in base b. |
Sample Input |
5 10 5 2 16 10 |
Sample Output |
1 3 3 |
Judge Tips |
Hint: |
Problem Source |
HNU Contest |
题意就是问你n的阶乘n!在进制b下末尾零的个数。思路就是将b进行质因子分解,记录因子及其个数,那么肯定会有一个因子制约零的个数,我们取min即可。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <algorithm>
#include <queue>
#include <stack>
#include <climits>
#include <map>
#include <bitset>
using namespace std;
vector < pair <int,int> > v;
void factor(int n)
{
int now = n,cnt;
for (int i = 2; i <= n / i; i++)
{
if (now % i == 0)
{
cnt=0;
while (now % i == 0)
{
now /= i;
cnt++;
}
v.push_back(make_pair(i,cnt));
}
}
if (now != 1)
{
v.push_back(make_pair(now,1));
}
}
int main()
{
int n,b;
while(scanf("%d%d",&n,&b)==2)
{
v.clear();
factor(b);int minn=INT_MAX;
int len=v.size(),x,ans;
for(int i=0;i<len;++i)
{
x=n;ans=0;
while(x)
{
x/=v[i].first;
ans+=x;
}
ans/=v[i].second;
minn=min(ans,minn);
}
cout<<minn<<endl;
}
}