解题报告:HDU_3988 Harry Potter and the Hide Story 大素数分解+勒让德

Harry Potter and the Hide Story

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2836    Accepted Submission(s): 720


Problem Description
iSea is tired of writing the story of Harry Potter, so, lucky you, solving the following problem is enough.

 

Input
The first line contains a single integer T, indicating the number of test cases.
Each test case contains two integers, N and K.

Technical Specification

1. 1 <= T <= 500
2. 1 <= K <= 1 000 000 000 000 00
3. 1 <= N <= 1 000 000 000 000 000 000
 

Output
For each test case, output the case number first, then the answer, if the answer is bigger than 9 223 372 036 854 775 807, output “inf” (without quote).
 

Sample Input
  
  
2 2 2 10 10
 

Sample Output
  
  
Case 1: 1 Case 2: 2
 

Author
iSea@WHU
 

Source
 

Recommend
lcy
 

Statistic |  Submit |  Discuss | Note

题意:给定N , K 求最大i 满足 (K^i) | (N!) 

思路:先用Pollard_rho算法得到K的唯一分解式,然后用勒让德定理求得答案

代码:

#include<bits/stdc++.h>

using namespace std;


long long fast_mult(long long x,long long y,long long mod){
    x %= mod;
    long long res =0;
    while(y){
        if(y&1){
            res = (res+x)%mod;
        }x<<=1;
        x%=mod;
        y>>=1;
    }return res;
}

long long qpow(long long x,long long y,const long long& mod){
    long long res = 1;
    while(y){
        if(y&1){
            res = fast_mult(res,x,mod);
        }x = fast_mult(x,x,mod);
        y>>=1;
    }return res;
}

inline bool check(long long a,long long n,long long x,long long t){
    long long res  = qpow(a,x,n);
    long long la = res ;
    for(int i=1;i<=t;i++){
        res = fast_mult(res,res,n);
        if(res==1 && la != 1&& la !=n-1){
            return true;
        }la = res;
    }if(res!=1){
        return true;
    }return false;
}

inline bool IsPrime(long long n){
    if(n==2){
        return true;
    }if((n&1)==0||n<2)return false;
    long long x = n-1;
    long long t = 0;
    while((x&1)==0){
        x>>=1;
        t++;
    }const int check_time = 20;
    for(int i=0;i<check_time;i++){
        long long a = rand()%(n-1)+1;
        if(check(a,n,x,t)){
            return false;
        }
    }return true;
}

long long e[100];
long long em[100];
int num[100];
int tol ;
long long gcd(long long a ,long long b ){
    return b?gcd(b,a%b):a;
}

long long Pollard_rho(long long n,long long c){
    long long i = 1, k =2;
    long long x = rand()%(n-1)+1;
    long long y = x;
    while(true){
        i++;
        x = (fast_mult(x,x,n)+c)%n;
        long long d = gcd((y-x+n)%n,n);
        if(d>1&&d<n)return d;
        if(y==x){
            return n;
        }if(i==k){
            k<<=1;
            y=x;
        }
    }
}

void oper(long long n){
    if(n==1)return ;
    if(IsPrime(n)){
        e[tol++]=n;
        return ;
    }long long p = n;
    while(p>=n)p = Pollard_rho(p,rand()%(n-1)+1);
    oper(p);
    oper(n/p);
}


inline void work(){
    sort(e,e+tol);
    int all=0;
    memset(num,0,sizeof(num));
    em[all]=e[0];
    num[all]++;
    for(int i=1;i<tol;i++){
        if(e[i]==em[all]){
            num[all]++;
        }else {
            em[++all]=e[i];
            num[all]++;
        }
    }tol = all+1;

}

inline long long get(int i,long long n){
    long long x = em[i],sum=0;
    while(x<=n){
        sum+=n/x;
        if(x*em[i]/em[i]!=x){
            break;
        }x*=em[i];
    }//printf("%I64d %I64d,sum-->%I64d\n",em[i],n,sum);
    return sum/num[i];
}

int main()
{
    srand(time(NULL));
    int T,t = 0;
    scanf("%d",&T);
    while(T--){
        long long a,b;
        scanf("%I64d%I64d",&b,&a);
        if(a==1LL){
            printf("Case %d: inf\n",++t);
            continue;
        }
        tol = 0;
        oper(a);
        work();

        long long ans = 9e18;
        for(int i=0;i<tol;i++){
            ans = min(ans,get(i,b));
        }printf("Case %d: %I64d\n",++t,ans);
    }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值