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
- 1≤N≤2×105
- 1≤K≤N+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;
}
浙公网安备 33010602011771号