HDU 3641 Treasure Hunting(阶乘素因子分解+二分)

Description

Zstu_yhr is a very curious person who fell in love with math when he was in elementary school phase. When he entered the middle school, he learned Multiplication and Power Multiplication. yhr is so ambitious that he not only dreams to be a mathematician but also dreams to be richer than Bill Gates.
One day, he is suddenly encountered with a crazy thought that is to hunt treasure to make one of his dreams a reality. Since yhr is such a strong-willed person that he will never give up as long as his goal has not been achieved. After going through 9*9 challenges, as a reward of god for that hard, he finally discovers an antique hole which is very likely to have a good number of treasures in it. However, as every novel writes, he can never get the treasures so easily. He has to open a coded door at first. He finds that there are 2*N numbers on the door. He speculates that they must be able to generate the password. Disappointedly, there isn’t any clue left for him. He has no better way but to YY. Firstly, he divides these 2*N number into N piles equally. The first pile is composed of a1,b1 and the second pile is composed of a2,b2...certainly, the i-th pile is composed of ai,bi…After completing this task, he calculates a1^b1*a2^b2*a3^b3…*an^bn and gets its result M. He takes M as the password to open the door. What’s a pity, he fails. Then he starts to YY again. Maybe the right password is the minimum number x which satisfies the equation x!%M=0. So he wants to have a try. But he doesn’t know how to get the number so that he has to turn to you for help. Can you help him?
 

Input

In the first line is an integer T (1<=T<=50) indicating the number of test cases.
Each test case begins with an integer n (1<=n<=100), then followed n lines. Each line contains two numbers ai and bi (1 <= ai <= 100, 1<=bi<=10000000000000)
 

Output

For each test case output the result x in one line.

Sample Input

1
2
3 2
4 1

Sample Output

6


        
  

Hint

 

n! is the factorial of number n: 0!=1 n!=n*(n-1)! (n>=1) a^0=1 (a>=1) a^i=a*(a^i-1) (i>=1)  

题意:M=a1^b1*a2^b2*...*an^bn,求使得N!%M=0的N的最小值。

思路:本题用到了求素数,素因子分解,二分。

可以用埃氏筛法求素数,可以用短除法素因子分解,由于N!包含因子p的个数为[n/p]+[n/(p*p)]+...(勒让德定理)

质因子分解:http://www.cnblogs.com/youxin/p/3232049.html

勒让德定理:http://www.doc88.com/p-9909966973219.html 涉及到勒让德定理的一个题目:http://acm.hust.edu.cn/vjudge/problem/26852

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
ll prime[25],vis[100];
ll findx(ll a,ll b)///x!的质因子有a,并且个数大于等于b,二分
{
    ll left=0,right=((ll)1<<60),mid,cnt;
    while(left<=right)
    {
        cnt=0;
        mid=left+(right-left)/2;
        ll t=mid;
        while(t)
        {
            cnt+=t/a;
            t/=a;
        }
        if(cnt<b) left=mid+1;
        else if(cnt>b) right=mid-1;
        else break;
    }
    return mid/a*a;
}
void Getprime()
{
    ll top=0;
    memset(vis,0,sizeof vis);
    for(ll i=2;i<100;i++)
        if(!vis[i])
        {
            prime[top++]=i;
            for(ll j=i*i;j<100;j+=i)
                vis[j]=1;
        }
}
int main()
{
    Getprime();
//    for(ll i=0;i<25;i++)
//        cout<<prime[i]<<endl;
    ll T,n,a,b,index[25];
    scanf("%lld",&T);
    while(T--)
    {
        scanf("%lld",&n);
        memset(index,0,sizeof index);
        while(n--)
        {
            scanf("%lld%lld",&a,&b);
            if(a==1) continue;
            for(ll j=0;j<25;j++)
            {
                ll k=0;
                while(a%prime[j]==0)
                {
                    k++;
                    a/=prime[j];
                }
                index[j]=index[j]+k*b;
                if(a==1) break;
            }
        }
        ll Max=0;
        for(ll i=0;i<25;i++)
            if(index[i])
                Max=max(findx(prime[i],index[i]),Max);
        printf("%lld\n",Max);
    }
    return 0;
}

 

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值