zoj 3621 Factorial Problem in Base K 数论 s!后的0个数

Factorial Problem in Base K

Time Limit: 20 Sec  Memory Limit: 256 MB

题目连接

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3621

Description

How many zeros are there in the end of s! if both s and s! are written in base k which is not necessarily to be 10? For general base, the digit order is 0-9,A-Z,a-z(increasingly), for example F4 in base 46 is actually 694 in base 10,and f4 in base 46 is 1890 in base 10.

Input

There are multiple cases(less than 10000). Each case is a line containing two integers s and k(0 ≤ s < 2^63, 2 ≤ k ≤ 62).

Output

For each case, output a single line containing exactly one integer in base 10 indicating the number of zeros in the end of s!.

 

Sample Input

101 2
12 7
 

Sample Output

3
1

HINT

题意

给你个在k进制下的数S,然后求S!在K进制下,有多少个末尾0
 

题解:

首先在10进制下,我们是怎么做的?我们先对10进行了质因数分解,分解成了2和5,然后我们就统计s!中,2和5各有多少个,然后取最少的就好了

就这样,我们先对k进行质因数分解,然后我们取最少个数就好了

代码:

 

//qscqesze
#include <cstdio>
#include <cmath>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
#include <fstream>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
//freopen("D.in","r",stdin);
//freopen("D.out","w",stdout);
#define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
#define test freopen("test.txt","r",stdin)  
#define maxn 200001
#define mod 10007
#define eps 1e-9
int Num;
char CH[20];
//const int inf=0x7fffffff;   //нчоч╢С
const int inf=0x3f3f3f3f;
inline ll read()
{
    ll x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
inline void P(int x)
{
    Num=0;if(!x){putchar('0');puts("");return;}
    while(x>0)CH[++Num]=x%10,x/=10;
    while(Num)putchar(CH[Num--]+48);
    puts("");
}
//**************************************************************************************

string s;
int n;
const int p[18]={2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61};
int a[30];
int main()
{
    while(cin>>s>>n)
    {
        memset(a,0,sizeof(a));
        ll tmp=0;
        ll k=1;
        for(int i=s.size()-1;i>=0;i--)
        {
            if(s[i]<='9'&&s[i]>='0')
                tmp+=(s[i]-'0')*k;
            else if(s[i]<='Z'&&s[i]>='A')
                tmp+=(s[i]-'A'+10)*k;
            else 
                tmp+=(s[i]-'a'+36)*k;
            k*=n;
        }
        for(int i=0;i<18;i++)
        {
            while(n%p[i]==0&&n>0)
            {
                n/=p[i];
                a[i]++;
            }
        }
        ll ans=(1LL<<63)-1;
        for(int i=0;i<18;i++)
        {
            ll now=tmp,tot=0;
            while(now>0)
            {
                now/=p[i];
                tot+=now;
            }
            if(a[i]>0)
                ans=min(ans,tot/a[i]);
        }
        printf("%lld\n",ans);
    }
    
}

 

你可能感兴趣的:(RIA)