Again Prime? No Time.(UVA 10780)
来源:互联网 发布:火车头淘宝采集规则 编辑:程序博客网 时间:2024/06/05 15:56
题目链接
预处理答案ans为+∞,定义函数ok(x,y)表示y!中含有的x的最高次幂的指数,定义函数go(y,x)表示y中含有的x的最高次幂的指数,将m分解质因数,对于每一个质因数i,当前答案now=ok(i,n)/go(m,i),假如now更小则更新ans。
求y!中x的最高次幂的方法为求出并加上1到y中能被x整除的数的个数即y/x,然后将每个数除以x,不能整除的数删去,剩下的数就变成了1到y/x,重复该操作直到y==0。
代码实现:
int ok(int x,int y){ int ans=0; while(y) { ans+=y/x; y/=x; } return ans;}
附上AC代码:
#include<iostream>#include<algorithm>#include<cstring>#include<cmath>#include<cstdio>#include<queue>#include<set>#include<vector>#include<map>#include<string>#include<cmath>#define pq priority_queue#define Pi acos(-1.0)#define MAXX 1000000007using namespace std;bool life[6666];int v[6666],l=0;int ok(int x,int y){ int ans=0; while(y) { ans+=y/x; y/=x; } return ans;}int go(int y,int x){ int ans=0; while(y%x==0) { ans++; y/=x; } return ans;}int main(){ for(int j=2;j<=5000;j++) { if(!life[j]) { v[l++]=j; for(int k=2;k*j<=5000;k++) life[k*j]=1; } } int t,n,m,i=0,now; int ans; cin>>t; while(i<t) { i++; ans=100000000; scanf("%d%d",&m,&n); cout<<"Case "<<i<<":"<<endl; for(int j=0;j<l;j++) { if(m%v[j]==0) { ans=min(ans,ok(v[j],n)/go(m,v[j])); } } if(ans) cout<<ans<<endl; else cout<<"Impossible to divide"<<endl; } return 0;}
Memory: 0 KB Time: 0 MS
Language: C++ 4.8.2 Result: Accepted
0 0
- Again Prime? No Time.(UVA 10780)
- uva 10780 Again Prime? No Time.
- UVa 10780-Again Prime? No Time.
- UVA 10780 - Again Prime No Time
- UVA - 10780 Again Prime? No Time.
- UVa:10780 Again Prime? No Time.
- 【UVa 10780】 Again Prime? No time.
- UVA 10780 Again Prime? No time
- UVa - 10780 - Again Prime? No Time.
- [UVa 10780]Again Prime?No time.
- UVa 10780 - Again Prime? No Time.
- UVA 10780 C - Again Prime? No Time.
- UVA 10780-Again Prime? No Time.
- uva 10780Again Prime? No Time.
- Uva 10780 Again Prime? No time. 解题报告(数学)
- uva 10780 - Again Prime? No Time.(数论)
- UVA 10780 - Again Prime? No Time.(数论)
- UVA 10780-Again Prime? No Time. (数学-质因子)
- SSH端口转发与内网穿透
- Why was SCTP Selected for WebRTC’s Data Channel?
- UVA 10780 C - Again Prime? No Time.
- Debug Extension MEX 开放了
- SpringBoot 1.1.9.RELEASE升级1.3.6.RELEASE所遇异常
- Again Prime? No Time.(UVA 10780)
- HDU 3982 (半平面交 多边形和圆面积交)
- UUID、GUID、CLSID
- 指针与数组(三)——数组认知
- 名字空间
- SystemVerilog中结合interface实现输出数据总线的功能(2)
- 回家_洛谷U3427_dfs&bfs
- Java installation 安装报错 1603
- 多波束测深软件Caris若干问题