F - Sum of Remainders

Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Description

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 Input

Input
3 4
Output
4
Input
4 4
Output
1
Input
1 1
Output

0

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

这个题目的思路是,和为 n * m - sum ( [ n / i ] * i )  ,[ ] 是向下取整,i 从(1--- m)

具体是:

前面的 n*m 肯定就这样了

主要是后面的 :将 [ n / i ] 相同的做一个区间,用求和公式去节省时间

i 从 1 --- sqrt (n) ;

l = n / ( i + 1 ) + 1 , r =  n / i ; // 这就是一个个的区间

比如 n = 20 , m = 20

i=1 -->  l=11, r=20   n / (11---20) 都是等于 1

i=2 -->  l=7, r=10     n / (7---10) 都等于2

i=3 -->  l=r=6

i=4 -->  l=r=5

注意一些特殊情况,看注释

 #include <iostream>
#include <cstdio>
#include <cmath>
using namespace std; #define ll __int64
const ll MOD=1e9+; int main()
{
ll n,m;
scanf("%I64d%I64d",&n,&m);
ll ans=(n%MOD)*(m%MOD)%MOD;
ll temp=,las=m+;//记录哪些数没被减
m=min(n,m);//n 余大于 n 的都等于 n
ll nn=(ll)sqrt(n*1.0);
for (ll i=;i<=nn;i++)
{
ll l = n/(i+)+;
ll r = n/i; r=min(r,m);//可能 r 比 m 大
if (l>r) continue; las=min(las,l); ll s1=l+r , s2 =(r-l+);//这里高斯求和有个坑,要先判断哪个数可以除2,再乘
if (s1%==) s1/=; //直接用公式也不对,会超出ll限制
else s2/=;
s1%=MOD;s2%=MOD;
s1=(s1*s2)%MOD;
s1=s1*i%MOD;
temp=(temp+s1)%MOD;
}
ans=(ans+MOD-temp)%MOD;
for (ll i=;i<las;i++) //剩下的没被减得数,将n余之为0的最大整数减去
{
temp=n/i%MOD*i%MOD;
ans=(ans+MOD-temp)%MOD;
}
printf("%I64d\n",ans); return ;
}

最新文章

  1. CSS中的常用属性
  2. 8个主要的Velocity语法使用说明
  3. (单选后,显示相对应的div)点击免费没有变化,点击收费出现输入框
  4. D3D depth buffer的预览
  5. LA 2678 Subsequence
  6. 156. Binary Tree Upside Down
  7. 从source folder 下将其所有子文件夹的*.* 文件拷贝到 target folder (不拷贝文件夹名仅拷贝文件)
  8. QF——网络之网络请求的几种方式,图片缓存
  9. 在查询用户的权限的时候 使用左外连接 和 access数据库中左外连接
  10. 使用iftop网络流量监控
  11. XML SelectSingleNode的使用 根据节点属性获取该节点
  12. SSIS从理论到实战,再到应用(7)----常用的数据类型转换操作
  13. Cannot find PHPUnit in include path phpstorm
  14. iOS开发从申请开发账号到APP上架的整体流程详解
  15. webstorm 的 .后缀名-tab快捷键
  16. Fiddler的安装与使用
  17. 《面向对象程序设计(java)》第七周学习总结
  18. 梯度下降(Gradient Descent)
  19. Web大前端面试题-Day12
  20. 如何实现@ResponseBody,把Json字符串转换为指定类型

热门文章

  1. How is javascript asynchronous AND single threaded?
  2. 2017.9.5 postgresql加密函数的使用
  3. Centos 7 联想Y430P无线网卡驱动安装 过程参考
  4. mysql开发之---使用游标双层嵌套对总表进行拆分为帖子表和回复表
  5. 10分钟-jQuery-基础选择器
  6. Fog of War小调研
  7. ExtJs的Ext.grid.GridPanel不能选择复制表格中的内容解决方案
  8. ubuntu安装rpm格式软件包
  9. mysql用merge合并表
  10. ASP.NET中的配置文件