lonlife oj 1029 - Bob and Alice are playing factors

DESCRIPTION
Bob and his girl friend Alice like factors very much.

One day, Bob find a beautiful path: 4, 2, 1, Becauese 2 is the factor of 4 and 1 is the factor of 2.

Alice is excited, says “why can’t we find a longest path A1,A2,A3,…,Ak,Ai+1
is the factor of Ai and Ai+1

#include
#include
#include
#include
#include
#include
using namespace std;
const int S=20;
typedef long long LL;
const long long  mod=1e9+7;

long long mult_mod(long long a,long long b,long long c)
{
    a%=c;
    b%=c;
    long long ret=0;
    while(b)
    {
        if(b&1){ret+=a;ret%=c;}
        a<<=1;
        if(a>=c)a%=c;
        b>>=1;
    }
    return ret;
}

long long pow_mod(long long x,long long n,long long mod)//x^n%c
{
    if(n==1)return x%mod;
    x%=mod;
    long long tmp=x;
    long long ret=1;
    while(n)
    {
        if(n&1) ret=mult_mod(ret,tmp,mod);
        tmp=mult_mod(tmp,tmp,mod);
        n>>=1;
    }
    return ret;
}

bool check(long long a,long long n,long long x,long long t)
{
    long long ret=pow_mod(a,x,n);
    long long last=ret;
    for(int i=1;i<=t;i++)
    {
        ret=mult_mod(ret,ret,n);
        if(ret==1&&last!=1&&last!=n-1) return true;//合数
        last=ret;
    }
    if(ret!=1) return true;
    return false;
}

bool Miller_Rabin(long long n)
{
    if(n<2)return false;
    if(n==2)return true;
    if((n&1)==0) return false;//偶数
    long long x=n-1;
    long long t=0;
    while((x&1)==0){x>>=1;t++;}
    for(int i=0;ilong long a=rand()%(n-1)+1;//rand()需要stdlib.h头文件
        if(check(a,n,x,t))
            return false;//合数
    }
    return true;
}


map<long long,long long> factor;
map<long long,long long>::iterator it1;
int tol;
long long gcd(long long a,long long b)
{
    if(a==0)return 1;//???????
    if(a<0) return gcd(-a,b);
    while(b)
    {
        long long t=a%b;
        a=b;
        b=t;
    }
    return a;
}

long long Pollard_rho(long long x,long long c)
{
    long long i=1,k=2;
    long long x0=rand()%x;
    long long y=x0;
    while(1)
    {
        i++;
        x0=(mult_mod(x0,x0,x)+c)%x;
        long long d=gcd(y-x0,x);
        if(d!=1&&d!=x) return d;
        if(y==x0) return x;
        if(i==k){y=x0;k+=k;}
    }
}

void findfac(long long n)
{
    if(Miller_Rabin(n))
    {
        factor[n]++;
        //factor[tol++]=n;
        return;
    }
    long long p=n;
    while(p>=n)p=Pollard_rho(p,rand()%(n-1)+1);
    findfac(p);
    findfac(n/p);
}
void exgcd(LL a, LL b, LL &x, LL &y)   //ax+by=1; x为a mod  b 的逆元,y为 b mod a的逆元    a,b互质
{
    if(b == 0)
    {
        x = 1;
        y = 0;
        return;
    }
    LL x1, y1;
    exgcd(b, a%b, x1, y1);
    x = y1;
    y = x1-(a/b)*y1;
}


int main()
{
    int  t;
    scanf("%d",&t);
    long long n;
    for(int i=1;i<=t;i++)
    {
        factor.clear();
        scanf("%lld",&n);
        printf("Case #%d: ",i);
        if(n==1){
            printf("1 1\n");
            continue;
        }
        if(Miller_Rabin(n)){
            printf("2 1\n");
            continue;
        }
        findfac(n);
        long long  sum=0;
        for(it1=factor.begin();it1!=factor.end();it1++){
            sum+=it1->second;
        }
        printf("%lld ",(sum+1)%mod);

        long long  ans=1,x,y;
        for(int i=2;i<=sum;i++) ans=ans*i%mod;

       for(it1=factor.begin();it1!=factor.end();it1++){
            long long  gg=it1->second,an=1;
            for(int i=2;i<=gg;i++) an=an*i%mod;
           exgcd(an,mod,x,y);
           ans=ans*x%mod;
       }
        printf("%lld\n",ans%mod);
    }
    return 0;
}

你可能感兴趣的:(数论)