老BOJ 11 Counting

10 篇文章 0 订阅
9 篇文章 0 订阅
Counting
Accept:100    Submit:807
Time Limit:2000MS    Memory Limit:65536KB

Description

 

We are familiar with the game called “Counting 24”. Now it comes a problem that we want to know whether we can figure out the exact answer with given 4 numbers only using +,-,*,/ and (,).

 

Input

There are several cases in a test. Each case contains only 5 integers, a1, a2, a3, a4 and required answer. All integers are positive and no more than 100.

Output

If it is possible to figure out the required answer, output “Possible”; otherwise output “Impossible”.

Sample Input

 

5 5 1 5 24

 

Sample Output

 

Impossible


#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<set>
using namespace std;
int a[4],ans;
set<int>s[16];
set<int>::iterator it1,it2;
inline void deal(int x,int tmp){s[x].insert(tmp);}
void cal(int x){
    if(!s[x].empty())return;
    for(int i=1;i<16;i++)
        if((x&i)==i)
            for(it1=s[i].begin();it1!=s[i].end();it1++)
                for(it2=s[x-i].begin();it2!=s[x-i].end();it2++){
                    deal(x,(*it1)+(*it2));
                    deal(x,(*it1)*(*it2));
                    deal(x,(*it1)-(*it2));
                    if((*it2)&&(*it1)%(*it2)==0)deal(x,(*it1)/(*it2));
                }
}
bool can(){
    int i;
    for(i=0;i<16;i++)
        s[i].clear();
    for(i=0;i<4;i++)
        s[1<<i].insert(a[i]);
    for(i=1;i<16;i++)
        cal(i);
    return s[15].find(ans)==s[15].end()?false:true;
}
int main(){
    while(~scanf("%d%d%d%d%d",&a[0],&a[1],&a[2],&a[3],&ans))
        printf("%s\n",can()?"Possible":"Impossible");
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值