首页 > ACM题库 > HDU-杭电 > Hdu 1416 Gizilch-数论[解题报告] C++

Hdu 1416 Gizilch-数论[解题报告] C++


问题描述 :

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.


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


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


343 49 
3599 610 
62 36



int flag1, flag2;   //分别表示n, m是否已被成功分解
void dfs(int n, int m, int fac = 100){
    if(flag1)   //如果n, m均可分解且因子不同
    if(n == 1 && m == 1){   //n, m均分解完
        flag1 = 1;
        flag2 = 1;
    if(m == 1){ //m分解完
        flag2 = 1;
    if (fac < 2)
    if(n % fac == 0)    //精华之处,对同一个因子不同时分解n, m,难点解决
        dfs(n / fac, m, fac - 1);
    if(m % fac == 0)
        dfs(n, m / fac, fac - 1);
    dfs(n, m, fac - 1);

int main(){
    int n, m;
    while(~scanf("%d%d", &n, &m)){
        if(m > n){ //交换m, n
            n = m ^ n;
            m = m ^ n;
            n = m ^ n;
        flag1 = 0;
        flag2 = 0;
        dfs(n, m);
        if(flag1 || !flag2) //如果n成功分解货m无法成功分解
            printf("%d\n", n);
            printf("%d\n", m);
    return 0;


  1. 第23行:
    hash = -1是否应该改成hash[s ] = -1