D - Sum of Large Numbers

Posted asunayi

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了D - Sum of Large Numbers相关的知识,希望对你有一定的参考价值。

Time Limit: 2 sec / Memory Limit: 1024 MB

 

Score : 400 points

ps:我发现一个很有趣的问题,long long和int 一起使用时,数据过大可能会出现错误,比如下面。所以最好还是统一用long long。

Problem Statement

We have N+1 integers: 10^100 , 10^100+1, ..., 10^100+N .

We will choose K or more of these integers. Find the number of possible values of the sum of the chosen numbers, modulo (109+7) .

Constraints

  • 1N2×105
  • 1KN+1
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

NK

Output

Print the number of possible values of the sum, modulo (10^9+7) .


Sample Input 1 Copy

Copy
3 2

Sample Output 1 Copy

Copy
10

The sum can take 1010 values, as follows:

  • (10100)+(10100+1)=2×10100+1(10100)+(10100+1)=2×10100+1
  • (10100)+(10100+2)=2×10100+2(10100)+(10100+2)=2×10100+2
  • (10100)+(10100+3)=(10100+1)+(10100+2)=2×10100+3(10100)+(10100+3)=(10100+1)+(10100+2)=2×10100+3
  • (10100+1)+(10100+3)=2×10100+4(10100+1)+(10100+3)=2×10100+4
  • (10100+2)+(10100+3)=2×10100+5(10100+2)+(10100+3)=2×10100+5
  • (10100)+(10100+1)+(10100+2)=3×10100+3(10100)+(10100+1)+(10100+2)=3×10100+3
  • (10100)+(10100+1)+(10100+3)=3×10100+4(10100)+(10100+1)+(10100+3)=3×10100+4
  • (10100)+(10100+2)+(10100+3)=3×10100+5(10100)+(10100+2)+(10100+3)=3×10100+5
  • (10100+1)+(10100+2)+(10100+3)=3×10100+6(10100+1)+(10100+2)+(10100+3)=3×10100+6
  • (10100)+(10100+1)+(10100+2)+(10100+3)=4×10100+6(10100)+(10100+1)+(10100+2)+(10100+3)=4×10100+6

Sample Input 2 Copy

Copy
200000 200001

Sample Output 2 Copy

Copy
1

We must choose all of the integers, so the sum can take just 1 value.


Sample Input 3 Copy

Copy
141421 35623

Sample Output 3 Copy

Copy
220280457
#include <bits/stdc++.h>
using namespace std;
const int mod=1e9+7;

int main(){
long long n,k;//若使用int n,k,答案错误
long long sum=0;
cin>>n>>k;
while(k<=n+1){
    sum+=(2*n-k+1)*k/2-k*(k-1)/2+1;
    k++;
}
sum%=mod;
cout<<sum<<endl;
return 0;
}

  

以上是关于D - Sum of Large Numbers的主要内容,如果未能解决你的问题,请参考以下文章

中心极限定理 | central limit theorem | 大数定律 | law of large numbers

[ CF Contest ] Sum of Round Numbers 题解

[ CF Contest ] Sum of Round Numbers 题解

The Sum of 0 for four numbers(拆解加二分思想)

LeetCode Sum of Square Numbers

#Leetcode# 633. Sum of Square Numbers