2014-01-24 10:07:01Morris

[UVA][搜索] 653 - Gizilch


  Gizilch 

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



Clarification:

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.



Miguel A. Revilla
2000-01-17

題目描述:


兩個人玩遊戲,只能從 1 - 100 中間挑數字,每個數字只能使用一次。
因此兩個人不能使用同一個數字,然後把這些數字相乘起來。

現在有人作弊,低分的要挑戰高分的人,如果發現高分的人有作弊嫌移。

如果高分的有作弊,輸出低分。反之,輸出高分。

題目解法:


這題沒有說明相乘之後會不會使用到大數,不過根據測試起來似乎沒有跑出 64 bits。

接著就窮舉分解情況,將兩個數字分解到 1 - 100 之間的數字,試圖將兩個數字都變成 1。

如果原本低分就無法分解的話,則可以直接輸出高分的。

// 題目相當地令人討厭。

#include <stdio.h>
#include <vector>
#include <string.h>
#include <iostream>
using namespace std;
int checkflag = 0;
int mark[105];
int dfs(long long n, long long m, int st, int alt) {
    if(n == 1) {
        checkflag = 1;
        if(alt == 1)
            return 1;
        else
            return dfs(m, -1, 100, alt+1);
    }
    for(int i = st; i >= 1; i--) {
        if(n%i == 0 && mark[i] == 0) {
            mark[i] = 1;
            if(dfs(n/i, m, i-1, alt))
                return 1;
            mark[i] = 0;
        }
    }
    return 0;
}
int main() {
    long long n, m;
    while(cin >> n >> m) {
        if(n < m)    swap(n, m);
        memset(mark, 0, sizeof(mark));
        checkflag = 0;
        if(dfs(m, n, 100, 0) || checkflag == 0)
            printf("%lld\n", n);
        else
            printf("%lld\n", m);
    }
    return 0;
}