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
31
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);}}?
轉載于:https://www.cnblogs.com/qscqesze/p/4539970.html
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的zoj 3621 Factorial Problem in Base K 数论 s!后的0个数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 幼儿园手工帽子教案一等奖
- 下一篇: 有哪些好用的手机视频制作App?视频制作