Codeforces 616E - Sum of Remainders
来源:互联网 发布:tcp端口号怎么看 编辑:程序博客网 时间:2024/05/22 17:35
616E Sum of Remainders
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
思路:
1.对mod的理解与转化: n%mod = n - n/mod*mod;
现在答案就变成了 n*m - Σ(1->m)(n/i*i),乍一看认为没什么优化,还是要求后面的从1 到m的Σ(n/i*i);这就是优化。现在就需要用到对整除的理解的~~ 对于n/x = n/i;这样的x的区间边界时什么? 经分析知:[i,n/(n/i)];却这里得到一个很有趣的结论,对于n/(n/(n/i)) = n/i;2.然后就是mod运算了,注意在同一个n/i下的区间内数的和(l+r)/2时,为了不爆范围,先用偶数除以二,之后在相乘;
3.举个例子对下面程序的运行:
比如n = 15,m = 13;
则[1,1] ==>是第一个计算的,对于任意的x ,l <= x <= r,n/x = n/i;
[2,2],[3,3],[4,5],[6,7],[8,13];//区间距离越来越大;这里是优化的对象;
之后 l = r,再++l;就是对于每一个n/i相等的区间,直接利用等差数列求完,下次求解的时候,l = r+1;即区间不会覆盖;所以对于1e13范围内的数据,还是可以在436ms内求解完毕;如上面最后一次是8~13
#include<bits/stdc++.h>using namespace std;typedef long long ll;const int mod = 1e9+7;int add(ll a, ll b) { return (a+b)%mod; }int sub(ll a, ll b) { return ((a-b)%mod + mod)%mod; }int mult(ll a, ll b) { return ((a%mod) * (b%mod))%mod; }int main(){ ll i,n,m; cin>>n>>m; int ans = mult(n,m),sum = 0; m = min(n,m); for(i = 1;i <= m;i++){// l = i,r = n/(n/i);while:l <= x <= r ;n/x = n/i; ll r = n/(n/i); r = min(r,m); ll sm = i + r,nm = r - i + 1; if(sm&1) sm = mult(sm,nm/2); else sm = mult(sm/2,nm); sum = add(sum,mult(sm,n/i)); i = r; } ans = sub(ans,sum); cout<<ans;}
- Codeforces 616E Sum of Remainders
- codeforces 616E - Sum of Remainders
- Codeforces 616E - Sum of Remainders
- 【数论】【分块】【乱搞】codeforces 616E Sum of Remainders
- Codeforces 616E Sum of Remainders 【数学分块】
- CodeForces 616E Sum of Remainders(数论)
- codeforces 616E Sum of Remainders 数学公式转化
- Educational Codeforces Round 5 E. Sum of Remainders 数论
- Educational Codeforces Round 5 E. Sum of Remainders(数学)
- Codeforces educational round 5. E Sum of Remainders 数论 分块
- Educational Codeforces Round 5 E. Sum of Remainders
- Educational Codeforces Round 5-E. Sum of Remainders
- codeforces 616E Sum of Remainders(数学公式转化 较难)
- CodeForces - 616E Sum of Remainders (数论)大数取余求和 好题
- E. Sum of Remainders (数学)★
- Educational Codeforces Round 5 E. Sum of Remainders 思维 数学转换
- CF-edu#5 E - Sum of Remainders -数学-枚举
- Codeforces 616E Sum Of Reminders
- 发表时间分别显示
- Java中getResourceAsStream的用法
- FEDERATED存储引擎的使用,如何开启FEDERATED,FEDERATED Support NO?
- Virtual Studio中设置异常触发 LoaderLock
- 计算脸部部位在坐标系中的位置
- Codeforces 616E - Sum of Remainders
- 内存消耗测试
- #Android源码#自定义UI控件
- bzoj2127: happiness
- MFC简单皮肤颜色绘制(SetDialogBkColor不再被支持,看 WM_CTLCOLOR消息如何百变控件颜色)
- Android 自定义对话框(控制大小、位置)
- 即时通讯收发语音,兼容安卓wav和amr格式互转
- spring mvc redirect/forward 表单重复提交
- 性能测试之互联网应用需求建模分析