Again Prime? No Time.(UVA 10780)

题目链接
预处理答案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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值