codeforces 616E Sum of Remainders (数论,找规律)

E. Sum of Remainders
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Calculate the value of the sum: n mod 1 + n mod 2 + n mod 3 + ... + n mod m. As the result can be very large, you should print the value modulo 109 + 7 (the remainder when divided by 109 + 7).

The modulo operator a mod b stands for the remainder after dividing a by b. For example 10 mod 3 = 1.

Input

The only line contains two integers n, m (1 ≤ n, m ≤ 1013) — the parameters of the sum.

Output

Print integer s — the value of the required sum modulo 109 + 7.

Sample test(s)
input
3 4
output
4
input
4 4
output
1
input
1 1
output
0

 题意很好理解,最后求和的时候找找规律就能过;

AC代码:

#include <bits/stdc++.h>
using namespace std;
const int mod=1e9+7;
int main()
{
    long long n,m;
    cin>>n>>m;
    long long ans=0;
    ans=(n%mod)*(m%mod)%mod;
    if(m>=n){m=n;}
    long long fx,fy,fz,pre=m;
    long long s=0;
    while(pre>1)
    {
        fy=pre;
        fz=n/pre;
        fx=n/(fz+1);
        long long r;
        if((fy-fx)%2==0)r=((fx+fy+1)%mod)*(((fy-fx)/2)%mod);
        else r=((fx+fy+1)/2)%mod*((fy-fx)%mod);
        s+=(r%mod)*fz%mod;
        s%=mod;
        pre=fx;
    }
    s+=n;
    s%=mod;
    cout<<(ans-s+mod)%mod<<endl;
    return 0;
}
 
原文地址:https://www.cnblogs.com/zhangchengc919/p/5155818.html