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
原创粉丝点击