[hdu5203]计数水题

思路:把一个木棍分成3段,使之能够构成三角形的方案总数可以这样计算,枚举一条边,然后可以推公式算出当前方案数。对于已知一条边的情况,也用公式推出。用max和min并维护下,以减少情况数目。

  1 #pragma comment(linker, "/STACK:10240000,10240000")
  2 
  3 #include <iostream>
  4 #include <cstdio>
  5 #include <algorithm>
  6 #include <cstdlib>
  7 #include <cstring>
  8 #include <map>
  9 #include <queue>
 10 #include <deque>
 11 #include <cmath>
 12 #include <vector>
 13 #include <ctime>
 14 #include <cctype>
 15 #include <set>
 16 #include <bitset>
 17 #include <functional>
 18 #include <numeric>
 19 #include <stdexcept>
 20 #include <utility>
 21 
 22 using namespace std;
 23 
 24 #define mem0(a) memset(a, 0, sizeof(a))
 25 #define lson l, m, rt << 1
 26 #define rson m + 1, r, rt << 1 | 1
 27 #define define_m int m = (l + r) >> 1
 28 #define rep0(a, b) for (int a = 0; a < (b); a++)
 29 #define rep1(a, b) for (int a = 1; a <= (b); a++)
 30 #define all(a) (a).begin(), (a).end()
 31 #define lowbit(x) ((x) & (-(x)))
 32 #define constructInt4(name, a, b, c, d) name(int a = 0, int b = 0, int c = 0, int d = 0): a(a), b(b), c(c), d(d) {}
 33 #define constructInt3(name, a, b, c) name(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {}
 34 #define constructInt2(name, a, b) name(int a = 0, int b = 0): a(a), b(b) {}
 35 #define pchr(a) putchar(a)
 36 #define pstr(a) printf("%s", a)
 37 #define sint(a) ReadInt(a)
 38 #define sint2(a, b) ReadInt(a);ReadInt(b)
 39 #define sint3(a, b, c) ReadInt(a);ReadInt(b);ReadInt(c)
 40 #define pint(a) WriteInt(a)
 41 
 42 typedef double db;
 43 typedef long long LL;
 44 typedef pair<int, int> pii;
 45 typedef multiset<int> msi;
 46 typedef set<int> si;
 47 typedef vector<int> vi;
 48 typedef map<int, int> mii;
 49 
 50 const int dx[8] = {0, 1, 0, -1, 1, 1, -1, -1};
 51 const int dy[8] = {1, 0, -1, 0, -1, 1, 1, -1};
 52 const int maxn = 1e3 + 7;
 53 const int maxm = 1e5 + 7;
 54 const int maxv = 1e7 + 7;
 55 const int max_val = 1e6 + 7;
 56 const int MD = 1e9 +7;
 57 const int INF = 1e9 + 7;
 58 const double PI = acos(-1.0);
 59 const double eps = 1e-10;
 60 
 61 template<class T>T gcd(T a, T b){return b==0?a:gcd(b,a%b);}
 62 template<class T>void ReadInt(T &x){char c=getchar();while(!isdigit(c))c=getchar();x=0;while(isdigit(c)){x=x*10+c-'0';c=getchar();}}
 63 template<class T>void WriteInt(T i) {int p=0;static int b[20];if(i == 0) b[p++] = 0;else while(i){b[p++]=i%10;i/=10;}for(int j=p-1;j>=0;j--)pchr('0'+b[j]);}
 64 
 65 
 66 LL work(int a, int b) {
 67     if (b <= a) return 0;
 68     return max(0, (a + b - 1) / 2 - (b - a) / 2);
 69 }
 70 LL work(int maxv) {
 71     LL ans = 0;
 72     rep1(i, maxv - 1) {
 73         ans += work(i, maxv - i);
 74     }
 75     return ans;
 76 }
 77 int a[1010];
 78 int main() {
 79     //freopen("in.txt", "r", stdin);
 80     int n, m;
 81     while (cin >> n >> m) {
 82         rep0(i, m) {
 83             sint(a[i]);
 84         }
 85         sort(a, a + m);
 86         int minv = a[0] - 1, maxv = n - a[m - 1];
 87         if (minv > maxv) swap(minv, maxv);
 88         if (minv == 0) pint(work(maxv));
 89         else {
 90             if (minv == 1) {
 91                 if (maxv & 1) pint(0);
 92                 else pint(1);
 93             }
 94             else {
 95                 if (minv == maxv) pint(0);
 96                 else pint(work(minv, maxv));
 97             }
 98         }
 99         pchr('\n');
100     }
101     return 0;
102 }
View Code

 

posted @ 2015-04-12 05:16  jklongint  阅读(181)  评论(0编辑  收藏  举报