1. 程式人生 > >hdu 1416Gizilch(dfs 深搜)

hdu 1416Gizilch(dfs 深搜)

Problem 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 of1’’, 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 題意:兩個人比賽吃葡萄,葡萄的編號是1~100,每吃一個葡萄就會相應的乘以這個葡萄的編號。吃完後報兩個數,代表自己的成績。有可能虛報。如果兩個人都有可能虛報或者都沒有虛報,就是數字大的人獲勝。如果數字大的人說謊了,就是數字小的人獲勝了。 分析:由於每個葡萄只有一個,所以就像第一個樣例343和49,49*7=343。但是編號49的葡萄只有一個,那麼343就說謊了。如果說49說謊了,那為什麼不說大了??這樣看來,就是343說謊了,獲勝的就是49。如果a(宣告得分是a的人) 吃了葡萄i,必須a%i0 如果搜尋到i,以前a的得分是s[i-1],b的是t[i-1]; 那麼i如果是 a吃,必須 (a/s[i-1])%i0

在吃i後,s[i]=s[i-1]*i;

如果到某步後,a=s[i];且b=t[i], 說明a,b都是真實的,否則有一個是不真實的。 程式碼如下:

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

int flag;
int fuck;
int maxx;
int minn;
int f[101];

int dfs(int t)
{
	if(t==1)
	{
		if(flag==0)
		{
			fuck=1;
			flag=1;
			if(dfs(maxx)) return 1;
			else
			{
				flag=0;
				return 0;
			}
		}
		return 1;
	}
	int len=100<t?100:t;
	for(int i=2;i<=len;i++)
	{
		if(t%i==0&&f[i]==0)
		{
			f[i]=1;
			if(dfs(t/i)) return 1;
			f[i]=0;
		}
	}
	return 0;
}
int main()
{
	int x,y;
	while(scanf("%d%d",&x,&y)!=EOF)
	{
		int ans;
		flag=0;
		fuck=0;
		maxx=max(x,y);
		minn=min(x,y);
		memset(f,0,sizeof(f));
		int tt = dfs(minn);
		if(fuck==0) ans=maxx;
		else if(fuck==1&&tt==1) ans=maxx;
		else ans=minn;
		printf("%d\n",ans);
	}
	return 0;
}

努力加油a啊,(o)/~