📜  从L到R的所有自然数之和(对于L和R较大的值)

📅  最后修改于: 2021-05-06 20:22:22             🧑  作者: Mango

给定两个非常大的数LR ,其中L≤R ,任务是计算从LR的所有自然数之和。总和可能很大,因此请打印总和%1000000007

例子:

方法:

  • sum(N)是一个返回前N个自然数之和的函数。
  • N个自然数的总和sum(N)=(N *(N + 1))/ 2
  • L到R之间的数字之和为RangeSum = sum(R)– sum(L – 1)
  • 答案是使用10 9 + 7取模的,所以,

下面是上述方法的实现:

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