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的更多相关文章

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

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

  2. codeforces 616E. Sum of Remainders 数学

    题目链接 给两个数n, m. 求n%1+n%2+.......+n%m的值. 首先, n%i = n-n/i*i, 那么原式转化为n*m-sigma(i:1 to m)(n/i*i). 然后我们可以发 ...

  3. 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 ...

  4. Sum of Remainders(数学题)

    F - Sum of Remainders Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I ...

  5. Codeforces 85D Sum of Medians(线段树)

    题目链接:Codeforces 85D - Sum of Medians 题目大意:N个操作,add x:向集合中加入x:del x:删除集合中的x:sum:将集合排序后,将集合中全部下标i % 5 ...

  6. Educational Codeforces Round 5 E. Sum of Remainders (思维题)

    题目链接:http://codeforces.com/problemset/problem/616/E 题意很简单就不说了. 因为n % x = n - n / x * x 所以答案就等于 n * m ...

  7. Codeforces 616 E Sum of Remainders

    Discription Calculate the value of the sum: n mod 1 + n mod 2 + n mod 3 + ... + n mod m. As the resu ...

  8. Codeforces 1442D - Sum(找性质+分治+背包)

    Codeforces 题面传送门 & 洛谷题面传送门 智商掉线/ll 本来以为是个奇怪的反悔贪心,然后便一直往反悔贪心的方向想就没想出来,看了题解才发现是个 nb 结论题. Conclusio ...

  9. Codeforces 1303G - Sum of Prefix Sums(李超线段树+点分治)

    Codeforces 题面传送门 & 洛谷题面传送门 个人感觉这题称不上毒瘤. 首先看到选一条路径之类的字眼可以轻松想到点分治,也就是我们每次取原树的重心 \(r\) 并将路径分为经过重心和不 ...

随机推荐

  1. QUiLoader 动态加载.ui文件

    动态加载UI文件是指,用 Qt Designer 通过拖拽的方式生产.ui 文件.不用 uic工具把.ui 文件变成等价的 c++代码,而是在程序运行过程中需要用到UI文件时,用 QUiLoader ...

  2. js整理常用方法

    javascript对象合并或追加属性的方法 function objMerger(obj1, obj2){ for(var r in obj2){ //eval("obj1."+ ...

  3. scrollLeft、offsetLeft、clientLeft、clientHeight详解

    offsetLeft:Html元素相对于自己的offsetParent元素的位置 scrollLeft:返回和设置当前横向滚动务的坐标值 scrollLeft.offsetLeft.clientLef ...

  4. SQL Server I/O 问题的诊断分

    一. SQL Server 什么时候和磁盘打交道: 1. SQL 需要访问的数据没有在Buffer pool中,第一次访问时需要将数据所在的页面从数据文件中读取到内存中.(只读) 2. 在insert ...

  5. JavaScript总结1

    一.JavaScript变量类型.声明.作用域 1.1 数字 number 小数和整数都叫number,以0x或0X开头的表示十六进制.当无穷大时,用Infinity表示(试试 9/0),其他非数字用 ...

  6. C#导入导出Excel表的数据

    一:C#导入导出EXCEL文件的类 代码如下: 首先将Microsoft Excel 14.0 Object Library 引用导入 using System; using System.Data; ...

  7. 使用Keil建立工程和烧录到89C52板上

    又开始学习C51了,不清楚能坚持多久,之前学过一段时间,学完P1口就没再学了,之前学的都忘了. 1. 使用Keil 进行建立工程:打开Keil 4. 加载C文件进工程里面 然后展开"Sour ...

  8. Python函数中参数* 和 ** 的区别

    * 函数接收参数为元组 例如 def myfun(*args): #相当于 def myfun(1,2,3)    ==> args 就相当于(1,2,3) for a in args: pri ...

  9. Magento中直接使用SQL语句

    原理: magento是基于Zend Framework的,所以底层用的还是zend的zend db 在文件app/code/core/Mage/Catalog/model/Resource/Eav ...

  10. JSON数据理解

    话说JSON数据平常用的确实挺多的,但是基本上只知道怎么用,对其一些细节并没有整理过,今儿趁着下午有点空,坐下来,学习整理下,并分享出来. 对于JSON,首先它只是一种数据格式,并非一种语言,虽然和j ...