Showing posts with label Mobius Function. Show all posts
Showing posts with label Mobius Function. Show all posts

Saturday, April 4, 2020

[Codeforces] F. Make It One

Author            : Dipu Kumar Mohanto 
                    CSE, Batch - 6
                    BRUR.
Problem Statement : F. Make It One
Source            : Codeforces
Category          : Number Theroy
Algorithm         : Mobius Function 
Verdict           : Accepted

  1. #include "bits/stdc++.h"  
  2.   
  3. #define ll          long long int  
  4. #define endl        '\n'  
  5.   
  6. using namespace std;  
  7.   
  8. static const int maxn = 3e5 + 5;  
  9. static const long long mod = 1e9 + 7;  
  10.   
  11. int fre[maxn];  
  12. int divi[maxn];  
  13. bool isprime[maxn];  
  14. vector <int> prime;  
  15.   
  16. void seive()  
  17. {  
  18.       for (int i = 2; i < maxn; i++) isprime[i] = true;  
  19.       for (int i = 2; i < maxn; i++)  
  20.       {  
  21.             if (isprime[i]) prime.push_back(i);  
  22.             for (int j = 1; 1LL * i * j < maxn; j++)  
  23.             {  
  24.                   isprime[i * j] = false;  
  25.                   divi[i] += fre[i * j];  
  26.             }  
  27.       }  
  28. }  
  29.   
  30. int mobius[maxn];  
  31.   
  32. void calc_mobius()  
  33. {  
  34.       for (int i = 1; i < maxn; i++) mobius[i] = 1;  
  35.       for (int p : prime)  
  36.       {  
  37.             if (1LL * p * p > maxn) break;  
  38.             int x = p * p;  
  39.             for (int j = x; j < maxn; j += x) mobius[j] = 0;  
  40.       }  
  41.       for (int p : prime)  
  42.       {  
  43.             for (int j = p; j < maxn; j += p) mobius[j] *= -1;  
  44.       }  
  45. }  
  46.   
  47. long long binaryExpo(long long a, long long p, long long m)  
  48. {  
  49.       if (p == 0) return 1 % m;  
  50.       if (p == 1) return a % m;  
  51.       if (p & 1) return (a % m * binaryExpo(a, p - 1, m) % m) % m;  
  52.       long long ret = binaryExpo(a, p / 2, m);  
  53.       return (ret % m * ret % m) % m;  
  54. }  
  55.   
  56. long long mod_inv(long long a, long long m)  
  57. {  
  58.       return binaryExpo(a, m - 2, m);  
  59. }  
  60.   
  61. long long fact[maxn];  
  62. long long inv[maxn];  
  63.   
  64. void calc_fact()  
  65. {  
  66.       fact[0] = 1;  
  67.       inv[0] = 1;  
  68.       for (int i = 1; i < maxn; i++)  
  69.       {  
  70.             fact[i] = (1LL * i * fact[i - 1]) % mod;  
  71.             inv[i] = mod_inv(fact[i], mod);  
  72.       }  
  73. }  
  74.   
  75. long long nCr(int n, int r)  
  76. {  
  77.       if (n < r) return 0;  
  78.       if (n == r) return 1;  
  79.       long long nom = fact[n];  
  80.       long long dem = (fact[r] * fact[n - r]) % mod;  
  81.       long long ncr = (nom * mod_inv(dem, mod)) % mod;  
  82.       return ncr;  
  83. }  
  84.   
  85. ll count_coprime(int r)  
  86. {  
  87.       ll coprime = 0;  
  88.       for (int d = 1; d < maxn; d++)  
  89.       {  
  90.             ll ncr = nCr(divi[d], r);  
  91.             coprime += (mobius[d] * ncr + mod) % mod;  
  92.             coprime %= mod;  
  93.       }  
  94.       return coprime;  
  95. }  
  96.   
  97. signed main()  
  98. {  
  99.       ios_base::sync_with_stdio(false);  
  100.       cin.tie(nullptr);  
  101.   
  102.       #ifndef ONLINE_JUDGE  
  103.             freopen("in.txt", "r", stdin);  
  104.       #endif // ONLINE_JUDGE  
  105.   
  106.   
  107.       int n;  
  108.       cin >> n;  
  109.       for (int i = 1; i <= n; i++)  
  110.       {  
  111.             int x;  
  112.             cin >> x;  
  113.             if (x == 1) return (cout << 1), 0;  
  114.             fre[x]++;  
  115.       }  
  116.       divi[1] = n;  
  117.       seive();  
  118.       calc_mobius();  
  119.       calc_fact();  
  120.       for (int r = 2; r < 10; r++)  
  121.       {  
  122.             ll coprime = count_coprime(r);  
  123.             if (coprime > 0) return (cout << r << endl), 0;  
  124.       }  
  125.       cout << -1 << endl;  
  126. }  
  127. �  

