codeforces 616E Sum of Remainders 数学公式转化
2017-05-18 10:05
429 查看
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,除完后再求余
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; }
相关文章推荐
- codeforces 616E Sum of Remainders(数学公式转化 较难)
- 转化字符串表达式为数学公式并算出结果
- Codeforces 839D Winter is here【容斥原理+数学公式】
- 转化字符串表达式为数学公式并算出结果
- 转化字符串表达式为数学公式并算出结果
- Codeforces 534C Polycarpus' Dice(数学,公式推导)
- codeforces Buttons 数学公式构建
- 转化字符串表达式为数学公式并算出结果
- POJ1019 NumberSequence 【数学公式转化题】
- CodeForces 630Q Pyramids(数学公式)
- CodeForces 233B Non-square Equation(数学问题方程转化)
- Codeforces 758C Unfair Poll 数学推导,公式
- Codeforces 513C Second price auction 数学公式求期望
- 5.3学习内容 放射获取泛型类型,for与数学公式的转化示范
- 转化字符串表达式为数学公式并算出结果—实用类
- CodeForces 635C XOR Equation 数学 公式
- Codeforces 615D Multipliers(数学推公式)
- 【矩阵快速幂 】Codeforces 450B - Jzzhu and Sequences (公式转化)
- codeforces 650 C. Watchmen(数学公式)
- CodeForces 659 D. Bicycle Race(计算几何 + 数学公式)