Advertisement
Saleh127

SPOJ LCMSUM / Number theory

Feb 18th, 2022
777
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.99 KB | None | 0 0
  1. /***
  2.  created: 2022-02-18-21.13.25
  3. ***/
  4.  
  5. #include <bits/stdc++.h>
  6. using namespace std;
  7. #define ll long long
  8. #define test int tt; cin>>tt; for(int cs=1;cs<=tt;cs++)
  9. #define get_lost_idiot return 0
  10. #define nl '\n'
  11.  
  12. ll phi[1000005];
  13. ll ans[1000005];
  14.  
  15. void eulerphi()
  16. {
  17.     for(ll i=0; i<=1000005; i++)
  18.     {
  19.         phi[i]=i;
  20.     }
  21.  
  22.     for(ll i=2; i<=1000005; i++)
  23.     {
  24.         if(phi[i]==i)
  25.         {
  26.             for(ll j=i; j<=1000005; j+=i)
  27.             {
  28.                 phi[j]-= phi[j]/i;
  29.             }
  30.         }
  31.     }
  32.     for(ll i=1; i<=1000005; i++)
  33.     {
  34.         for(ll j=i; j<=1000005; j+=i)
  35.         {
  36.             ans[j]+=(i*phi[i]);
  37.         }
  38.     }
  39.  
  40. }
  41.  
  42. int main()
  43. {
  44.     ios_base::sync_with_stdio(0);
  45.     cin.tie(0);
  46.     cout.tie(0);
  47.  
  48.  
  49.     /// ∑LCM(i, n) = ((∑(d * ETF(d)) + 1) * n) / 2
  50.  
  51.     eulerphi();
  52.  
  53.     test
  54.     {
  55.          ll n,m,i,k;
  56.  
  57.          cin>>n;
  58.  
  59.          m=(ans[n]+1)*n;
  60.  
  61.          cout<<m/2<<nl;
  62.     }
  63.  
  64.  
  65.     get_lost_idiot;
  66. }
  67.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement