B.Petr and a Combination Lock

 https://codeforces.com/contest/1097/problem/A

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 360360 degrees and a pointer which initially points at zero:

 

 

Petr called his car dealer, who instructed him to rotate the lock's wheel exactly nn times. The ii-th rotation should be aiai degrees, either clockwise or counterclockwise, and after all nn 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 nn rotations the pointer will point at zero again.

 

 

Input

 

The first line contains one integer nn (1n151≤n≤15) — the number of rotations.

Each of the following nn lines contains one integer aiai (1ai1801≤ai≤180) — the angle of the ii-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
3
10
20
30
output
YES
input
3
10
10
10
output
NO
input
3
120
120
120
output
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 360360 degrees clockwise and the pointer will point at zero again.

1左移i位, 然后与c按位与。
&当两个操作数对应位都是1,结果才是1.
而1<<i 只有右数第i位是1, 其他都是0.
那么要结果非0, 除非c的第i位也是1.
所以 这个就是判断c的第i位是否为1, 如为1, 那么if成立。 否则if不成立。
PS:这里说的第i位都是从0计数的。

所以此处的意思从1开始遍历全部+1,-1的过程

递推

 1 /*
 2  Author: LargeDumpling
 3  Email: LargeDumpling@qq.com
 4  Edit History:
 5     2019-01-04    File created.
 6 */
 7 
 8 #include<iostream>
 9 #include<cstdio>
10 #include<cstdlib>
11 #include<cstring>
12 #include<cmath>
13 #include<algorithm>
14 using namespace std;
15 const int MAXN=15;
16 int n,a[MAXN],limit;
17 int main()
18 {
19     bool flag=false;
20     scanf("%d",&n);
21     for(int i=0;i<n;i++)
22         scanf("%d",&a[i]);
23     limit=1<<n;
24     for(int S=0;S<limit;S++)
25     {
26         int sum=0;
27         for(int i=0;i<n;i++)
28             if((S>>i)&1) sum+=a[i];
29             else sum-=a[i];
30         if(sum%360==0)
31             flag=true;
32     }
33     if(flag) puts("YES");
34     else puts("NO");
35     fclose(stdin);
36     fclose(stdout);
37     return 0;
38 }
View Code

 

递归 dfs

 1 #include <bits/stdc++.h>
 2 #include<iostream>
 3 #include<algorithm>
 4 #include<cstdlib>
 5 #include<cstring>
 6 #include<cstdio>
 7 #include<string>
 8 #include<vector>
 9 #include<bitset>
10 #include<queue>
11 #include<deque>
12 #include<stack>
13 #include<cmath>
14 #include<list>
15 #include<map>
16 #include<set>
17 //#define DEBUG
18 #define RI register int
19 using namespace std;
20 typedef long long ll;
21 //typedef __int128 lll;
22 const int N=100000+10;
23 const int MOD=1e9+7;
24 const double PI = acos(-1.0);
25 const double EXP = 1E-8;
26 const int INF = 0x3f3f3f3f;
27 int t,n,m,k,q,ans;
28 int a[N];
29 char str;
30 void dfs(int c,int x){
31     if(c>=n){
32         if(x%360==0)
33             ans=1;
34         return;
35     }
36     if(ans)
37         return;
38     dfs(c+1,x+a[c+1]);
39     dfs(c+1,x-a[c+1]);
40 }
41 int main()
42 {
43 #ifdef DEBUG
44     freopen("input.in", "r", stdin);
45     //freopen("output.out", "w", stdout);
46 #endif
47     scanf("%d",&n);
48     for(int i=1;i<=n;i++){
49         cin>>a[i];
50     }
51     dfs(0,0);
52     if(ans)
53         cout << "YES" << endl;
54     else
55         cout << "NO" << endl;
56 
57     return 0;
58 }
View Code

 

转载于:https://www.cnblogs.com/DWVictor/p/10224854.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值