Codeforces Educational Codeforces Round 5 E. Sum of Remainders 数学
E. Sum of Remainders
题目连接:
http://www.codeforces.com/contest/616/problem/E
Description
The only line contains two integers n, m (1 ≤ n, m ≤ 1013) — the parameters of the sum.
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 Input
3 4
Sample Output
4
Hint
题意
给你n,m,让你输出n%1+n%2+n%3+....+n%m
答案需要mod1e9+7
题解:
n%i = n - i * [n/i]
所以 sigma(n%i) = n * m - sigma(i * n/i)
在一段区间内,n/i都是不会变化的,所以利用这一点,我们就可以暴力sqrt去处理就好了
但是有一些区间我们最后并没有处理到,怎么办?
直接再暴力一发去处理就好了
代码
#include<bits/stdc++.h>
using namespace std;
const int mod = 1e9 + 7;
int main()
{
long long n,m;
scanf("%lld%lld",&n,&m);
long long ans = (n%mod)*(m%mod)%mod;
long long S = sqrt(n+5);
long long k = m+1;
for(int i=1;i<=min(n,S);i++)
{
long long r = n / i;
long long l = n / (i+1)+1;
r = min(r,m);
if(l>r)continue;
k = min(k,l);
long long s1 = (l+r);
long long s2 = (r-l+1);
if(s1%2==0)
s1/=2LL;
else
s2/=2LL;
long long K = (s1%mod)*(s2%mod)%mod;
K = (i * K)%mod;
ans-=K;
ans%=mod;
if(ans<0)ans+=mod;
}
for(int i=1;i<k;i++)
{
ans-=(n-n%i);
ans%=mod;
if(ans<mod)ans+=mod;
}
cout<<ans%mod<<endl;
}