Advertisement
mihaimarcel21

Logic_of

Mar 23rd, 2021
640
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.65 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. #define MOD 666013
  4. #define N_MAX 10
  5.  
  6. using namespace std;
  7.  
  8. ifstream fin("logic.in");
  9. ofstream fout("logic.out");
  10.  
  11. int t;
  12. int N, Q;
  13. int L;
  14. char op[1 << N_MAX];
  15. int E[1 << N_MAX];
  16. int cnt[1 << N_MAX][2];
  17.  
  18. int main()
  19. {
  20.     fin >> t;
  21.     fin >> N;
  22.     for(int i = 0; i < N; i++) {
  23.         for(int j = 1; j <= (1 << i); j++) {
  24.             fin >> op[++L];
  25.         }
  26.     }
  27.  
  28.     if(t == 1) {
  29.         for(fin >> Q; Q; Q--) {
  30.             for(int i = 1; i <= (1 << N); i++) {
  31.                 char c;
  32.                 fin >> c;
  33.                 E[L + i] = c - '0';
  34.             }
  35.  
  36.             for(int i = L; i >= 1; i--) {
  37.                 if(op[i] == '|') {
  38.                     E[i] = E[2 * i] | E[2 * i + 1];
  39.                 }
  40.                 else {
  41.                     E[i] = E[2 * i] & E[2 * i + 1];
  42.                 }
  43.             }
  44.  
  45.             fout << E[1] << "\n";
  46.         }
  47.     }
  48.     else {
  49.         int result;
  50.         fin >> result;
  51.         for(int i = 1; i <= (1 << N); i++) {
  52.             cnt[L + i][0] = cnt[L + i][1] = 1;
  53.         }
  54.  
  55.         for(int i = L; i >= 1; i--) {
  56.             for(int le = 0; le <= 1; le++) {
  57.                 for(int ri = 0; ri <= 1; ri++) {
  58.                     if(op[i] == '|') {
  59.                         cnt[i][le | ri] = (cnt[i][le | ri] + 1ll * cnt[2 * i][le] * cnt[2 * i + 1][ri]) % MOD;
  60.                     }
  61.                     else {
  62.                         cnt[i][le & ri] = (cnt[i][le & ri] + 1ll * cnt[2 * i][le] * cnt[2 * i + 1][ri]) % MOD;
  63.                     }
  64.                 }
  65.             }
  66.         }
  67.  
  68.         fout << cnt[1][result] << "\n";
  69.     }
  70.  
  71.     return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement