zoj 1003 Crashing Balloon(两次dfs)

Crashing Balloon

Time Limit: 2 Seconds      Memory Limit: 65536 KB

On every June 1st, the Children's Day, there will be a game named "crashing balloon" on TV.  The rule is very simple.  On the ground there are 100 labeled balloons, with the numbers 1 to 100.  After the referee shouts "Let's go!"the two players, who each starts with a score of  "1", race to crashthe balloons by their feet and, at the same time,multiply their scores by the numbers written on the balloons they crash. After a minute, the little audiences are allowed to take the remaining balloons away, and each contestant reports his\her score, the product of the numberson the balloons he\she's crashed.  The unofficial winner is the player whoannounced the highest score.

Inevitably, though, disputes arise, and so the official winner is notdetermined until the disputes are resolved.  The player who claimsthe lower score is entitled to challenge his\her opponent's score.  Theplayer with the lower score is presumed to have told the truth, becauseif he\she were to lie about his\her score, he\she would surely come up with a biggerbetter lie.  The challenge is upheld if the player with the higherscore has a score that cannot be achieved with balloons not crashed by thechallenging player.  So, if the challenge is successful, the playerclaiming the lower score wins.

So, for example, if one player claims 343 points and the other claims49, then clearly the first player is lying; the only way to score 343 isby crashing balloons labeled 7 and 49, and the only way to score 49 is by crashinga balloon labeled 49.  Since each of two scores requires crashing theballoon 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 claims81, it is possible for both to be telling the truth (e.g. one crashes balloons2, 3 and 27, while the other crashes balloon 81), so the challenge would notbe upheld.

By the way, if the challenger made a mistake on calculating his/her score, then the challenge would not be upheld. For example, if one player claims 10001 points and the other claims 10003, then clearly none of them are tellingthe truth. In this case, the challenge would not be upheld.

Unfortunately, anyone who is willing to referee a game of crashing balloon islikely to get over-excited in the hot atmosphere that he\she could not reasonably be expected to perform the intricate calculationsthat 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, thatare claimed scores from a game of crashing balloon.

Output

Numbers, one to a line, that are the winning scores, assuming that theplayer with the lower score always challenges the outcome.
Sample Input
343 49
3599 610
62 36

Sample Output
49
610
62

Source: Zhejiang University Local Contest 2001


题意:

就是给出两个数,如果较小的数能够分解成100以内不同因数的连乘积,那么这个较小的数可以挑战较大的数,如果较大的数也可以分解为100以内不同因数的连乘积,并且因数不能和较小的某一分解的因数相同,那么挑战是失败的,如果大数分解不成立(无法用100以内数分解,或着必须和较小的数共用因数)那么挑战成功。如343 49 

343分解为 1 * 7 * 49,但是49  可以分解为 49 * 1 ,49不能重用,因为气球只能扎破一次。

有着这样的逻辑:

小  大  输出

能  能    大

能 不能 小

不能     大

也就是说只有在小的数自己能分解成功,并且大的数不能分解成功才输出小的数,用flag1,flag2,标记小数和大数是否能分解。我们先把所有两数所有因子都找出来,然后先dfs出所有能组成小数的可能,对于搜出的每一种可能,在所有因子中尚未被标记的因子中我们再次进行dfs,查看此时能否组成大数。

(说句题外话,在网上看到这个题目的题解,震惊的发现大部分都是同样的代码,也并没解释清楚人家的代码什么含义,自己都难道并不知道动手写一些么? 还非常乐观的承认借鉴别人的~!)

这是我的代码:

/*=============================================================================
#
#      Author: liangshu - cbam 
#
#      QQ : 756029571 
#
#      School : 哈尔滨理工大学 
#
#      Last modified: 2015-09-02 20:12
#
#     Filename: 新建文本文档 (3).cpp
#
#     Description: 
#        The people who are crazy enough to think they can change the world, are the ones who do ! 
=============================================================================*/
#
#include<iostream>
#include<sstream>
#include<algorithm>
#include<cstdio>
#include<string.h>
#include<cctype>
#include<string>
#include<cmath>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
using namespace std;
const int INF = 10000;
int cnt[INF];
int vis[INF];
int ans1 , ans ;
int flag1, flag2;
int a, b, t;
bool dfs1()
{
    if(ans1 > b)return 0;
    if(ans1 == b)
        return 1;
    for(int i = 0; i < t; i++)
    {
        if(!vis[i])
        {
            ans1 *= cnt[i];
            vis[i] = 1;
            if(dfs1())
                return 1;
            vis[i] = 0;
            ans1 /= cnt[i];
        }
    }
    return 0;
}

bool dfs2()
{
    if(ans > a)return 0;
    if(ans == a)
    {
        flag1 = 1;
        if(dfs1())
           {
               flag2 = 1;
                return 1;
           }
    }
    for(int i = 0; i < t; i++)
    {
        if(!vis[i])
        {
            vis[i] = 1;
            ans *= cnt[i];
            if(dfs2())
                return 1;
            vis[i] = 0;
            ans /= cnt[i];
        }
    }
    return 0;
}

int main()
{

    while(cin>>a>>b)
    {
        ans1 = ans = 1;
        flag1 = flag2 = 0;
        t = 0;
        if(a > b)
            swap(a, b);
        memset(vis, 0, sizeof(vis));
        memset(cnt, 0, sizeof(cnt));
        for(int i = 1; i <= 100; i++)
        {
            if(a % i == 0 && !vis[i])
            {
                cnt[t++] = i;
                vis[i] = 1;
            }
        }
        for(int i = 1; i <= 100; i++)
        {
            if(b % i == 0 && !vis[i])
            {
                cnt[t++] = i;
                vis[i] = 1;
            }
        }
        memset(vis, 0, sizeof(vis));
        dfs2();
        if(flag1 && !flag2)
            cout<<a<<endl;
        else
            cout<<b<<endl;
    }
    return 0;
}
/*
2 3
1 2
*/




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值