Topic links
Give two number n, M. to find the value of n%1+n%2+.......+n%m.
First, n%i = N-n/i*i, then the original conversion is N*m-sigma (I:1 to M) (N/i*i).
Then we can find 1/4 = 2/4 = 3/4 = 0, 4/4 = 5/4 = 6/4 = 7/4 = 1. So you can divide these results into chunks and calculate the results by block.
Pay attention to avoid longlong in the calculation process at all times.
#include <iostream>#include<vector>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<map>#include<Set>#include<string>#include<queue>#include<stack>#include<bitset>using namespacestd;#definePB (x) push_back (x)#definell Long Long#defineMK (x, y) make_pair (x, y)#defineLson L, M, rt<<1#defineMem (a) memset (a, 0, sizeof (a))#defineRson m+1, R, rt<<1|1#defineMem1 (a) memset (a,-1, sizeof (a))#defineMEM2 (a) memset (a, 0x3f, sizeof (a))#defineRep (i, N, a) for (int i = A; i<n; i++)#defineFi first#defineSe Secondtypedef pair<int,int>PLL;Const DoublePI = ACOs (-1.0);Const DoubleEPS = 1e-8;Constll mod = 1e9+7;Const intINF =1061109567;Const intdir[][2] = { {-1,0}, {1,0}, {0, -1}, {0,1}};vector<pair<ll, ll> >V;ll Getsum (ll A, ll b) {if((b-a)%2==0) { return((B-A)/2%MOD) * ((a+b+1)%mod)%MoD; } return((a+b+1)/2%MOD) * ((b-a)%mod)%MoD;}intMain () {ll n, m; CIN>>n>>m; for(LL i =1; i*i<=n; i++) {ll tmp= n/i; V.PB (MK (i, TMP)); if(TMP! =i) {V.PB (MK (TMP, i)); }} V.PB (Mk (0,0)); Sort (V.begin (), V.end ()); ll ans= (n%mod) * (m%mod)%MoD; inti; for(i =1; I<v.size () &&m>=v[i].fi; i++) {ans= (Ans-getsum (v[i-1].fi, v[i].fi)%mod* (v[i].se%mod)%mod+mod)%MoD; } if(m<N) Ans= (Ans-getsum (v[i-1].fi, M)%mod* (v[i].se%mod)%mod+mod)%MoD; cout<<ans<<Endl; return 0;}
Codeforces 616E. Sum of Remainders Math