bitmasks brute force dp *1200

Please click on ads to support us..

Python Code:

n=int(input())
l=[]
for i in range(n):
    l.append(int(input()))
for i in range(0,2**n):
    res=0
    for j in range(n):
        if(i &(2**j)):
            res+=l[j]
        else:
            res-=l[j]
    if res%360==0:
        print("yes")
        break
else:
    print("no")
    

C++ Code:

#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
#include<numeric>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vl;
#define all(v)          v.begin(),v.end()
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
void Never_give_up() {
    std::ios_base::sync_with_stdio(NULL);
    cin.tie(NULL); cout.tie(NULL);
#ifndef ONLINE_JUDGE 
    freopen("in.txt", "r", stdin);
#endif
}
void solve() {
    int n;  cin >> n;
    bool Ok = false;
    vi v(n);  for (auto& it : v) cin >> it;
    for (int mask = 0; mask < (1 << n); mask++) {
        int sum = 0;
        for (int i = 0; i < n; i++) {
            if (mask & (1 << i)) sum += v[i];
            else sum -= v[i];
        }
        Ok |= (sum % 360 == 0);
    }
    cout << ((Ok) ? "YES" : "NO");
}
int main() {
    //وَلَسَوْفَ يُعْطِيكَ رَبُّكَ فَتَرْضَى
    Never_give_up();
    int T = 1;  //cin >> T;
    while (T--) {
        solve();
    }
    return 0;
}
	  					  			 			 	  			 		 	 	


Comments

Submit
0 Comments
More Questions

1651B - Prove Him Wrong
381A - Sereja and Dima
41A - Translation
1559A - Mocha and Math
832A - Sasha and Sticks
292B - Network Topology
1339A - Filling Diamonds
910A - The Way to Home
617A - Elephant
48A - Rock-paper-scissors
294A - Shaass and Oskols
1213A - Chips Moving
490A - Team Olympiad
233A - Perfect Permutation
1360A - Minimal Square
467A - George and Accommodation
893C - Rumor
227B - Effective Approach
1534B - Histogram Ugliness
1611B - Team Composition Programmers and Mathematicians
110A - Nearly Lucky Number
1220B - Multiplication Table
1644A - Doors and Keys
1644B - Anti-Fibonacci Permutation
1610A - Anti Light's Cell Guessing
349B - Color the Fence
144A - Arrival of the General
1106A - Lunar New Year and Cross Counting
58A - Chat room
230A - Dragons