orazma

i.cpp

May 23rd, 2026
28
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.49 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define f first
  3. #define s second
  4. #define pb push_back
  5. #define mp make_pair
  6. #define ll long long
  7.  
  8. #define IOS ios::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
  9.  
  10. using namespace std;
  11. const int INF = 1e9 + 1;
  12. const int MOD = 1e9 + 7;
  13. const int MAXN = 1e6 + 10;
  14. int cnt[MAXN], a[MAXN], s[MAXN], kk[MAXN];
  15. int min_div[MAXN + 10];
  16. ll dp[MAXN + 10], phi[MAXN + 10];
  17. void solve(){
  18.  
  19.     ll n;
  20.     cin >> n;
  21.  
  22.     cout << 1LL * n * (n - 1) / 2 - dp[n] << "\n";
  23.  
  24. }
  25.  
  26. int main() {
  27.     IOS;
  28.  
  29.     for (int i = 2; i <= MAXN; i++){
  30.         if (min_div[i] == 0){
  31.             min_div[i] = i;
  32.             for (ll j = 1LL * i * i; j <= MAXN; j += i){
  33.                 if (min_div[j] == 0){
  34.                     min_div[j] = i;
  35.                 }
  36.             }
  37.         }
  38.     }
  39.     phi[1] = 0;
  40.     dp[1] = 0;
  41.     for (int i = 2; i <= MAXN; i++){
  42.         if (min_div[i] == i){
  43.             phi[i] = i - 1;
  44.         }
  45.         else{
  46.             int x = i;
  47.             int y = 1;
  48.             while(x % min_div[i] == 0){
  49.                 x /= min_div[i];
  50.                 y *= min_div[i];
  51.             }
  52.             if (x == 1){
  53.                 phi[i] = i - i / min_div[i];
  54.             }
  55.             else{
  56.                 phi[i] = phi[x] * phi[y];
  57.             }
  58.         }
  59.         dp[i] = dp[i - 1] + phi[i];
  60.     }
  61.  
  62.     int t = 1;
  63.    // cin >> t;
  64.  
  65.     while(t--){
  66.         solve();
  67.     }
  68.     return 0;
  69. }
  70.  
  71.  
  72. //phi[120] = phi[2 ^ 3] * phi[3] * phi[5]
  73.  
Advertisement
Add Comment
Please, Sign In to add comment