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[];
//const int inf=0x7fffffff; //нчоч╢С
const int inf=0x3f3f3f3f;
inline ll read()
{
ll x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
inline void P(int x)
{
Num=;if(!x){putchar('');puts("");return;}
while(x>)CH[++Num]=x%,x/=;
while(Num)putchar(CH[Num--]+);
puts("");
}
//************************************************************************************** string s;
int n;
const int p[]={,,,,,,,,,,,,,,,,,};
int a[];
int main()
{
while(cin>>s>>n)
{
memset(a,,sizeof(a));
ll tmp=;
ll k=;
for(int i=s.size()-;i>=;i--)
{
if(s[i]<=''&&s[i]>='')
tmp+=(s[i]-'')*k;
else if(s[i]<='Z'&&s[i]>='A')
tmp+=(s[i]-'A'+)*k;
else
tmp+=(s[i]-'a'+)*k;
k*=n;
}
for(int i=;i<;i++)
{
while(n%p[i]==&&n>)
{
n/=p[i];
a[i]++;
}
}
ll ans=(1LL<<)-;
for(int i=;i<;i++)
{
ll now=tmp,tot=;
while(now>)
{
now/=p[i];
tot+=now;
}
if(a[i]>)
ans=min(ans,tot/a[i]);
}
printf("%lld\n",ans);
} }
上一篇:Unity Shader 获取模型空间坐标


下一篇:UNITY把3D模型显示在UI层级上的思路