Skip to content

Commit 4187d2c

Browse files
authored
Create J(状压dp)
1 parent bbe93c7 commit 4187d2c

File tree

1 file changed

+61
-0
lines changed
  • Training_With_Contest/PSTraining Camp 2016 Day 9

1 file changed

+61
-0
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//Solution 1:
2+
//dp[105][1<<10][35]:表示算到第i个点,顺序状态是sta,%=35的情况总数。
3+
#include <string.h>
4+
#include <algorithm>
5+
#include <iostream>
6+
#include <cstdio>
7+
#include <cstdlib>
8+
#define rep(i,n) for(int i = 0; i < n; i++)
9+
using namespace std;
10+
typedef long long ll;
11+
12+
const int MX = 105, MM = 35, n10 = 1<<10;
13+
14+
// Mod int
15+
const int mod = 1000000007;
16+
struct mint{
17+
ll x;
18+
mint():x(0){}
19+
mint(ll x):x(x){}
20+
mint operator+=(mint a){ if((x+=a.x)>=mod) x-=mod; return *this;}
21+
mint operator-=(mint a){ if((x+=mod-a.x)>=mod) x-=mod; return *this;}
22+
mint operator*=(mint a){ (x*=a.x)%=mod; return *this;}
23+
mint operator+(mint a){ return mint(*this) += a;}
24+
mint operator-(mint a){ return mint(*this) -= a;}
25+
mint operator*(mint a){ return mint(*this) *= a;}
26+
};
27+
//
28+
29+
int n, m, k;
30+
int a[MX];
31+
mint dp[MX][n10][MM];
32+
33+
int f(int x){
34+
int res = 0, pre = -1;
35+
rep(i,k){
36+
if((x&1) != pre) res++;
37+
pre = (x&1); x >>= 1;
38+
}
39+
return res-1;
40+
}
41+
42+
43+
int main() {
44+
scanf("%d%d%d",&n,&m,&k);
45+
rep(i,n-1) scanf("%d",&a[i+1]);
46+
dp[0][0][0] = 1;
47+
rep(i,n)rep(j,1<<k)rep(r,m){
48+
int nxtm = (r+f(j)*a[i])%m;
49+
dp[i+1][j][nxtm] += dp[i][j][r];
50+
rep(l,k){
51+
if(j>>l&1) continue;
52+
dp[i+1][j|1<<l][nxtm] += dp[i][j][r];
53+
}
54+
}
55+
56+
cout << dp[n][(1<<k)-1][0].x << endl;
57+
return 0;
58+
}
59+
60+
//Solution 2:
61+
//dp[105][k][k][m][2]:表示有k1个1°点,k2个2°点。

0 commit comments

Comments
 (0)