1. 程式人生 > >C.Yuhao and a Parenthesis

C.Yuhao and a Parenthesis

blank else std poi true == fuel head define

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

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

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: [email protected]
 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

C.Yuhao and a Parenthesis