hdu 4910 Problem about GCD 找规律+Miller_Rabin算法+线性筛

题意:

输入一个正整数n(n<=1e18),输出所有的i相乘并对n取余所得的值。(gcd(i,n)==1,1<=i<=n)

题解:

比赛的时候花了一个小时找规律,楞是没找到,无语死了。。比赛后看到规律,想直接用Pollard_rho算法和Miller_Rabin算法找出所有因子求解,无限TLE,别人都能200MS过,为毛我写的算法就这么挫呢委屈。最后只要退一步用Miller_Rabin算法+线性筛了。

规律:将n分解分所有质因子相乘,质因子2个数为a,其余质因子个数为b,ans=(a==0?a:a-1)+b;当ans<2时结果为n-1,否则结果为1.

Miller_Rabin算法:判断n是否为素数,

Pollard rhos算法:求n的所有因子。

详细的可以看:算法集锦(特殊模板集)

我们将对2取余,得到a和剩余的数k,然后判断k是否为1,为1的时候b=0。然后判断是否是素数,素数的时候b=1;当k不是素数的时候只有k=c^m(c为素数,m>=2)时b=1,其余情况b>=2,则结果必定为1。怎么判断k=c^m?m=2的时候直接开方判断,m>2则枚举1e6以内的所有素数判断即可。




代码:

#include <cstdio>
#include <cstring>
#include <cmath>
#include <ctime>
#include <iostream>
#include <algorithm>
using namespace std;
typedef __int64 LL;
const LL NUM=20;//运算次数,Miller_Rabin算法为概率运算,误判率为2^(-NUM);
const int maxn=1e6+10;
int prime[maxn],c[maxn],tot;
LL t,f[1000];
LL mul_mod(LL a,LL b,LL n)//求a*b%n,由于a和b太大,需要用进位乘法
{
    a=a%n;
    b=b%n;
    LL s=0;
    while(b)
    {
        if(b&1)
            s=(s+a)%n;
        a=(a<<1)%n;
        b=b>>1;
    }
    return s;
}
LL pow_mod(LL a,LL b,LL n)//求a^b%n
{
    a=a%n;
    LL s=1;
    while(b)
    {
        if(b&1)
            s=mul_mod(s,a,n);
        a=mul_mod(a,a,n);
        b=b>>1;
    }
    return s;
}
bool check(LL a,LL n,LL r,LL s)
{
    LL ans,p,i;
    ans=pow_mod(a,r,n);
    p=ans;
    for(i=1;i<=s;i++)
    {
        ans=mul_mod(ans,ans,n);
        if(ans==1&&p!=1&&p!=n-1)return true;
        p=ans;
    }
    if(ans!=1)return true;
    return false;
}
bool Miller_Rabin(LL n)//Miller_Rabin算法,判断n是否为素数
{
    if(n<2)return false;
    if(n==2)return true;
    if(!(n&1))return false;
    LL i,r,s,a;
    r=n-1;s=0;
    while(!(r&1)){r=r>>1;s++;}
    for(i=0;i<NUM;i++)
    {
        a=rand()%(n-1)+1;
        if(check(a,n,r,s))
            return false;
    }
    return true;
}
LL gcd(LL a,LL b)
{
    return b==0?a:gcd(b,a%b);
}
LL Pollard_rho(LL n,LL c)//Pollard_rho算法,找出n的因子
{
    LL i=1,j,k=2,x,y,d,p;
    x=rand()%n;
    y=x;
    while(true)
    {
        i++;
        x=(mul_mod(x,x,n)+c)%n;
        if(y==x)return n;
        if(y>x)p=y-x;
        else p=x-y;
        d=gcd(p,n);
        if(d!=1&&d!=n)return d;
        if(i==k)
        {
            y=x;
            k+=k;
        }
    }
}
void find(LL n)//找出n的所有因子
{
    if(Miller_Rabin(n))
    {
        f[t++]=n;//保存所有因子
        return;
    }
    LL p=n;
    while(p>=n)p=Pollard_rho(p,rand()%(n-1)+1);//由于p必定为合数,所以通过多次求解必定能求得答案
    find(p);
    find(n/p);
}
void init()//预处理,找出所有1e7以内的素数,以减少查找1e14范围数的因子的时间
{           //现行筛素数的方法,时间复杂度为O(n)
    memset(c,false,sizeof(c));
    int i,j;
    tot=0;
    for(i=2;i<=1e6;i++)
    {
        if(!c[i])prime[tot++]=i;
        for(j=0;j<tot;j++)
        {
            if(i*prime[j]>1e6)break;
            c[i*prime[j]]=true;
            if(i%prime[j]==0)break;
        }
    }
    //printf("%d\n",tot);
    //for(i=0;i<20;i++)
    //    printf("prime[%d]:%d\n",i,prime[i]);
}
bool judge(LL n)
{
    int i,j,k;
    for(i=0;i<tot;i++)
    {
        if(n%prime[i]==0)
        {
            while(n%prime[i]==0)n=n/prime[i];
            if(n==1)return true;
            return false;
        }
    }
    return false;
}
int main()
{
    //freopen("D:\\in.txt","r",stdin);
    //freopen("C:\\Documents and Settings\\Administrator\\桌面\\false.txt","w",stdout);
    srand(time(NULL));//随机数设定种子
    init();
    LL n;
    while(cin>>n)
    {
        if(n==-1)break;
        if(n==1){cout<<0<<endl;continue;}
        /*t=0;
        find(n);
        sort(f,f+t);
        LL i,j,k=0,ans=0;
        if(f[0]==2)k++;
        else ans++;
        for(i=1;i<t;i++)
        {
            if(f[i]==2)k++;
            if(f[i]!=f[i-1])ans++;
        }
        if(k>=1)
        ans+=k-1;
        //cout<<n<<":";
        if(ans<2)cout<<n-1<<endl;
        else cout<<1<<endl;*/
        LL i,j,k,ans=0;
        k=n;
        while(k%2==0){k=k/2;ans++;}
        if(ans>=1)ans--;
        if(k!=1)
        {
            if(Miller_Rabin(k))ans++;
            else
            {
                i=(LL)sqrt(k+0.5);
                if(i*i==k&&Miller_Rabin(i)||judge(k))ans++;
                else ans+=2;
            }
        }
        if(ans<2)cout<<n-1<<endl;
        else cout<<1<<endl;
    }
    return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值