您的位置:首页 > 大数据 > 人工智能

Again Prime? No Time.(UVA 10780)

2016-07-18 23:49 447 查看
题目链接

预处理答案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 1000000007
using 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  uva 分解质因数