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) kkk-sided die out of your pocket and you decide to let fate decide.
Of course, the number of ice cream choices, nnn, may not be precisely kkk, in which case you could not just throw the die once, rolling iii, and take the iiith 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 nnn and kkk, can you determine the minimum number iii such that there is a fair algorithm that uses at most iii die 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 nnn and kkk (1≤n,k≤1091 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
题意:我有想买冰淇淋但是不好选择哪一种,所以掷骰子决定,但是筛子的面数不一定等于冰淇淋的数量,所以要多投掷几次保证选中每种冰淇淋的概率相同。
举个栗子:4个冰淇淋我有一个两面的筛子,假设为A,B面投一次A,B两种情况,投两次就有AA,AB,BA,BB四种情况,那么就可以对应4种冰淇淋。
比如4种冰淇淋20个面的筛子,那么每种冰淇淋就可以就可以对应5个面那么投一次就可以。
那么综上所诉只要筛子投掷的情况数能过整除冰淇淋的数目就可以了,那么情况的排列组合就等于面数k的i次方。
ps:因为除了1以外面数k为2,3,4。。。2^29<10^9,2^30>10^9,所以最多投29次。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
ll n,k;
scanf("%lld%lld",&n,&k);
ll r = 1%n;//只有一种冰淇淋的时候r=0;
for(int i=0;i<=29;i++)
{
if(r==0)
{
printf("%d
",i);
break;
}
r=(r*k)%n;
}
if(r!=0)printf("unbounded
");
}
return 0;
}