Educational Codeforces Round 5 E. Sum of Remainders

E. Sum of Remainders


题目链接:点这里


题意:

求n%1+n%2+n%3+...n%m; 答案对1e9+7取模

数据范围 1<n,m<1e13


题解:

我们来看下取模运算是怎么进行的:  n%i = n-n/i*i

对原式子化解成 n*m - sum(n/i*i)     (1<=i<=m)

然后我们假设n/i~n/j的值是相同的。我们求j的最大值。j=n/(n/j)。

然后就可以把sum(n/i*i)看成若干个等差数列。很容易就能得到答案。

时间复杂度sqrt(n)


代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<sstream>
#include<algorithm>
#include<vector>
#include<bitset>
#include<set>
#include<queue>
#include<stack>
#include<map>
#include<cstdlib>
#include<cmath>
#define PI 2*asin(1.0)
#define LL long long
#define pb push_back
#define pa pair<int,int>
#define clr(a,b) memset(a,b,sizeof(a))
#define lson lr<<1,l,mid
#define rson lr<<1|1,mid+1,r
#define bug(x) printf("%d++++++++++++++++++++%d\n",x,x)
#define key_value ch[ch[root][1]][0]
const LL  MOD = 1000000000+7;
const int N = 5e5+15;
const int maxn = 1e6+1000;
const int letter = 130;
const int INF = 1e17;
const double pi=acos(-1.0);
const double eps=1e-8;
using namespace std;
inline int read()
{
    int x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}
LL n,m;
LL md(LL x){
    return x%MOD;
}
int main(){
    /// n*m-sum(n/i*i)
    cin>>n>>m; /// sum = n%1+n%2+n%3...n%m,sum=sum%MOD;
    LL ans=0,vs=0;
    LL j;
    ans=md(md(n)*md(m));
    for(LL i=1;i<=min(n,m);i++){
        j=n/(n/i);
        ///%2
        if(j>m) j=m;
        LL pc;
        if((i+j)&1) pc=md(md((j-i+1)/2)*md(md(n/i)*md(i+j)));
        else        pc=md(md(j-i+1)*md(md(n/i)*md((i+j)/2)));
        vs=vs+pc;
        vs=md(vs);
        i=j;
    }
    ans=md(ans-vs);
    if(ans<0) ans+=MOD;
    cout << ans << endl;
   





你可能感兴趣的:(数论,codeforces)