danielvitor23

Fun with Stones

Oct 12th, 2022
1,283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.52 KB | Source Code | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. typedef pair<int64_t, int64_t> ll;
  5.  
  6. const int64_t MOD = 1e9+7;
  7.  
  8. int64_t extended_gcd(int64_t a, int64_t b, int64_t &x, int64_t &y) {
  9.     if (b == 0) {
  10.         x = 1; y = 0;
  11.         return a;
  12.     }
  13.     int64_t g = extended_gcd(b, a%b, y, x);
  14.     y -= x*(a/b);
  15.     return g;
  16. }
  17.  
  18. int64_t inv_mod(int64_t a, int64_t m = MOD) {
  19.   int64_t x, y;
  20.   extended_gcd(a, m, x, y);
  21.   return (x%m + m)%m;
  22. }
  23.  
  24. string toBinary(int64_t x) {
  25.   string a;
  26.   for (int i = 0; i < 30; ++i) {
  27.     if (x & (1 << i)) {
  28.       a = '1' + a;
  29.     } else {
  30.       a = '0' + a;
  31.     }
  32.   }
  33.   return a;
  34. }
  35.  
  36. int mxSz;
  37. int l1, l2, l3;
  38. int r1, r2, r3;
  39. string s1, s2, s3;
  40. string s11, s22, s33;
  41.  
  42. ll dp[30][2][2][2][2][2][2][2];
  43. bool used[30][2][2][2][2][2][2][2];
  44.  
  45. ll solve(int idx, bool xxor, bool top1, bool top2, bool top3, bool bot1, bool bot2, bool bot3) {
  46.   if (idx == 30) {
  47.     return xxor ? ll{0, 1} : ll{1, 0};
  48.   }
  49.  
  50.   if (used[idx][xxor][top1][top2][top3][bot1][bot2][bot3]) {
  51.     return dp[idx][xxor][top1][top2][top3][bot1][bot2][bot3];
  52.   }
  53.  
  54.   used[idx][xxor][top1][top2][top3][bot1][bot2][bot3] = true;
  55.  
  56.   ll ans{0, 0};
  57.  
  58.   int mx1 = top1 ? 1 : s1[idx]-'0';
  59.   int mn1 = bot1 ? 0 : s11[idx]-'0';
  60.   for (int i = mn1; i <= mx1; ++i) {
  61.     int mx2 = top2 ? 1 : s2[idx]-'0';
  62.     int mn2 = bot2 ? 0 : s22[idx]-'0';
  63.     for (int j = mn2; j <= mx2; ++j) {
  64.       int mx3 = top3 ? 1 : s3[idx]-'0';
  65.       int mn3 = bot3 ? 0 : s33[idx]-'0';
  66.       for (int k = mn3; k <= mx3; ++k) {
  67.         // cout << i << ' ' << j << ' ' << k << '\n';
  68.         ll res = solve(idx + 1, xxor | (i xor j xor k), top1 | (i != mx1), top2 | (j != mx2), top3 | (k != mx3), bot1 | (i != mn1), bot2 | (j != mn2), bot3 | (k != mn3));
  69.         ans.first = (ans.first + res.first) % MOD;
  70.         ans.second = (ans.second + res.second) % MOD;
  71.       }
  72.     }
  73.   }
  74.  
  75.   return dp[idx][xxor][top1][top2][top3][bot1][bot2][bot3] = ans;
  76. }
  77.  
  78. int main() {
  79.   cin.tie(0)->sync_with_stdio(0);
  80.  
  81.   cin >> l1 >> r1 >> l2 >> r2 >> l3 >> r3;
  82.  
  83.   s1 = toBinary(r1);
  84.   s2 = toBinary(r2);
  85.   s3 = toBinary(r3);
  86.  
  87.   s11 = toBinary(l1);
  88.   s22 = toBinary(l2);
  89.   s33 = toBinary(l3);
  90.  
  91.   // cout << s1 << '\n';
  92.   // cout << s2 << '\n';
  93.   // cout << s3 << '\n';
  94.   // cout << s11 << '\n';
  95.   // cout << s22 << '\n';
  96.   // cout << s33 << '\n';
  97.  
  98.   ll ans = solve(0, 0, 0, 0, 0, 0, 0, 0);
  99.  
  100.   // cout << ans.first << ' ' << ans.second << '\n';
  101.  
  102.   int64_t tot = (ans.first + ans.second) % MOD;
  103.  
  104.   int64_t res = (ans.second * inv_mod(tot, MOD)) % MOD;
  105.  
  106.   cout << res << '\n';
  107.  
  108. }
Advertisement
Add Comment
Please, Sign In to add comment