现在的位置: 首页 > 综合 > 正文

HDU/HDOJ 3641 Treasure Hunting 2010年杭州赛区网络赛

2018年01月20日 ⁄ 综合 ⁄ 共 2821字 ⁄ 字号 评论关闭

 

Treasure Hunting

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 985    Accepted Submission(s): 316

Problem 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)
 

Source
 
一年前,实力很弱,居然想到了取对数神马玄乎又玄的东西。
昨天跑去重新做了下,发现原来是个水题,可以直接二分答案计算出来
 
解法:
把所有的ai进行一个质因数分解,然后二分答案,判断当前的mid值的阶乘是否能够整除M
 
我的代码:
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<vector>

using namespace std;

typedef __int64 ll;

ll num[105];
ll a[105],b[105];
ll n;

void solve(ll N,ll th)
{
    int i,cnt;
    for(i=2;i*i<=N;i++)
    {
        cnt=0;
        if(N%i==0)
        {
            N=N/i;
            cnt++;
            while(N%i==0)
            {
                N=N/i;
                cnt++;
            }
            num[i]=num[i]+cnt*th;
        }
        if(N==1)
            break;
    }
    if(N>1)
        num[N]=num[N]+th;
}

ll cal(ll x,ll N)
{
    ll ret=0;
    while(N)
    {
        N=N/x;
        ret=ret+N;
    }
    return ret;
}

bool judge(ll x)
{
    ll i,temp;
    for(i=1;i<=100;i++)
    {
        if(num[i]>0)
        {
            temp=cal(i,x);
            if(temp<num[i])
                return false;
        }
    }
    return true;
}

void init()
{
    memset(num,0,sizeof(num));
}

int main()
{
    ll i,t,left,right,mid,ans;
    scanf("%I64d",&t);
    while(t--)
    {
        scanf("%I64d",&n);
        init();
        for(i=1;i<=n;i++)
        {
            scanf("%I64d%I64d",&a[i],&b[i]);
            solve(a[i],b[i]);
        }
        left=0,right=1000000000000000000ll;
        while(left<=right)
        {
            mid=(left+right)>>1;
            if(judge(mid))
            {
                right=mid-1;
                ans=mid;
            }
            else
            {
                left=mid+1;
            }
        }
        printf("%I64d\n",ans);
    }
    return 0;
}

抱歉!评论已关闭.