Hello 2019 B Petr and a Combination Lock
B. Petr and a Combination Lock
time limit per test 1 second memory limit per test 256 megabytes
input standard input
output standard output
Petr has just bought a new car. He's just arrived at the most known Petersburg's petrol station to refuel it when he suddenly discovered that the petrol tank is secured with a combination lock! The lock has a scale of 360
degrees and a pointer which initially points at zero:
Petr called his car dealer, who instructed him to rotate the lock's wheel exactly n
times. The i-th rotation should be ai degrees, either clockwise or counterclockwise, and after all n
rotations the pointer should again point at zero.
This confused Petr a little bit as he isn't sure which rotations should be done clockwise and which should be done counterclockwise. As there are many possible ways of rotating the lock, help him and find out whether there exists at least one, such that after all n
rotations the pointer will point at zero again.
Input
The first line contains one integer n (1≤n≤15) — the number of rotations. Each of the following n lines contains one integer ai (1≤ai≤180) — the angle of the i-th rotation in degrees.
Output
If it is possible to do all the rotations so that the pointer will point at zero after all of them are performed, print a single word "YES". Otherwise, print "NO". Petr will probably buy a new car in this case.
You can print each letter in any case (upper or lower).
Examples
Input
Copy
3 10 20 30
Output
Copy
YES
Input
Copy
3 10 10 10
Output
Copy
NO
Input
Copy
3 120 120 120
Output
Copy
YES
Note
In the first example, we can achieve our goal by applying the first and the second rotation clockwise, and performing the third rotation counterclockwise.
In the second example, it's impossible to perform the rotations in order to make the pointer point at zero in the end.
In the third example, Petr can do all three rotations clockwise. In this case, the whole wheel will be rotated by 360
degrees clockwise and the pointer will point at zero again.
///////////////////////////////////////////////////////////////////////////////////////////////////
解法很多 我这里是二进制枚举(嗯,暴力) 注意下咋枚举就行,详见代码
#include<cstdio>
#include<cstring>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<stdlib.h>
using namespace std;
const int inf=0x3f3f3f3f;
const int maxn=1000000;
int a[maxn];
int main(){
int n;
cin>>n;
for(int i=0;i<n;i++){
cin>>a[i];
}
int flag=0;
int ans=0;
for(int i=0;i<1<<n;i++){//共有1<<n个状态 枚举每个状态
ans=0;
for(int j=0;j<n;j++){//枚举每个状态的每一位 如1111
if(i&(1<<j)){//这里是 1 就相加(顺时针)
ans+=a[j];
ans=ans%360;
}else{ // 0 就想减(逆时针)
ans-=a[j];
ans=ans%360;
}
}
if(ans==0){//即转了一圈
flag=1;
break;
}
}
if(flag){
cout<<"YES";
}else{
printf("NO");
}
return 0;
}