willy108

CF 915 G WA 5

Feb 23rd, 2021 (edited)
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.24 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cstring>
  4. #include <cstdlib>
  5. #include <ctime>
  6. #include <cmath>
  7. #include <cassert>
  8. #include <algorithm>
  9. #include <vector>
  10.  
  11. #define max_v 2100000
  12. #define int_max 0x3f3f3f3f
  13. #define cont continue
  14. #define pow_2(n) (1 << (n))
  15. #define ll long long
  16. #define pii pair<int, int>
  17. #define pb push_back
  18. #define mp make_pair
  19. #define lsb(n) ((n)&(-(n)))
  20. #define LC(n) (((n) << 1) + 1)
  21. #define RC(n) (((n) << 1) + 2)
  22. #define add(a, b) (((a)%mod + (b)%mod)%mod)
  23. #define mul(a, b) (((a)%mod * (b)%mod)%mod)
  24. #define init(arr, val) memset(arr, val, sizeof(arr))
  25.  
  26. const ll mod = 1e9 + 7;
  27.  
  28. using namespace std;
  29.  
  30. void setIO(const string& file_name){
  31.     freopen((file_name+".in").c_str(), "r", stdin);
  32.     freopen((file_name+".out").c_str(), "w+", stdout);
  33. }
  34. ll m[max_v], low[max_v], vis[max_v], BIT[max_v], fac[max_v];
  35. ll freq[max_v], p[max_v], n, k, ans[max_v], contr[max_v];
  36. //this'll hurt my brain for sure
  37. //mew(1) is 1
  38. //mew(2) is -1
  39. //mew(3) is -1
  40. //mew(6) is 1
  41. //hopefully this'll work for my brick of a brain
  42.  
  43. ll bin_pow(ll a, ll b){
  44.   ll ans = 1ll;
  45.   for(ll i = 0; pow_2(i) <= b; i++){
  46.     if(b&pow_2(i)){
  47.       ans = mul(ans, a);
  48.     }
  49.     a = mul(a, a);
  50.   }
  51.   return ans;
  52. }
  53.  
  54. ll S(int k, ll A){ return (!k) ? A%mod : S(k - lsb(k), A + BIT[k]);}
  55. void U(int k, ll val){ for(; k <=n; k += lsb(k)) BIT[k] += val;}
  56.  
  57.  
  58. int main(){
  59.     cin.tie(0) -> sync_with_stdio(0);
  60.   low[1] = mod; //1 -> 0 for a better progression
  61.   m[1] = 1;
  62.   //seive for primes, low[i] is the smallest prime that divides i
  63.   for(int i = 2; i<=2e6; i++){
  64.     if(vis[i]) cont;
  65.     for(int j = i; j<=2e6; j += i){
  66.       if(!vis[j]) low[j] = i;
  67.       vis[j] = 1;
  68.     }
  69.   }
  70.   //compute m, or mobius's function
  71.   for(int i = 2; i <=2e6; i++){
  72.     if(low[i] == i) m[i] = -1;
  73.     else if((i/low[i])%low[i] == 0) m[i] = 0;
  74.     else m[i] = m[i/low[i]] * -1;
  75.     //if(i <= 100) cout << i << ' ' << m[i] << '\n';
  76.   }
  77.  
  78.   cin >> n >> k;
  79.   //compute each i^n
  80.   for(int i = 1; i<=k; i++){
  81.     p[i] = bin_pow((ll)i, n);
  82.   }
  83.   ll tot = 0ll;
  84.  
  85.   for(int i = 1; i<=k; i++){
  86.     //ans[i] = ans[i - 1]; //the current answer is similar to the prior one
  87.     /**for(int j = i; j; j /= low[j]){
  88.       ans[i] -= contr[j];  //the only differences are the numbers
  89.       contr[j] = mul(mod + m[j], p[i/j]);   //that evenly divide i
  90.       ans[i] += contr[j];       //thats what this is for
  91.       (ans[i] += mod*2) %= mod;
  92.       assert(i%j == 0);
  93.     **/
  94.     //ans[i] = ans[i - 1]; //the current answer is similar to the prior one
  95.     //cout << "factors of " << i << " :";
  96.     int ind = 0;
  97.     for(int j = i; j; j/=low[j]){
  98.       if((j/low[j])%low[j] != 0){
  99.         fac[ind++] = low[j];
  100.       }
  101.     }
  102.     for(int b = 0; b < pow_2(ind); b++){
  103.       int x = 1;
  104.       for(int j = 0; j<ind; j++) if(b&pow_2(j)) x *= fac[j];
  105.       //cout << " " << x;
  106.       U(x, mod -contr[x]);
  107.       contr[x] = (m[x] * p[i/x])%mod;
  108.       U(x, contr[x]);
  109.     }
  110.     //cout << endl;
  111.     //for(int j = 1; j<=n; j++) ans[i] += contr[j];
  112.     ans[i] += S(n, 0ll);
  113.     ans[i] %= mod;
  114.     //cout << i << ' ' <<  ans[i] << ' ' << (int)(ans[i]^(ll)i)%mod << endl;
  115.     tot += ans[i] ^ (ll)i;
  116.   }
  117.  
  118.   cout << tot%mod << '\n'; //WHY IS IT WA. WHY WHY WHY
  119.   return 0;
  120. }
  121.  
Add Comment
Please, Sign In to add comment