2734: [HNOI2012]集合选数
分析:
转化一下题意。
1 3 9 27...
2 6 18 54...
4 12 36 108...
8 24 72 216...
...
写成这样的矩阵阵后,那么题意就是不能选相邻的点,求方案数。可以知道行不超过18,列不超过11,然后状压dp即可。
发现5在这个矩阵中没有出现,于是可以在构造a[1][1]=5的矩阵,利用乘法原理求出相乘。同样地,构成a[1][1]为没有出现的数的矩阵,相乘。
代码:
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#include<cctype>
#include<set>
#include<queue>
#include<vector>
#include<map>
using namespace std;
typedef long long LL; inline int read() {
int x=,f=;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch=='-')f=-;
for(;isdigit(ch);ch=getchar())x=x*+ch-'';return x*f;
} const int mod = ; int a[][], b[], f[][], n;
bool vis[]; inline void add(int &x,int y) { x += y; if (x >= mod) x -= mod; }
int Calc(int x) {
memset(b, , sizeof(b));
a[][] = x;
for (int i = ; i <= ; ++i)
if ((a[i - ][] << ) <= n) a[i][] = a[i - ][] << ;
else a[i][] = n + ;
for (int i = ; i <= ; ++i)
for (int j = ; j <= ; ++j)
if (a[i][j - ] * <= n) a[i][j] = a[i][j - ] * ;
else a[i][j] = n + ;
for (int i = ; i <= ; ++i)
for (int j = ; j <= ; ++j)
if (a[i][j] <= n) b[i] += ( << (j - )), vis[a[i][j]] = ;
for (int i = ; i <= ; ++i)
for (int j = ; j <= b[i]; ++j) f[i][j] = ;
f[][] = ;
for (int i = ; i < ; ++i)
for (int s = ; s <= b[i]; ++s)
if (f[i][s])
for (int t = ; t <= b[i + ]; ++t)
if ((s & t) == && (t & (t >> )) == ) add(f[i + ][t], f[i][s]);
return f[][];
} int main() {
n = read(); LL ans = ;
for (int i = ; i <= n; ++i)
if (!vis[i]) ans = 1ll * ans * Calc(i) % mod;
cout << ans;
return ;
}