AtCoder-2379 - 连接竹竿 思维 | 数学

Problem Statement

 

Snuke has N integers. Among them, the smallest is A, and the largest is B. We are interested in the sum of those N integers. How many different possible sums there are?

Constraints

 

  • 1≤N,A,B≤109
  • A and B are integers.

Input

 

Input is given from Standard Input in the following format:

N A B

Output

 

Print the number of the different possible sums.

Sample Input 1

 

4 4 6

Sample Output 1

 

5

There are five possible sums: 18=4+4+4+6, 19=4+4+5+6, 20=4+5+5+6, 21=4+5+6+6 and 22=4+6+6+6.

有点唬人 还以为是组合数学相关一类的问题

题意:就是说已知有N条竹竿  然后竹竿长度分布的范围为A~B 

那么求不同可能的长度的竹竿长度之和的数量 

分析:一开始一直再往r进制或是组合数上想 因为这种问题难免考虑组合

但是最后也没想出一个理想的公式 看过题解后才发觉有更简单更好的做法

我们可知竹竿之和的范围: N个竹竿 长度之和最小大概是 (N-1)* A+B   ~ (N-1)*B+A 此为最大值

那么如果从最小之和开始 让一个竹竿长度+1 那么最终之和就是离着最大和又近了一步 那么如果重复上个步骤

我们会发现 由于每个竹竿都向一个从A~B的进度条 于是乎在范围内的所有长度都会通过+1遍历到

所以 几行代码就搞定


#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int main()
{
    ll n,s,e;
    scanf("%lld%lld%lld",&n,&s,&e);
    printf("%lld\n",max(1LL*0,((n-1)*e+s)-((n-1)*s+e-1)));//int会溢出
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_33859479/article/details/87193201