P1896-[SCOI2005]互不侵犯

 1 #include <bits/stdc++.h>
 2 #define _for(i,a,b) for(register int i = (a);i < b;i ++)
 3 #define _rep(i,a,b) for(register int i = (a);i > b;i --)
 4 #define INF 0x3f3f3f3f
 5 #define MOD 1000000007
 6 #define maxn 15003
 7 typedef long long ll;
 8 
 9 using namespace std;
10 typedef pair<int,int> P;
11 inline ll read()
12 {
13     ll ans = 0;
14     char ch = getchar(), last = ' ';
15     while(!isdigit(ch)) last = ch, ch = getchar();
16     while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
17     if(last == '-') ans = -ans;
18     return ans;
19 }
20 inline void write(ll x)
21 {
22     if(x < 0) x = -x, putchar('-');
23     if(x >= 10) write(x / 10);
24     putchar(x % 10 + '0');
25 }
26 int N,K;
27 int validState[maxn],validStateKingNum[maxn];
28 int validNum = 0;
29 ll dp[10][maxn][82];
30 void Init(int curValidState,int curValidStateKingNum,int col)
31 {
32     if(col > N-1)
33     {
34         validState[++validNum] = curValidState;
35         validStateKingNum[validNum] = curValidStateKingNum;
36         return ;
37     }
38     Init(curValidState,curValidStateKingNum,col + 1);
39     Init(curValidState + (1<<col),curValidStateKingNum + 1,col + 2);
40 }
41 int main()
42 {
43     N = read();K = read();
44     Init(0,0,0);
45     _for(i,1,validNum+1)
46         dp[1][i][validStateKingNum[i]] = 1;
47     _for(i,2,N+1)
48         _for(j,1,validNum+1)
49             _for(k,1,validNum+1)
50             {
51                 if(validState[j]&validState[k]
52                 ||(validState[j]<<1)&validState[k]
53                 ||(validState[j]&validState[k]<<1))
54                     continue;
55                 _rep(s,K,validStateKingNum[j]-1)
56                     dp[i][j][s] += dp[i-1][k][s-validStateKingNum[j]];
57             }
58     ll rnt = 0;
59     _for(i,1,validNum+1)
60         rnt += dp[N][i][K];
61     write(rnt);
62     return 0;
63 }
原文地址:https://www.cnblogs.com/Asurudo/p/11624396.html