uva11401:Triangle Counting 递推 数学

  uva11401:Triangle Counting

题目读不清楚的下场就是多做两个小时...从1-n中任选3个不重复数字(不重复啊!!坑爹啊!)问能组成三角形的有多少个,

显然1~n能组成的三角形集合肯定包括了1~n-1所能组成的三角形,所以假如have[i-1]为1~n-1组成的三角形数目,have[i]只要计算:含有长度为n的边的三角形数目+have[i-1] 就行了

然后分一下类就行了,1~n/2为一块,n/2+1~n为一块,因为有一条边必须为n,剩下两条边要么都在第二块,要么分别在第一第二块,在把n分奇偶讨论下很容易就得到公式了

 1 #include <iostream>
 2 #include <string.h>
 3 #include <cstdio>
 4 #include <queue>
 5 #include <vector>
 6 #include <cstring>
 7 #include <algorithm>
 8 #include <math.h>
 9 
10 #define SIGMA_SIZE 26
11 #pragma warning ( disable : 4996 )
12 
13 using namespace std;
14 typedef long long LL;
15 //typedef unsigned long long uLL;
16 
17 inline LL LMax(LL a,LL b)    { return a>b?a:b; }
18 inline LL LMin(LL a,LL b)    { return a>b?b:a; }
19 inline int Max(int a,int b) { return a>b?a:b; }
20 inline int Min(int a,int b) { return a>b?b:a; }
21 inline int gcd( int a, int b ) { return b==0?a:gcd(b,a%b); }
22 inline int lcm( int a, int b ) { return a/gcd(a,b)*b; }  //a*b = gcd*lcm
23 const long long INF = 0x3f3f3f3f3f3f3f3f;
24 const int inf  = 0x3f3f3f3f;
25 const int mod  = 7;
26 const int maxk = 5005;
27 const int maxn = 1e6+5;
28 
29 int num[maxn];
30 LL have[maxn];
31 
32 void init()
33 {
34     //have[1] = 1; have[2] = 2;
35     bool test = true;            //true表示奇数,false表示偶数
36     
37     have[3] = 0;
38     LL tmp;
39     for ( LL i = 4; i <= (LL)1e6; i++ )
40     {
41         LL t = i/2;
42         if (!test)
43         {
44             have[i] = t*(t-1) + have[i-1];
45             test = !test;
46         }
47         else
48         {
49             have[i] =  (t-1)*(t-1) + have[i-1];
50             test = !test;
51         }
52     }
53 }
54 
55 int main()
56 {
57     init();
58 
59     int x;
60     
61     while ( ~scanf("%d", &x) && x >= 3 )
62         printf( "%lld
", have[x] );
63     return 0;
64 }
View Code
原文地址:https://www.cnblogs.com/chaoswr/p/8893647.html