UVa-10139 Factovisors -(阶乘的整除)

计算数 m 和数 2 ~n 的 GCD,若 GCD > 1,则 m /= GCD,若最后 m 不为 1 则表明 m 不能整除 n!,

代码:

#include<iostream>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<queue>
#include<cstring>
#include<map>
using namespace std;
#define M 1000005
typedef long long ll;

int gcd(int a,int b)
{
    return b==0?a:gcd(b,a%b);
}
bool isprime(int x)
{
    double tx=sqrt(1.0*x);
    int it=2;
    while(it<=tx&&x%it!=0)
        it++;
    if(it>tx)
        return true;
    else
        return false;
}
/*bool isprime(int number)
{
    if (number == 2)
        return true;

    if (number % 2 == 0)
        return false;

    for (int c = 3; c <= ceil(sqrt(1.0*number)); c += 2)
        if (number % c == 0)
            return false;

    return true;
}*/

int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        if(m==0)
        {
            printf("%d does not divide %d!\n",m,n);
            continue;
        }
        if(m==1)
        {
            printf("%d divides %d!\n",m,n);
            continue;
        }
        if(isprime(m)&&m>n)
        {
            printf("%d does not divide %d!\n",m,n);
            continue;
        }
        if(m<=n)
        {
            printf("%d divides %d!\n",m,n);
            continue;
        }

        bool flag=false;
        int tm=m;
        int gc;
        for(int i=n;i>=2;i--)
        {
            gc=gcd(tm,i);
            if(gc>1)
            {
                tm=tm/gc;
                if(tm<i)
                {
                    flag=true;
                    break;
                }
                if(isprime(tm))
                {
                    break;
                }
            }
        }
        if(flag)
            printf("%d divides %d!\n",m,n);
        else
            printf("%d does not divide %d!\n",m,n);
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值