ProgMe

Untitled

Jan 20th, 2021 (edited)
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.64 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define int int64_t
  3.  
  4. using namespace std;
  5.  
  6. int n, k;
  7. vector<vector<string>> s;
  8. int dp[4096][1001];
  9.  
  10. bool rec(int cur, int i){
  11. if(i >= s.size())
  12. return true;
  13. if(dp[cur][i] == -2)
  14. return dp[cur][i] = false;
  15. if (dp[cur][i] != -1)
  16. return dp[cur][i];
  17. dp[cur][i] = -2;
  18. if(s[i].front() == "jump")
  19. return dp[cur][i] = rec(cur, stoi(s[i][2]) + 1);
  20. int nxt = cur;
  21. if(s[i].front() == "begin")
  22. return dp[cur][i] = rec(cur, i + 1);
  23. if(s[i].front() == "end")
  24. return true;
  25. if(s[i].front() == "and"){
  26. if((cur>>stoi(s[i][1]) & 1) && (cur>>stoi(s[i][2]) & 1))
  27. nxt|= 1<<stoi(s[i][1]);
  28. else
  29. nxt &= ~(1<<stoi(s[i][1]));
  30. }
  31. if(s[i].front() == "or"){
  32. if((cur>>stoi(s[i][1]) & 1) || (cur>>stoi(s[i][2]) & 1))
  33. nxt|= 1<<stoi(s[i][1]);
  34. else
  35. nxt &= ~(1<<stoi(s[i][1]));
  36. }
  37. if(s[i].front() == "xor"){
  38. if((cur>>stoi(s[i][1]) & 1) ^ (cur>>stoi(s[i][2]) & 1))
  39. nxt|= 1<<stoi(s[i][1]);
  40. else
  41. nxt &= ~(1<<stoi(s[i][1]));
  42. }
  43. if(s[i].front() == "not")
  44. nxt ^= 1<<stoi(s[i][1]);
  45. if(s[i].front() == "move"){
  46. if(cur>>stoi(s[i][2]) & 1)
  47. nxt |= 1<<stoi(s[i][1]);
  48. else
  49. nxt &= ~(1<<stoi(s[i][1]));
  50. }
  51. if(s[i].front() == "set"){
  52. if(stoi(s[i][2]))
  53. nxt |= 1<<stoi(s[i][1]);
  54. else
  55. nxt &= ~(1<<stoi(s[i][1]));
  56. }
  57. return dp[cur][i] = rec(nxt, i + 1);
  58. }
  59.  
  60. int32_t main() {
  61. cin >> n >> k;
  62. s.resize(n + 1);
  63. for(int i = 0; i < 1<<k; i++)
  64. for(int j = 1; j <= n; j++)
  65. dp[i][j] = -1;
  66. for(int i = 1; i <= n; i++){
  67. string cur;
  68. cin >> cur;
  69. s[i].push_back(cur);
  70. if(cur == "begin" || cur == "end")
  71. continue;
  72. if(cur != "not"){
  73. cin >> cur;
  74. s[i].push_back(cur);
  75. }
  76. cin >> cur;
  77. s[i].push_back(cur);
  78. }
  79. for(int mask = 0; mask < 1<<k; mask++){
  80. if(!rec(mask, 1)) {
  81. cout << "No\n";
  82. vector<int> ans;
  83. int pw = 1<<(k - 1), cur = mask;
  84. while(pw > 0){
  85. if(cur >= pw) {
  86. ans.push_back(1);
  87. cur -= pw;
  88. }
  89. else
  90. ans.push_back(0);
  91. pw >>= 1;
  92. }
  93. reverse(ans.begin(), ans.end());
  94. for(auto j: ans)
  95. cout << j;
  96. return 0;
  97. }
  98. }
  99. cout << "Yes\n";
  100. }
Advertisement
Add Comment
Please, Sign In to add comment