版权声明:https://blog.csdn.net/huashuimu2003 https://blog.csdn.net/huashuimu2003/article/details/89490948
title
BZOJ 1231
LUOGU 2915
Description
混乱的奶牛 [Don Piele, 2007] Farmer John的N(4 <= N <= 16)头奶牛中的每一头都有一个唯一的编号S_i (1 <= S_i <= 25,000). 奶牛为她们的编号感到骄傲, 所以每一头奶牛都把她的编号刻在一个金牌上, 并且把金牌挂在她们宽大的脖子上. 奶牛们对在挤奶的时候被排成一支"混乱"的队伍非常反感. 如果一个队伍里任意两头相邻的奶牛的编号相差超过K (1 <= K <= 3400), 它就被称为是混乱的. 比如说,当N = 6, K = 1时, 1, 3, 5, 2, 6, 4 就是一支"混乱"的队伍, 但是 1, 3, 6, 5, 2, 4 不是(因为5和6只相差1). 那么, 有多少种能够使奶牛排成"混乱"的队伍的方案呢?
Input
- 第 1 行: 用空格隔开的两个整数N和K
- 第 2…N+1 行: 第i+1行包含了一个用来表示第i头奶牛的编号的整数: S_i
Output
第 1 行: 只有一个整数, 表示有多少种能够使奶牛排成"混乱"的队伍的方案. 答案保证是 一个在64位范围内的整数.
Sample Input
4 1
3
4
2
1
Sample Output
2
输出解释:
两种方法分别是:
3 1 4 2
2 4 1 3
analysis
设 表示以 结尾,状态为 时的总方案数。
- 初始:
- 状态转移方程:
- 目标: 。
code
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=17,maxs=1<<16;
template<typename T>inline void read(T &x)
{
x=0;
T f=1, ch=getchar();
while (!isdigit(ch) && ch^'-') ch=getchar();
if (ch=='-') f=-1, ch=getchar();
while (isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48), ch=getchar();
x*=f;
}
int s[maxn];
ll f[maxn][maxs],ans;
int main()
{
int n,C;
read(n);read(C);
for (int i=1; i<=n; ++i)
read(s[i]);
for (int i=1; i<=n; ++i)
f[i][1<<i-1]=1;
for (int i=0; i<=(1<<n)-1; ++i)
for (int j=1; j<=n; ++j)
if ((1<<j-1)&i)
for (int k=1; k<=n; ++k)
if (i!=((1<<k-1)|i) && abs(s[j]-s[k])>C)
f[k][(1<<k-1)|i]+=f[j][i];
for (int i=1; i<=n; ++i)
ans+=f[i][(1<<n)-1];
printf("%lld",ans);
return 0;
}