Advertisement
Saleh127

CSES 1082 / Number theory sum of div(1 to n)

Mar 8th, 2022
1,241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.03 KB | None | 0 0
  1. /***
  2.  created: 2022-03-08-15.59.43
  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. const ll mod= 1000000007;
  12.  
  13. ll bigmod(ll a,ll c,ll d)
  14. {
  15.  
  16.    ll ans=1;
  17.    a%=d;
  18.    while(c>0)
  19.    {
  20.        if(c&1)
  21.        {
  22.            ans=(ans*a)%d;
  23.        }
  24.        c=c>>1;
  25.  
  26.        a=(a*a)%d;
  27.    }
  28.  
  29.    return ans;
  30.  
  31. }
  32.  
  33. /// bigmod(2,mod-2,mod) = 500000004;
  34.  
  35. ll sum(ll n)
  36. {
  37.     return ((((n%mod)*((n+1)%mod))%mod)*500000004)%mod;
  38. }
  39.  
  40. ll divsum1ton(ll n)
  41. {
  42.     ll i,j,k,l=1,r;
  43.  
  44.  
  45.     ll ans=0;
  46.  
  47.     while(l<=n)
  48.     {
  49.         k=n/l;
  50.         r=n/k;
  51.  
  52.         k%=mod;
  53.  
  54.         ans+=((sum(r)-sum(l-1)%mod)*k)%mod;
  55.  
  56.         ans%=mod;
  57.  
  58.         l=r+1;
  59.     }
  60.  
  61.     ans%=mod;
  62.  
  63.     if(ans<0) ans+=mod;
  64.  
  65.     return ans;
  66. }
  67.  
  68.  
  69. int main()
  70. {
  71.     ios_base::sync_with_stdio(0);
  72.     cin.tie(0);
  73.     cout.tie(0);
  74.  
  75.     ll n;
  76.  
  77.     cin>>n;
  78.  
  79.     cout<<divsum1ton(n)<<nl;
  80.  
  81.     get_lost_idiot;
  82. }
  83.  
  84.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement