BAPC 2014 Preliminary套题 A.Choosing Ice Cream

传送门:点击打开链接

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^91n,k109): 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

题目来源

BAPC 2014 Preliminary

题目大意:有n种冰淇淋和一个k面的骰子,问至少扔几次骰子使得选择每种骰子的概率相等。

先把大神的代码先放这,还没看懂大哭当然也可以用快速幂试试

// Time complexity: O(log(n))
// Memory: O(1)

// @EXPECTED_RESULTS@: CORRECT

/* Solution method:
 *
 * Insight: n divides k^t iff n divides d^t, where d = gcd(n,k).
 * Hence n divides k^t iff n/d divides d^(t-1).
 * Let n' = n/d, k' = d and t' = t-1. Then n divides k^t iff n' divides k'^t'.
 * We can repeat this process.
 * If at some point we have n' = 1, then t' = 0 is the smallest t';
 *   t is then the number of iterations.
 * If it is not unbounded, we should eventually get t' = 0. Then n' = 1 must hold.
 * Hence, if we never reach n' = 1, it is unbounded.
 * We never reach n' = 1 iff at some point k' = 1 (and n' > 1).
 */

#include <algorithm>
#include <cstdio>
#include<iostream>
using namespace std;

// Greatest common divisor
int gcd (int a, int b)
{ return b ? gcd(b, a%b) : a;
}

int main()
{	int runs, run, n, k, t;
	scanf("%d", &runs);
	for (run = 0; run < runs; run++)
	{
		scanf("%d %d", &n, &k);
		//cout<<"n="<<n<<",k="<<k<<endl;
		for (t = 0; n > 1 && k > 1; t++)
		{
		  //cout<<"***n="<<n<<",k="<<k<<endl;
		  k = gcd(n, k);
		  n /= k;
		}
		if (n > 1)
			printf("unbounded\n");
		else
			printf("%d\n", t);
	}
	return 0;
}






猜你喜欢

转载自blog.csdn.net/qq_37275680/article/details/80960889
今日推荐