Advertisement
welleyth

XOR grid thinking

Aug 5th, 2022
843
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.03 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include <ext/pb_ds/assoc_container.hpp>
  3. #include <ext/pb_ds/tree_policy.hpp>
  4. using namespace __gnu_pbds;
  5. using namespace std;
  6.  
  7. #define int long long
  8. #define mp make_pair
  9. #define pb push_back
  10.  
  11. #pragma GCC optimize("Ofast")
  12. #pragma GCC optimize("unroll-loops")
  13. #pragma GCC target("avx2")
  14.  
  15. constexpr int INF = (int)2e18;
  16. constexpr int N = (int)2e5 + 111;
  17. constexpr int md = (int)998244353;
  18. constexpr int mdPower = (int)1e9+7 - 1;
  19. constexpr double eps = 1e-7;
  20.  
  21. typedef __int128 _uint;
  22. typedef long long ll;
  23.  
  24. mt19937_64 rnd(time(nullptr));
  25.  
  26. void add(int& a,int b){
  27.     a += b; if(a >= md) a -= md;
  28. }
  29. void sub(int& a,int b){
  30.     a -= b; while(a < 0) a += md;
  31. }
  32. void add(__int128& a,int b){
  33.     a += b;
  34. }
  35. void sub(__int128& a,int b){
  36.     a -= b;
  37. }
  38.  
  39. int bpow(int a,int b){
  40.     if(b == 0)
  41.         return 1;
  42.     if(b % 2 == 0){
  43.         int t = bpow(a,b>>1);
  44.         return 1ll*t*t%md;
  45.     }
  46.     return 1ll * a*bpow(a,b-1) % md;
  47. }
  48.  
  49. int inv(int a){
  50.     return bpow(a,md-2);
  51. }
  52.  
  53. int fac[N],invfac[N];
  54.  
  55. void init(){
  56.     fac[0] = 1;
  57.     for(int i = 1; i < N; i++){
  58.         fac[i] = (fac[i-1] * i) % md;
  59.     }
  60.     invfac[N-1] = inv(fac[N-1]);
  61.     for(int i = N-2; i >= 0; i--){
  62.         invfac[i] = (invfac[i+1] * (i+1))%md;
  63.     }
  64.     return;
  65. }
  66.  
  67. int C(int n,int k){
  68.     if(k > n)
  69.         return 0;
  70.     return fac[n] * invfac[k] % md * invfac[n-k] % md;
  71. }
  72.  
  73. int A(int n,int k){
  74.     if(k > n)
  75.         return 0;
  76.     return fac[n] * invfac[n-k] % md;
  77. }
  78. int g[1111][1111];
  79. int n;
  80. string to_binary(int x){
  81.     string s = "";
  82.     for(int i = 0; i < n*n; i++){
  83.         s += (char)((x >> i & 1) + '0');
  84.     }
  85.     return s;
  86. }
  87. int getBits(int r){
  88.     if(r == 0)
  89.         return 0;
  90.     return (1 << (r+1)) - 1;
  91. }
  92. int getBits(int l,int r){
  93.     return getBits(r) ^ getBits(l-1);
  94. }
  95.  
  96. void solve(){
  97.     cin >> n;
  98.  
  99.     int c = 0;
  100.     for(int i = 1; i <= n; i++){
  101.         for(int j = 1; j <= n; j++){
  102.             g[i][j] = (1ll << c);
  103.             c++;
  104.         }
  105.     }
  106.  
  107.     int a[n+2][n+2];
  108.  
  109.     for(int i = 1; i <= n; i++){
  110.         for(int j = 1; j <= n; j++){
  111.             a[i][j] = g[i][j-1] ^ g[i][j+1] ^ g[i+1][j] ^ g[i-1][j];
  112.         }
  113.     }
  114.  
  115.     int x = 0;
  116.  
  117.     for(int i = 1; i < n; i++){
  118.         for(int j = 1; j <= n; j++){
  119.             if(i % 2 == 0 && (j % 4 < 2))
  120.                 continue;
  121.             x ^= a[i][j];
  122.         }
  123.         cout << to_binary(x) << "\n";
  124.     }
  125. //    assert(x == (1 << (n*n)) - 1);
  126. //    return;
  127.  
  128.     int mask = 0;
  129.     for(int mask = 0; mask < (1ll << (n*5)); mask = (mask + 1) | ((1ll << n) - 1)){
  130.         int x = 0;
  131.         c = 0;
  132.         mask |= (1ll << n) - 1;
  133.         mask |= getBits(n+1,2*n-2);
  134.         vector<pair<int,int>> p;
  135.         for(int i = 1; i <= n; i++){
  136.             for(int j = 1; j <= n; j++){
  137.                 if(mask >> c & 1){
  138.                     x ^= a[i][j];
  139.                     p.pb(mp(i,j));
  140.                 }
  141.                 c++;
  142.             }
  143.         }
  144. //        cerr << "mask = " << mask << "\n";
  145.         if((x & ((1ll << (4*n)) - 1)) == (1ll<<(4*n))-1){
  146.             cout << "x = " << to_binary(x) << "\n";
  147.             cout << p.size() << "\n";
  148.             for(auto&[x,y] : p){
  149.                 cout << x << " " << y << "\n";
  150.             }
  151.             cout.flush();
  152.         }
  153.     }
  154.  
  155.     return;
  156. }
  157.  
  158. signed main(){
  159.     ios::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
  160.     init();
  161.     int tests = 1;
  162.     cin >> tests;
  163.     for(int test = 1; test <= tests; test++){
  164. //        cerr << "test = " << test << "\n";
  165.         solve();
  166.     }
  167.     return 0;
  168. }
  169. /**
  170. 1: k,2k,3k,4k ...
  171. 2: k+(k+1),k+2(k+1),k+3(k+1)..., 2k+(k+1), 2k+2(k+1)
  172.  
  173. 1
  174. 6
  175. 100001111111000000000000000000000000
  176. 111000000001011001000000000000000000
  177. 111000111110111000111111000000000000
  178. 111000111110100001000001011001000000
  179. 111000111110100001111110111000111111
  180. x = 111111111111111111111111111111111111
  181. 24
  182. 1 1
  183. 1 2
  184. 1 3
  185. 1 4
  186. 1 5
  187. 1 6
  188. 2 2
  189. 2 3
  190. 2 4
  191. 2 5
  192. 3 1
  193. 3 2
  194. 3 5
  195. 3 6
  196. 4 2
  197. 4 3
  198. 4 4
  199. 4 5
  200. 5 1
  201. 5 2
  202. 5 3
  203. 5 4
  204. 5 5
  205. 5 6
  206.  
  207. **/
  208.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement