51 Nod 1509 加长棒(巧妙的隔板法计数)
1509?加長棒?
題目來源:?CodeForces
基準時間限制:1?秒 空間限制:131072?KB 分值:?40?難度:4級算法題
?收藏
?關注
現在有三根木棒,他們的長度分別是a,b,c厘米。你可以對他們進行加長(不同的木棒可以增加不同的長度),他們總的加長長度不能超過L厘米。你也可以不對他們進行加長。
現在請你計算一下有多少種加長的方式使得他們能構成合法的三角形(面積非0)。
Input
單組測試數據。 共一行,包含4?個整數a,b,c,L?(1≤a,b,c≤3*10^5,?0≤L≤3*10^5)。Output
輸出答案占一行。Input示例
1?1?1?2Output示例
4參考了這位大神代碼:
https://www.cnblogs.com/zyb993963526/p/6718933.html
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<cmath>
using namespace std;
typedef long long LL;
LL a,b,c,l;
LL solve(LL a,LL b, LL c, LL l) ?//設c為最大邊
{
? ? LL cnt=0;
? ? for(int z=0;z<=l;z++)
? ? {
? ? ? ? LL x=min(c+z-a-b,l-z);
? ? ? ? if(x>=0) ?cnt+=(x+1)*(x+2)/2;
? ? }
? ? return cnt;
}
int main()
{
? ? //freopen("D:\\input.txt","r",stdin);
? ? while(~scanf("%lld%lld%lld%lld",&a,&b,&c,&l))
? ? {
? ? ? ? LL ans=(l+1)*(l+2)*(l+3)/6;
? ? ? ? ans-=solve(a,b,c,l);
? ? ? ? ans-=solve(a,c,b,l);
? ? ? ? ans-=solve(b,c,a,l);
? ? ? ? printf("%lld\n",ans);
? ? }
}
轉載于:https://www.cnblogs.com/linruier/p/9865348.html
總結
以上是生活随笔為你收集整理的51 Nod 1509 加长棒(巧妙的隔板法计数)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Django Form -- 对单个表单
- 下一篇: andrew ng machine le