Treasure Hunting
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 953 Accepted Submission(s): 306
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?
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)
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
6Hintn! 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
题意:求x! % (ai^bi*ai+1^bi+1..)=0 的最小的x值
分析:
这道题差点把我给弄崩溃。。求n!中的质数p因子的个数是关键!(要想不被TLE就得使用这种方法,呵呵)
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
int sze; //2-100内的素数的个数,这里筛100以内的素数是有原因的(通过估计)
__int64 hash[30]; //plist[i]的指数
__int64 plist[30];//素数列表
__int64 x;
bool isPrim(int num)//判断素数
{
for (int i = 2; i * i <= num; ++i)
{
if (num % i == 0)
return false;
}
return true;
}
void init()//筛2-100内的素数
{
sze = 0;
for (int i = 2; i <= 100; ++i)
{
if (isPrim(i))
plist[sze++] = i;
}
}
__int64 GetP(__int64 x, __int64 p)//求x!中有多少个以质数p为因子数
{
__int64 res = 0;
while (x)
{
res = res + x / p ;
x = x / p ;
}
return res;
}
__int64 getp(__int64 x,__int64 p)
{
__int64 res = 0;
while(x)
{
res + = x / p;
x / = p;
}
return res;
}
bool Compare(__int64 x)//当n!> (mul)ai^bi return true否则 return false
{
for (int i = 0; i < sze; ++i)
{
if (hash[i] > 0)
{
__int64 p = GetP(x, plist[i]);
if (p < hash[i])
return false;
}
}
return true;
}
void BinS() //二分查找
{
__int64 L = 0;
__int64 H =(__int64) 1<<61;//2^61
__int64 m;
while (L < H)
{
m = (L + H) / 2;
if (Compare(m))
H = m;
else
L = m + 1;
}
x = L;
}
int main()
{
int test, cas;
init();
scanf("%d", &test);
for (cas = 1; cas <= test; ++cas)
{
int n;
__int64 a, b;
scanf("%d", &n);
memset(hash, 0, sizeof(hash));
for (int i = 0; i < n; ++i)
{
scanf("%I64d %I64d", &a, &b);
for (int j = 0; j<sze; j++)
{
if (a % plist[j] == 0)
{
hash[j] = hash[j] + b;//求j质数对应的幂
a = a / plist[j];
--j;
}
}
}
BinS();
printf("%I64d\n", x);
}
return 0;
}