You are standing in the supermarket in front of the freezers. You have a very tough task ahead of you: you have to choose what type of ice cream you want for after dinner that evening. After a while, you give up: they are all awesome! Instead, you take your (fair) kk-sided die out of your pocket and you decide to let fate decide.
Of course, the number of ice cream choices, nn, may not be precisely kk, in which case you could not just throw the die once, rolling ii, and take the iith ice cream choice. You therefore have to use some algorithm that involves zero or more die throws that results in an ice cream choice with every choice being exactly equally likely. Being a good computer scientist, you know about the accept-reject method, which would let you make such a fair choice.
At that point, you remember that you have a very importantcompetition to attend that same afternoon. You absolutely cannot afford to be late for that competition. Because of this, you decide you cannot use the accept-reject method, as there may be no bound on the number of die throws needed to ensure a fair result, so you may end up standing there for a long time and miss the competition! Instead, you resolve to find an algorithm that is fair and uses as few dice choices as possible in the worst case.
Given nn and kk, can you determine the minimum number ii such that there is a fair algorithm that uses at most iidie throws per execution?
Input Format
On the first line one positive number: the number of test cases, at most 100. After that per test case:
- one line with two space-separated integers nn and kk (1 \leq n, k \leq 10^91≤n,k≤109): the number of ice cream choices and the number of sides of your die, respectively.
Output Format
Per test case:
- one line with a single integer: the smallest number of throws after which you are guaranteed to be able to make a fair choice. If there is no such number, print “unbounded” instead.
样例输入
3 4 2 2 4 3 2
样例输出
2 1 unbounded
题意理解:结合样例,我们可以看出,对于这个骰子它的投掷次数决定了一共有多少种选择可能性,设骰子面数为k,投掷次数为t,则k个t相乘即为总的可能种数,那么我们再用冰淇淋的数目n/总的可能种数s,就是平均每个冰淇淋被选出的可能性,为了保证每个冰淇淋倍公平的选择,所以就要保证s能被n整除。
现在就转化成了一个数能被另一个数整除的问题了。两种方法,一种是直接通过累乘k不断判断能否被n整除,第二种方法是通过标准素因子分解来判断能否s能否被n整除。
include <iostream>
#include <fstream>
using namespace std;
#define ll long long
#define MAXN 31
int main()
{
ll T,n,k;
//freopen("A.txt","r",stdin);
cin>>T;
while(T--)
{
cin>>n>>k;
if(n==1)
{
cout << "0" << endl;
continue;
}
ll i,t=k%n;
k=k%n;
for(i = 1; i < MAXN ; i++)
{
if( t==0 )
{
cout << i << endl;
break;
}
t = (t*k)%n;
}
if(t!=0)
cout << "unbounded" << endl;
}
return 0;
}
#include <iostream>
#include <fstream>
#include <algorithm>
using namespace std;
#define ll long long
ll n,k;
void solve()
{
int qc, qk, Max = 1;
bool unbounded = false;
for(int i = 2; i*i <= n; i++)
{
for(qc = 0; n % i == 0; qc++, n /= i);
for(qk = 0; k % i == 0; qk++, k /= i);
if(qc > 0)
{
if(qk == 0)
{
unbounded = true;
break;
}
else
{
Max = max( 1+(qc-1)/qk, Max);
}
}
}
if(k % n > 0)
unbounded = true;
if(unbounded)
cout<<"unbounded"<<endl;
else
cout<<Max<<endl;
}
int main()
{
int T;
cin>>T;
while(T--)
{
cin >> n >> k;
if(n == 1)
{
cout << "0" <<endl;
continue;
}
solve();
}
return 0;
}