给定两个非常大的数L和R ,其中L≤R ,任务是计算从L到R的所有自然数之和。总和可能很大,因此请打印总和%1000000007 。
例子:
Input: L = “8894” R = “98592”
Output: 820693329
Input: L = “88949273204” R = “98429729474298592”
Output: 252666158
方法:
- 令sum(N)是一个返回前N个自然数之和的函数。
- 前N个自然数的总和为sum(N)=(N *(N + 1))/ 2 。
- L到R之间的数字之和为RangeSum = sum(R)– sum(L – 1)
- 答案是使用10 9 + 7取模的,所以,
mod = 109 + 7
RangeSum = (sum(R) – sum(L-1) + mod)%mod;
This can be also written as RangeSum = (sum(R)%mod – sum(L-1)%mod + mod)%mod;
Now, sum(R) % mod can be written as ((R * (R + 1)) / 2) % mod
Or ((R % mod) * ((R + 1) % mod) * invmod(2)) % mod
Since R is large, the modulo of R can be calculated as described here.
The value of inversemod(2) = 500000004 which can be calculated using Fermat’s little theorem.
Similarly, sum(L – 1) % mod can also be calculated.
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
#define mod 1000000007
// Value of inverse modulo
// 2 with 10^9 + 7
const long long inv2 = 500000004;
// Function to return num % 1000000007
// where num is a large number
long long int modulo(string num)
{
// Initialize result
long long int res = 0;
// One by one process all the
// digits of string 'num'
for (long long int i = 0;
i < num.length();
i++)
res = (res * 10
+ (long long int)num[i]
- '0')
% mod;
return res;
}
// Function to return the sum of the
// integers from the given range
// modulo 1000000007
long long int findSum(string L,
string R)
{
long long int a, b, l, r, ret;
// a stores the value of
// L modulo 10^9 + 7
a = modulo(L);
// b stores the value of
// R modulo 10^9 + 7
b = modulo(R);
// l stores the sum of natural
// numbers from 1 to (a - 1)
l = ((a * (a - 1))
% mod * inv2)
% mod;
// r stores the sum of natural
// numbers from 1 to b
r = ((b * (b + 1))
% mod * inv2)
% mod;
ret = (r % mod - l % mod);
// If the result is negative
if (ret < 0)
ret = ret + mod;
else
ret = ret % mod;
return ret;
}
// Driver code
int main()
{
string L = "88949273204";
string R = "98429729474298592";
cout << findSum(L, R) << endl;
return 0;
}
Java
// Java implementation of the approach
import java.util.*;
class GFG
{
static long mod = 1000000007;
// Value of inverse modulo
// 2 with 10^9 + 7
static long inv2 = 500000004;
// Function to return num % 1000000007
// where num is a large number
static long modulo(String num)
{
// Initialize result
long res = 0;
// One by one process all the
// digits of string 'num'
for (int i = 0;
i < num.length(); i++)
res = (res * 10 +
(long)num.charAt(i) - '0') % mod;
return res;
}
// Function to return the sum of the
// longegers from the given range
// modulo 1000000007
static long findSum(String L, String R)
{
long a, b, l, r, ret;
// a stores the value of
// L modulo 10^9 + 7
a = modulo(L);
// b stores the value of
// R modulo 10^9 + 7
b = modulo(R);
// l stores the sum of natural
// numbers from 1 to (a - 1)
l = ((a * (a - 1)) % mod * inv2) % mod;
// r stores the sum of natural
// numbers from 1 to b
r = ((b * (b + 1)) % mod * inv2) % mod;
ret = (r % mod - l % mod);
// If the result is negative
if (ret < 0)
ret = ret + mod;
else
ret = ret % mod;
return ret;
}
// Driver code
public static void main(String[] args)
{
String L = "88949273204";
String R = "98429729474298592";
System.out.println(findSum(L, R));
}
}
// This code is contributed by 29AjayKumar
Python3
# Python3 implementation of the approach
mod = 1000000007
# Value of inverse modulo
# 2 with 10^9 + 7
inv2 = 500000004;
# Function to return num % 1000000007
# where num is a large number
def modulo(num) :
# Initialize result
res = 0;
# One by one process all the
# digits of string 'num'
for i in range(len(num)) :
res = (res * 10 + int(num[i]) - 0) % mod;
return res;
# Function to return the sum of the
# integers from the given range
# modulo 1000000007
def findSum(L, R) :
# a stores the value of
# L modulo 10^9 + 7
a = modulo(L);
# b stores the value of
# R modulo 10^9 + 7
b = modulo(R);
# l stores the sum of natural
# numbers from 1 to (a - 1)
l = ((a * (a - 1)) % mod * inv2) % mod;
# r stores the sum of natural
# numbers from 1 to b
r = ((b * (b + 1)) % mod * inv2) % mod;
ret = (r % mod - l % mod);
# If the result is negative
if (ret < 0) :
ret = ret + mod;
else :
ret = ret % mod;
return ret;
# Driver code
if __name__ == "__main__" :
L = "88949273204";
R = "98429729474298592";
print(findSum(L, R)) ;
# This code is contributed by AnkitRai01
C#
// C# implementation of the approach
using System;
class GFG
{
static long mod = 1000000007;
// Value of inverse modulo
// 2 with 10^9 + 7
static long inv2 = 500000004;
// Function to return num % 1000000007
// where num is a large number
static long modulo(String num)
{
// Initialize result
long res = 0;
// One by one process all the
// digits of string 'num'
for (int i = 0;
i < num.Length; i++)
res = (res * 10 +
(long)num[i] - '0') % mod;
return res;
}
// Function to return the sum of the
// longegers from the given range
// modulo 1000000007
static long findSum(String L, String R)
{
long a, b, l, r, ret;
// a stores the value of
// L modulo 10^9 + 7
a = modulo(L);
// b stores the value of
// R modulo 10^9 + 7
b = modulo(R);
// l stores the sum of natural
// numbers from 1 to (a - 1)
l = ((a * (a - 1)) % mod * inv2) % mod;
// r stores the sum of natural
// numbers from 1 to b
r = ((b * (b + 1)) % mod * inv2) % mod;
ret = (r % mod - l % mod);
// If the result is negative
if (ret < 0)
ret = ret + mod;
else
ret = ret % mod;
return ret;
}
// Driver code
public static void Main(String[] args)
{
String L = "88949273204";
String R = "98429729474298592";
Console.WriteLine(findSum(L, R));
}
}
// This code is contributed by PrinciRaj1992
252666158