codeforces 571a//Lengthening Sticks// Codeforces Round #317

题意:三角形3条边,最多共添加ll长,问组成的合法三角形个数。

本来想用暴搜,觉得会超时就搜题解了。不过保证我解释得更清晰。

先计算ll长分配给3条边有几种分法?由于不分也是合法的,因此最后实际分出去的量从0-ll都有可能。for循环枚举实际分的量(记为i)。对于每个x,分为m,p,q三份(每份可为0),相当于x+3分为,m+1,p+1,q+1(每份不可为0),相当于x+3长度上(中间只有x+2个间隔,选2个)切不同的两刀。因此就是循环(x+2)*(x+2-1)/(2*(2-1));

然后计算分法中不合法的数量。假如a是分完后最长的,那么a至少要分i=(b+c-a)长,b+c-a到ll之间枚举a增加的长度i,还剩ll-i,b和c的总和不超过a,因此bc增加的长度小于a+i-(b+c),因此bc能增加的总长度在0-min(ll-i,a+i-(b+c))之间,用上面分三份的方法计算分两份的组合。

乱码:

#pragma comment(linker,"/STACK:1024000000,1024000000") 
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
#include <stack>
using namespace std;
const int SZ=200010,INF=0x7FFFFFFF;
typedef long long lon;

lon sub(lon a,lon b,lon c,lon ll)
{
    lon res=0;
    for(lon i=max((lon)0,b+c-a);i<=ll;++i)
    {
        lon less=min(ll-i,a+i-(b+c));
        res+=less+1+less*(less+1)/2;
    }
    //cout<<"res: "<<res<<endl;
    return res;
}

int main()
{
    std::ios::sync_with_stdio(0);
    lon a,b,c,ll;
    cin>>a>>b>>c>>ll;
    lon res=0;
    for(lon i=0;i<=ll;++i)
    {
        res+=(i+3-1)*(i+3-2)/2;
    }
    //cout<<res<<endl;
    res-=sub(a,b,c,ll);
    res-=sub(b,c,a,ll);
    res-=sub(c,a,b,ll);
    cout<<res<<endl;
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/gaudar/p/9645006.html