F - 就差最后一题了,什么?不会做?请把题解看完,谢谢!
Time Limit:1000MS Memory Limit:128000KB 64bit IO Format:%lld & %llu
Description
Recently, Losanto find an interesting Math game. The rule is simple: Tell you a number H, and you can choose some numbers from a set {a[1],a[2],……,a[n]}.If the sum of the number you choose is H, then you win. Losanto just want to know whether he can win the game.
Input
There are several cases.
In each case, there are two numbers in the first line n (the size of the set) and H. The second line has n numbers {a[1],a[2],……,a[n]}. 0
#include "iostream"
#include "cstdio"
#include "cstring"
#include <algorithm>
#include <stdlib.h>
#include <set>
using namespace std;
long long a[50];
long long n,h;
long ssum[50]; //用来存储到当前位置为止所有数的和
bool flag=false;
bool ifprint=false; //打印标记 默认状态为false--没打印
void dfs(long long sum,int pos)
{
if(flag==true)
return;
if(sum==h)
{
if(ifprint==false)
{
cout<<"Yes"<<endl;
ifprint=true;
return;
}
else
return;
}
if(flag||pos==n+1)
return;
if(ssum[n] - ssum[pos - 1] + sum < h || sum > h)
return;
//每个数都只有两个状态 要么取 要么不取
dfs(sum+a[pos],pos+1);//TAKE ONE
if(flag==true)
return;
dfs(sum,pos+1);// DONT TAKE THIS ONE
return;
}
int main()
{
while(scanf("%lld %lld",&n,&h)!=EOF)
{
int i=1;
memset(ssum,0,sizeof(ssum));
for(i=1;i<=n;i++)
{
cin>>a[i];
ssum[i] = ssum[i - 1] + a[i];
}
flag=false;
ifprint=false;
dfs(0,1); //sum=0 位置 1
if(flag==false&&ifprint==false)
cout<<"No"<<endl;
}
}