Sum of Remainders(数学题)

F - Sum of Remainders

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

Submit Status Practice CodeForces 616E

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

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

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cmath>
 4 using namespace std;
 5
 6 #define ll __int64
 7 const ll MOD=1e9+7;
 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=0,las=m+1;//记录哪些数没被减
15     m=min(n,m);//n 余大于 n 的都等于 n
16     ll nn=(ll)sqrt(n*1.0);
17     for (ll i=1;i<=nn;i++)
18     {
19         ll l = n/(i+1)+1;
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+1);//这里高斯求和有个坑,要先判断哪个数可以除2,再乘
28         if (s1%2==0) s1/=2;     //直接用公式也不对,会超出ll限制
29         else s2/=2;
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=1;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 0;
44 }

时间: 2024-08-10 01:54:47

Sum of Remainders(数学题)的相关文章

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). 然后我们可以发现  1/4 = 2/4 = 3/4 = 0, 4/4 = 5/4 = 6/4 = 7/4 = 1. 所以可以将这些结果分成很多块, 按块算结果. 注意计算过程中时刻避免爆longlong. #include <iostream> #include <vector> #include

hdu 2058 The sum problem(数学题)

题意:求[1,n]的子区间,使得子区间的元素和为m 代码: #include<cstdio> #include<cstring> #include<cmath> using namespace std; int main() { int n,m; while(scanf("%d%d",&n,&m)&&(n||m)) { for(int j=(int)sqrt(2*m);j>=1;j--) { int i=(2*m

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

题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=2058 思路: 这题的n,m都很大,很显然直接暴力,会超时,那就不能全部都找了,利用等差数列求和公式, (1)sn=n*(a1+an)/2: 即可代入公式,(2)m=(e-s+1)*(s+e)/2                         注释*******//s代表起点,e代表终点. 则由(2)推出,(3)e=(int)(2*m+s*s-s),根据e点找s点,代入(1)成立则输出[s,e]; 

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

题目链接:http://codeforces.com/problemset/problem/616/E 题意很简单就不说了. 因为n % x = n - n / x * x 所以答案就等于 n * m - (n/1*1 + n/2*2 ... n/m*m) 在根号n复杂度枚举x,注意一点当m>n时,后面一段加起来就等于0,就不用再枚举了. 中间一段x1 ~ x2 的n/x可能相等,所以相等的一段等差数列求和. 1 //#pragma comment(linker, "/STACK:1024

杭电ACM分类

杭电ACM分类: 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDIATE DECODABILITY

【转】对于杭电OJ题目的分类

[好像博客园不能直接转载,所以我复制过来了..] 1001 整数求和 水题1002 C语言实验题——两个数比较 水题1003 1.2.3.4.5... 简单题1004 渊子赛马 排序+贪心的方法归并1005 Hero In Maze 广度搜索1006 Redraiment猜想 数论:容斥定理1007 童年生活二三事 递推题1008 University 简单hash1009 目标柏林 简单模拟题1010 Rails 模拟题(堆栈)1011 Box of Bricks 简单题1012 IMMEDI

转载:hdu 题目分类 (侵删)

转载:from http://blog.csdn.net/qq_28236309/article/details/47818349 基础题:1000.1001.1004.1005.1008.1012.1013.1014.1017.1019.1021.1028.1029. 1032.1037.1040.1048.1056.1058.1061.1070.1076.1089.1090.1091.1092.1093. 1094.1095.1096.1097.1098.1106.1108.1157.116

zoj 2358,poj 1775 Sum of Factorials(数学题)

题目poj 题目zoj //我感觉是题目表述不确切,比如他没规定xi能不能重复,比如都用1,那么除了0,都是YES了 //算了,这种题目,百度来的过程,多看看记住就好 //题目意思:判断一个非负整数n能否表示成几个数的阶乘之和 //这里有一个重要结论:n!>(0!+1!+……+(n-1)!), //证明很容易,当i<=n-1时,i!<=(n-1)!,故(0!+1!+……+(n-1)!)<=n*(n-1)!=n!. // 由于题目规定n<=1000000,而10!=362880

Timus 1120. Sum of Sequential Numbers 数学题

There is no involute formulation concerning factitiously activity of SKB Kontur in this problem. Moreover, there is no formulation at all. Input There is the only number N, 1 ≤ N ≤ 109. Output Your program is to output two positive integers A and P s