题目描述
Listening to the music is relax, but for obsessive(强迫症), it may be unbearable.
HH is an obsessive, he only start to listen to music at 12:00:00, and he will never stop unless the song he is listening ends at integral points (both minute and second are 0 ), that is, he can stop listen at 13:00:00 or 14:00:00,but he can't stop at 13:01:03 or 13:01:00, since 13:01:03 and 13:01:00 are not an integer hour time.
Now give you the length of some songs, tell HH whether it's possible to choose some songs so he can stop listen at an integral point, or tell him it's impossible.
Every song can be chosen at most once.
输入描述:
The first line contains an positive integer T(1≤T≤60), represents there are T test cases.For each test case:The first line contains an integer n(1≤n≤10 5), indicating there are n songs.The second line contains n integers a 1,a 2…a n (1≤a i≤10 9 ), the i th integer a i indicates the i th song lasts a i seconds.
输出描述:
For each test case, output one line "YES" (without quotes) if HH is possible to stop listen at an integral point, and "NO" (without quotes) otherwise.
示例1
输入
3 3 2000 1000 3000 3 2000 3000 1600 2 5400 1800
输出
NO YES YES
说明
In the first example it's impossible to stop at an integral point. In the second example if we choose the first and the third songs, they cost 3600 seconds in total, so HH can stop at 13:00:00 In the third example if we choose the first and the second songs, they cost 7200 seconds in total, so HH can stop at 14:00:00
题目大意:
给出N个数,问我们能否从中选出一些数字,使得其加和为3600的倍数。
思路1:
我们直接做O(T*n*3600)的取模背包的话,显然会TLE掉,我们可以暴力一点,直接用bitset优化一下,时间复杂度大约变成O(T*n*3600/64);
无脑简洁。
思路2:
我们不难想到,如果n>3600的话,无论物品的大小,我们肯定能够凑出3600的倍数来。所以问题就可以变成O(3600*3600)的一个暴力背包了。
Ac代码(bitset):
#include<stdio.h>
#include<string.h>
#include<bitset>
#include<iostream>
using namespace std;
int a[150000];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int flag=0;
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%d",&a[i]);
for(int i=1;i<=n;i++)a[i]=a[i]%3600;
bitset<7200>s,q,temp,temp2;
s[a[1]]=1;
for(int i=2;i<=n;i++)
{
q=s<<a[i];
temp=q;
temp=temp>>3600;
temp2=q;
temp2=temp2<<3600;
temp2=temp2>>3600;
s=s|temp;
s=s|temp2;
s[a[i]]=1;
}
if(s[0]==1)printf("YES\n");
else printf("NO\n");
}
}
Ac代码(暴力背包):
#include<stdio.h>
#include<string.h>
using namespace std;
int a[150000];
bool dp[3605][3605];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;scanf("%d",&n);
memset(dp,false,sizeof(dp));
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
}
if(n>3600)printf("YES\n");
else
{
for(int i=0;i<n;i++)
{
dp[i+1][a[i+1]%3600]=true;
for(int j=0;j<3600;j++)
{
if(dp[i][j]==true)
{
if(!(i==0&&j==0))
dp[i+1][j]=true;
dp[i+1][(j+a[i+1])%3600]=true;
}
}
}
if(dp[n][0]==true)printf("YES\n");
else printf("NO\n");
}
}
}