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
  1. 3 4
Output
  1. 4
Input
  1. 4 4
Output
  1. 1
Input
  1. 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

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

  1. #include <iostream>
  2. #include <cstdio>
  3. #include <cmath>
  4. using namespace std;
  5.  
  6. #define ll __int64
  7. const ll MOD=1e9+;
  8.  
  9. int main()
  10. {
  11. ll n,m;
  12. scanf("%I64d%I64d",&n,&m);
  13. ll ans=(n%MOD)*(m%MOD)%MOD;
  14. ll temp=,las=m+;//记录哪些数没被减
  15. m=min(n,m);//n 余大于 n 的都等于 n
  16. ll nn=(ll)sqrt(n*1.0);
  17. for (ll i=;i<=nn;i++)
  18. {
  19. ll l = n/(i+)+;
  20. ll r = n/i;
  21.  
  22. r=min(r,m);//可能 r 比 m 大
  23. if (l>r) continue;
  24.  
  25. las=min(las,l);
  26.  
  27. ll s1=l+r , s2 =(r-l+);//这里高斯求和有个坑,要先判断哪个数可以除2,再乘
  28. if (s1%==) s1/=; //直接用公式也不对,会超出ll限制
  29. else s2/=;
  30. s1%=MOD;s2%=MOD;
  31. s1=(s1*s2)%MOD;
  32. s1=s1*i%MOD;
  33. temp=(temp+s1)%MOD;
  34. }
  35. ans=(ans+MOD-temp)%MOD;
  36. for (ll i=;i<las;i++) //剩下的没被减得数,将n余之为0的最大整数减去
  37. {
  38. temp=n/i%MOD*i%MOD;
  39. ans=(ans+MOD-temp)%MOD;
  40. }
  41. printf("%I64d\n",ans);
  42.  
  43. return ;
  44. }

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 Educational Codeforces Round 5 E. Sum of Remainders 数学

    E. Sum of Remainders 题目连接: http://www.codeforces.com/contest/616/problem/E Description The only line ...

  3. Codeforces 616E - Sum of Remainders

    616E Sum of Remainders Calculate the value of the sum: n mod 1 + n mod 2 + n mod 3 + - + n mod m. As ...

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

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

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

  6. 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). 然后我们可以发 ...

  7. HDU-2058-The sum problem(数学题技巧型)

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2058 思路: 这题的n,m都很大,很显然直接暴力,会超时,那就不能全部都找了,利用等差数列求和公式, ...

  8. hdu 2058 The sum problem(数学题)

    一个数学问题:copy了别人的博客 #include<cstdio> #include<cstdlib> #include<cmath> int main() { ...

  9. 杭电ACM分类

    杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze ...

随机推荐

  1. C++ 如何得到当前进程所占用的内存呢?【转】

    使用SDK的PSAPI (Process Status Helper)中的BOOL GetProcessMemoryInfo( HANDLE Process, PPROCESS_MEMORY_COUN ...

  2. DataSet之增删改查操作(DataGridView绑定)

    DataSet数据集,数据缓存在客户端内存中,支持断开式连接.DataGridView控件绑定DataSet时,它自动的改变的DS的行的状态,而且在做增删改查的时候,可以借助SqlCommandBui ...

  3. [Algorithms] Classify Mystery Items with the K-Nearest Neighbors Algorithm in JavaScript

    The k-nearest neighbors algorithm is used for classification of unknown items and involves calculati ...

  4. 小tip: base64:URL背景图片与web页面性能优化

    转自:http://www.zhangxinxu.com/wordpress/?p=2341 一.base64百科 Base64是网络上最常见的用于传输8Bit字节代码的编码方式之一,可用于在HTTP ...

  5. hibernate学习系列-----(2)hibernate核心接口和工作机制

    在上一篇文章hibernate学习系列-----(1)开发环境搭建中,大致总结了hibernate的开发环境的搭建步骤,今天,我们继续了解有关hibernate的知识,先说说这篇文章的主要内容吧: C ...

  6. 倍福TwinCAT(贝福Beckhoff)基础教程 松下伺服驱动器报错 88怎么办

    请确认在TWINCAT在线模式下,把Drive的Modes of operation改为8       更多教学视频和资料下载,欢迎关注以下信息: 我的优酷空间: http://i.youku.com ...

  7. 熟悉jauery库中的构造函数 jQuery()

    如果调用构造函数 jQuery() 时传入的参数不同,创建 jQuery 对象的逻辑也会随之不同.实际上构造函数 jQuery() 有 7 种用法,下面一一举例 1.jQuery( selector ...

  8. angularjs与server交互

    真正的应用须要和真实的server进行交互,移动应用和新兴的Chrome桌面应用可能是个例外,可是对于此外的全部应用来说,不管你是想把数据持久化到云端.还是须要与其它用户进行实时交互.都须要让应用与s ...

  9. 谈一谈AGP

    typedef enum D3DPOOL { D3DPOOL_DEFAULT = 0, D3DPOOL_MANAGED = 1, D3DPOOL_SYSTEMMEM = 2, D3DPOOL_SCRA ...

  10. 面向对象-Object类

    一.Object类中的equals()方法 equals(Object obj) :指示其它某个对象是否与此对象"相等". 返回值类型是boolean Oblect类中的equal ...