poj 1078 Gizilch

poj 1078 Gizilch
题目大意:两个人比赛吃葡萄,葡萄上有分数1,2,3,...100.每次吃到一个葡萄,分数就乘上葡萄上的数字.比赛完,两人分别报出自己的分数,但是两人可能误报分数,所以胜利者不一定是分数高的人.
给你两个分数,问胜利者的分数.
评选胜利者依据如下:如果两个人都可能不在说谎,胜利者即为分高的;如果分低者一定在说谎,胜利者即为分高者;否则,胜利者为分高的.

原题如下:(注意hint很有用)

Gizilch
Time Limit: 1000MS
Memory Limit: 10000K
Total Submissions: 1329
Accepted: 514

Description

The game of gizilch has very simple rules. First 100 grapes are labeled, in nontoxic ink, with the numbers 1 to 100. Then, with a cry of ``GIZILCH!'', the referee fires the grapes up into the air with a giant gizilcher. The two players, who each start with a score of ``1'', race to eat the falling (or, shortly thereafter, fallen) grapes and, at the same time, multiply their scores by the numbers written on the grapes they eat. After a minute, the hungry squirrels are let loose to finish the remaining grapes, and each contestant reports his score, the product of the numbers on the grapes he's eaten. The unofficial winner is the player who announces the highest score.
Inevitably, though, disputes arise, and so the official winner is not determined until the disputes are resolved. The player who claims the lower score is entitled to challenge his opponent's score. The player with the lower score is presumed to have told the truth, because if he were to lie about his score, he would surely come up with a bigger better lie. The challenge is upheld if the player with the higher score has a score that cannot be achieved with grapes not eaten by the challenging player. So, if the challenge is successful, the player claiming the lower score wins.

So, for example, if one player claims 343 points and the other claims 49, then clearly the first player is lying; the only way to score 343 is by eating grapes labeled 7 and 49, and the only way to score 49 is by eating a grape labeled 49. Since each of two scores requires eating the grape labeled 49, the one claiming 343 points is presumed to be lying.

On the other hand, if one player claims 162 points and the other claims 81, it is possible for both to be telling the truth (e.g. one eats grapes 2, 3 and 27, while the other eats grape 81), so the challenge would not be upheld.

Unfortunately, anyone who is willing to referee a game of gizilch is likely to have himself consumed so many grapes (in a liquid form) that he or she could not reasonably be expected to perform the intricate calculations that refereeing requires. Hence the need for you, sober programmer, to provide a software solution.

Input

Pairs of unequal, positive numbers, with each pair on a single line, that are claimed scores from a game of gizilch.

Output

Numbers, one to a line, that are the winning scores, assuming that the player with the lower score always challenges the outcome.

Sample Input

343 49 
3599 610 
62 36

Sample Output

49 
610 
62

Hint

A clarification was made at the beginning of the contest to resolve some ambiguity in the problem description noticed a couple of days before the contest. The rules for deciding the winner of a game of gizilch are, first, if both players might be telling the truth, the larger score wins. Second, if the player with the lower score cannot be telling the truth, the player with the higher score wins. Finally, if neither of the previous two conditions holds, the lower score wins..

Source

South Central USA 1998

/*=============================================================================
#     FileName: Gizilch.cpp
#         Desc: poj 1078
#       Author: zhuting
#        Email: [email protected]
#     HomePage: my.oschina.net/locusxt
#      Version: 0.0.1
#    CreatTime: 2013-11-26
#   LastChange: 2013-11-26 23:48:03
#      History:
=============================================================================*/
#include <cstdio>
#include <cstdlib>
#include <string>
#include <cstring>
#define maxn 100


/*
 * 判断a是否合题意
 * a是否是可能出现的
 * a不可能出现则返回1
 */
bool find(int a, int cur)
{
	if (a == 1) return false;
	if (cur > 100) return true;
	int tmp = find(a, cur + 1);
	if (a % cur == 0)
		return find(a / cur, cur + 1) && tmp;
	return tmp;
}

/*如果发现冲突则返回1*/
bool judge(int a, int b, int cur)
{
	if (a == 1 && b == 1) return false;
	if (cur > 100) return true;
	int mod_a = a % cur, mod_b = b % cur;
	int tmp1 = 0, tmp2 = 0, tmp3 = 0;
	if (!mod_a) tmp1 = judge(a / cur, b, cur + 1);
	if (!mod_b) tmp2 = judge(a, b / cur, cur + 1);
	tmp3 = judge(a, b, cur + 1);
	if (!mod_a && !mod_b)
	{
		return tmp1 && tmp2 && tmp3;
	}

	if (!mod_a)
		return tmp1 && tmp3;
	if (!mod_b)
		return tmp2 && tmp3;
	return tmp3;
}


int main()
{
	int m = 0, n = 0;
	while (scanf("%d%d", &m, &n) != EOF)
	{

		if(m < n)/*keep m >= n*/
		{
			int tmp = m;
			m = n;
			n = tmp;
		}

		int tmp1 = find(m, 2);
		int tmp2 = find(n, 2);
		int tmp3 = judge(m, n, 2);
		if (!(tmp1 || tmp2 || tmp3))/*m,n两数均合法,输出大者*/
			printf("%d\n", m);
		else if(tmp2)/*若小者不合法,直接输出大者*/
			printf("%d\n", m);
		else printf("%d\n", n);/*否则输出小者*/
		
	}
	return 0;
}



你可能感兴趣的:(poj,cpp)