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 BOutput
?
Print the number of the different possible sums.
Sample Input 1
?
4 4 6Sample Output 1
?
5There 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; }?
?
總結
以上是生活随笔為你收集整理的AtCoder-2379 - 连接竹竿 思维 | 数学的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: fseek
- 下一篇: 常用工具说明--搭建基于rietveld