Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define int int64_t
- using namespace std;
- int n, k;
- vector<vector<string>> s;
- int dp[4096][1001];
- bool rec(int cur, int i){
- if(i >= s.size())
- return true;
- if(dp[cur][i] == -2)
- return dp[cur][i] = false;
- if (dp[cur][i] != -1)
- return dp[cur][i];
- dp[cur][i] = -2;
- if(s[i].front() == "jump")
- return dp[cur][i] = rec(cur, stoi(s[i][2]) + 1);
- int nxt = cur;
- if(s[i].front() == "begin")
- return dp[cur][i] = rec(cur, i + 1);
- if(s[i].front() == "end")
- return true;
- if(s[i].front() == "and"){
- if((cur>>stoi(s[i][1]) & 1) && (cur>>stoi(s[i][2]) & 1))
- nxt|= 1<<stoi(s[i][1]);
- else
- nxt &= ~(1<<stoi(s[i][1]));
- }
- if(s[i].front() == "or"){
- if((cur>>stoi(s[i][1]) & 1) || (cur>>stoi(s[i][2]) & 1))
- nxt|= 1<<stoi(s[i][1]);
- else
- nxt &= ~(1<<stoi(s[i][1]));
- }
- if(s[i].front() == "xor"){
- if((cur>>stoi(s[i][1]) & 1) ^ (cur>>stoi(s[i][2]) & 1))
- nxt|= 1<<stoi(s[i][1]);
- else
- nxt &= ~(1<<stoi(s[i][1]));
- }
- if(s[i].front() == "not")
- nxt ^= 1<<stoi(s[i][1]);
- if(s[i].front() == "move"){
- if(cur>>stoi(s[i][2]) & 1)
- nxt |= 1<<stoi(s[i][1]);
- else
- nxt &= ~(1<<stoi(s[i][1]));
- }
- if(s[i].front() == "set"){
- if(stoi(s[i][2]))
- nxt |= 1<<stoi(s[i][1]);
- else
- nxt &= ~(1<<stoi(s[i][1]));
- }
- return dp[cur][i] = rec(nxt, i + 1);
- }
- int32_t main() {
- cin >> n >> k;
- s.resize(n + 1);
- for(int i = 0; i < 1<<k; i++)
- for(int j = 1; j <= n; j++)
- dp[i][j] = -1;
- for(int i = 1; i <= n; i++){
- string cur;
- cin >> cur;
- s[i].push_back(cur);
- if(cur == "begin" || cur == "end")
- continue;
- if(cur != "not"){
- cin >> cur;
- s[i].push_back(cur);
- }
- cin >> cur;
- s[i].push_back(cur);
- }
- for(int mask = 0; mask < 1<<k; mask++){
- if(!rec(mask, 1)) {
- cout << "No\n";
- vector<int> ans;
- int pw = 1<<(k - 1), cur = mask;
- while(pw > 0){
- if(cur >= pw) {
- ans.push_back(1);
- cur -= pw;
- }
- else
- ans.push_back(0);
- pw >>= 1;
- }
- reverse(ans.begin(), ans.end());
- for(auto j: ans)
- cout << j;
- return 0;
- }
- }
- cout << "Yes\n";
- }
Advertisement
Add Comment
Please, Sign In to add comment