cf. Lengthening Sticks 组合数学

                       Lengthening Sticks

题目抽象:给你四个非负整数a, b,c, l.  问有多少种  a + x, b + y , c +z (其中 x + y + z <= l, x ,y,z >= 0) 情况使得这三边可以组成三角形.

分析:正面组合比较困难.  可以从反面来分析.   首先考虑所有的分配情况.   x + y +z = i (0 <= i <= l)的分配情况有  c(i + 2, 2)种.  那么总共的分配情况

为sigma c(i + 2, 2) (0 <=i <= l);   

接下来排除不满足的情况.  将不满足的三角形分成三类,分别为a, b, c为最长边时不满足的情况.   当以a为最长边时,不满足的情况为分配后  b + c <= a的情况.

 

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <queue>
 7 #include <map>
 8 using namespace std;
 9 typedef long long LL;
10 const int INF = 0X6FFFFFFF;
11 const int MS = 1005;
12 
13 LL cal(LL a, LL b, LL c, LL m) {
14     LL ret = min(a - b - c, m);
15     if (ret < 0)
16         return 0;
17     else
18         return (ret + 2) * (ret + 1) / 2;
19 }
20 
21 LL cal2(LL a, LL b, LL c, LL l) {
22     LL ret = 0;
23     for (LL i = max(b + c -a, 0LL); i <= l; i++) {
24         LL x = min(l - i, a + i - b - c);
25         //  将最多x长度分配给b,c.   也就是求和sigma bb + cc = t (0 <=t <= x)
26         //   ==   1 + 2 + ... + (x + 1)  = (x + 2) * (x + 1) / 2;
27         ret += (x + 2) * (x + 1) / 2;
28     }
29     return ret;
30 }
31 
32 int main() {
33     LL a, b, c, l;
34     cin >> a >> b >> c >>  l;
35     LL ans = 0;
36     for (LL i = 0; i <= l; i++)
37         ans += (i + 2) * (i + 1) / 2;   // 所有分配情况.
38     // 枚举三个边分别+i的长度且为最长边时的非法情况数目。
39     for (int i = 0; i <= l; i++) {
40         ans -= cal(a + i, b, c, l - i);
41         ans -= cal(b + i, a, c, l - i);
42         ans -= cal(c + i, a, b, l - i);
43     }
44 
45     /*
46     ans -= cal2(a, b, c, l);   // a作为最长边不满足的情况. 也就是分配后b + c <= a的情况
47     ans -= cal2(b, a, c, l);
48     ans -= cal2(c, a, b, l);
49     */
50     cout << ans << endl;
51     return 0;
52 }

 

posted on 2015-08-24 11:04  hutaishi  阅读(152)  评论(0编辑  收藏  举报

导航