Codeforces gym 102219 F. Military Class (状压 dp)

链接: F. Military Class

题意:
给两个 1 - n的序列,要求序列中的数两两配对,使得配对的两个数绝对值之差小于 e ,并且还有 k 对限制,即 u 不能和 v 配对。

思路:
观察到 e 的值只有 4 ,也就是 当前数最多与 9 个数配对,所以可以状压表示配对到当前位置,已经用了 9 个位置中的哪几个,注意判断是否冲突时要把上一个状态右移一位,和当前位对应。

代码:

#include<iostream>
#include<cstdio>
#include<stack>
#include<math.h>
#include<queue>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll ;
const int maxn=2e3+7;
const int mod = 1e9 +7;
ll dp[maxn][maxn],mp[maxn][maxn],ans;
int n,e,k;
int main(){
    
    
    cin>>n>>e>>k;
    for(int i = 0,u,v; i < k; i ++){
    
    
        scanf("%d%d",&u,&v);
        mp[u][v] = 1;
    }
    dp[0][0] = 1;
    for(int i = 1; i <= n; i++){
    
    
        for(int pre = 0; pre < (1 << 2 * e + 1); pre ++){
    
    
            for(int j = - e; j <= e; j ++){
    
    
                int k = i + j;
                if(k < 1 || k > n || mp[i][k]) continue;
                if((pre >> 1) & (1 << (j + e)))continue;
                dp[i][(pre >> 1)|(1 << (j + e))] = (dp[i][(pre >> 1)|(1 << (j + e))] + dp[i-1][pre]) % mod;
            }
        }
    }
    for(int i = 0; i < (1 << 2 * e + 1); i++){
    
    
        ans = (ans + dp[n][i]) % mod;
    }
    printf ("%lld\n",ans);



}


猜你喜欢

转载自blog.csdn.net/hddddh/article/details/107885734
今日推荐