codeforces 616E Sum of Remainders 数学公式转化

来源:互联网 发布:ubuntu kylin 双显卡 编辑:程序博客网 时间:2024/06/06 04:33

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.

Example
Input
3 4
Output
4
Input
4 4
Output
1
Input
1 1
Output
0

题意

给你两个数n,m
问你n % 1 + n % 2 + … + n% m为几

公式化简 n%i=n-n/i*i 那么就要求 n * m - sum(n/i * i) (i从1到m)

由于 当到达i的时候 从i到 n/(n/i) 的 n/i 的值是一样的 因为如果说 n/i 得到了一个因子的下界,那么n/这个因子。则是这个因子的上界。那么就可以那个求和公式求每一段了。

求得过程有些坑点需要注意,首先是注意m的大小,m的大小可能位于n的两个因子之间
然后是,求和公式那里也需要先求下余,再算乘法,这样就需要注意因为有个除2,所以要判断用哪一项能整除2,除完后再求余

#include <bits/stdc++.h>using namespace std;typedef long long ll;const ll mod=1e9+7;int main(){    long long n,m;    cin>>n>>m;    ll all = (n%mod)*(m%mod);    all%=mod;    long long res=0;    ll t=min(n,m);    for(ll i=1;i<=t;i++)    {        ll l=i;        ll r=min((n/(n/i)),m);        long long num=r-l+1;        long long sw=(l+r);        if(sw%2==0) sw/=2;        else num/=2;        num%=mod,sw%=mod;        ll sum=(num%mod)*(sw%mod);//这里不取模居然不行。。好奇怪        sum%=mod;        sum*=(n/i);        sum%=mod;        res+=sum;        res%=mod;        i=r;    }    res%=mod;   cout<<(all-res+mod)%mod<<endl;}
原创粉丝点击