Factorial Problem in Base K(zoj3621)
来源:互联网 发布:ubuntu源arm 编辑:程序博客网 时间:2024/06/12 21:25
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 212 7
Sample Output
31
题意:给你s和k表示k进制的s;现在求s的10进制的阶乘换成k进制后末尾有几个0;
思路:找k进制的质因子个数kv[i],以及s的阶乘中质因子的个数pn,那么最后换成k进制后有多少个0,就是pn/kv[i]中最小的。
刚开始找出来pn的时候,测试数据都过了,可是WA了,,,,后来我把ans开很大很大,结果就过了。郁闷
#include<cstdio>#include<cstring>#include<cmath>#include<iostream>#define N 65using namespace std;char str[N];int k;long long s;int prm[20]= {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61},kv[20];/*int judge(int x,int tp){ int y=x,a=0; while(y<=tp) { a+=tp/y;// printf("y:%d\ttemp:%d ans:%d\n",y,temp,ans); if(tp/y<x) break; y*=x; } return a;}*/void solve(long long x){ int i; long long res,ans=0x7ffffffffffffff;//开小了,,居然WA long long pn; for(i=0; i<18; i++) { res=x; res=res/prm[i]; pn=res; while(res) { res=res/prm[i]; pn+=res; } if(kv[i]&&pn/kv[i]<ans) ans=pn/kv[i]; } printf("%lld\n",ans);}int main(){ int i,j; while(scanf("%s%d",str,&k)!=EOF) { int len=strlen(str); s=0; for(i=0; i<len; i++) { int num; if(str[i]<='z'&&str[i]>='a') num=str[i]-'a'+36; else if(str[i]<='Z'&&str[i]>= 'A') num=str[i]-'A'+10; else num=str[i]-'0'; s=num+s*k; } memset(kv,0,sizeof(kv)); int n=k,x; for(i=0; i<18; i++) { while(n%prm[i]==0) { kv[i]++; n/=prm[i]; } } solve(s); } return 0;}
记得以前也做过类似的题。就是求阶乘中含多少质因子的题目。
0 0
- Factorial Problem in Base K(zoj3621)
- ZOJ3621 Factorial Problem in Base K
- ZOJ 3621 Factorial Problem in Base K(K进制)
- zoj_3621 Factorial Problem in Base K
- zoj 4745 Factorial Problem in Base K
- ZOJ 3621 Factorial Problem in Base K
- ZOJ 3621 Factorial Problem in Base K 分解质因数
- ZOJ - 3621——Factorial Problem in Base K
- Problem 20 Factorial digit sum (阶乘数和)
- Problem 20:Factorial digit sum
- ZOJ Problem Set - 2022||Factorial
- Thinking in Exception(Base on Alfresco)
- Problem K
- Problem K
- Problem K
- Problem K
- Problem K
- Problem K
- 浙大月赛ZOJ Monthly, August 2014
- xUtils框架的介绍
- Incircle and Circumcircle(二分+几何)浙大月赛zoj3806(详解版)图
- Beans Game(博弈 | | DP)zoj 3057
- 吉哥系列故事——临时工计划(dp)
- Factorial Problem in Base K(zoj3621)
- Inviting Friends(hdu3244 && zoj3187)完全背包+二分
- First Date (hnoj12952)日期计算
- 11078 - Open Credit System
- Median(vector+二分)
- 对进程地址空间的一点认识
- Cuckoo for Hashing(hash)
- Alice and Bob(博弈)
- 最小公倍数(BNUOJ30195)