[Codeforces] E. Mike and Foam

Author            : Dipu Kumar Mohanto 
                    CSE, Batch - 6
                    BRUR.
Problem Statement : E. Mike and Foam
Source            : Codeforces
Category          : Number Theroy
Algorithm         : Mobius Function 
Verdict           : Accepted

  1. #include "bits/stdc++.h"  
  2.   
  3. using namespace std;  
  4.   
  5. static const int maxn = 5e5 + 6;  
  6.   
  7. int sp[maxn];  
  8. vector <int> divisor[maxn];  
  9.   
  10. void seive()  
  11. {  
  12.       for (int i = 2; i < maxn; i++) sp[i] = i;  
  13.       for (int i = 2; i < maxn; i++)  
  14.       {  
  15.             for (int j = 1; 1LL * i * j < maxn; j++)  
  16.             {  
  17.                   sp[i * j] = min(sp[i * j], sp[i]);  
  18.                   divisor[i * j].push_back(i);  
  19.             }  
  20.       }  
  21. }  
  22.   
  23. signed main()  
  24. {  
  25.       ios_base::sync_with_stdio(false);  
  26.       cin.tie(nullptr);  
  27.   
  28.       #ifndef ONLINE_JUDGE  
  29.             freopen("in.txt", "r", stdin);  
  30.       #endif // ONLINE_JUDGE  
  31.   
  32.       seive();  
  33.       int n, q;  
  34.       cin >> n >> q;  
  35.       vector <int> arr(n + 1);  
  36.       for (int i = 1; i <= n; i++) cin >> arr[i];  
  37.       vector <int> in_self(n + 1);  
  38.       int in_self_cnt = 0;  
  39.       vector <int> divi(maxn);  
  40.       auto get = [&](vector <int> &vec)  
  41.       {  
  42.             int n = vec.size();  
  43.             long long coprime = 0;  
  44.             for (int mask = 0; mask < (1 << n); mask++)  
  45.             {  
  46.                   long long d = 1;  
  47.                   int bitCnt = 0;  
  48.                   for (int i = 0; i < n; i++)  
  49.                   {  
  50.                         if ((mask >> i) & 1)  
  51.                         {  
  52.                               d *= vec[i];  
  53.                               bitCnt++;  
  54.                         }  
  55.                   }  
  56.                   if (bitCnt & 1) coprime += divi[d];  
  57.                   else coprime -= divi[d];  
  58.             }  
  59.             return coprime;  
  60.       };  
  61.       long long ans = 0;  
  62.       while (q--)  
  63.       {  
  64.             int pos;  
  65.             cin >> pos;  
  66.             vector <int> prime_factors;  
  67.             int num = arr[pos];  
  68.             while (num > 1)  
  69.             {  
  70.                   int x = sp[num];  
  71.                   prime_factors.push_back(x);  
  72.                   while (num % x == 0) num /= x;  
  73.             }  
  74.             sort(prime_factors.begin(), prime_factors.end());  
  75.             num = arr[pos];  
  76.             if (in_self[pos])  
  77.             {  
  78.                   for (int d : divisor[num]) divi[d]--;  
  79.                   in_self[pos] = 0;  
  80.                   in_self_cnt--;  
  81.                   ans -= in_self_cnt - get(prime_factors);  
  82.             }  
  83.             else  
  84.             {  
  85.                   ans += in_self_cnt - get(prime_factors);  
  86.                   for (int d : divisor[num]) divi[d]++;  
  87.                   in_self[pos] = 1;  
  88.                   in_self_cnt++;  
  89.             }  
  90.             cout << ans << '\n';  
  91.       }  
  92. }