洛谷T47092 作业 + 状压动归

 

只要注意一下细节就毫无难点了,简简单单状态压缩即可。
Code:

#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn = 20;
const long long mod = 4921057 ;
int a[maxn], b[maxn], pos[maxn], sumv[maxn];
long long  dp[1 << maxn][maxn];
inline int lowbit(int t) { return t & (-t); }
inline int dis(int x,int y,int S){
    if(x < y) swap(x, y);
    int ans = sumv[x - 1] - sumv[y];
    for(int i = x; i > y; --i) if((S & pos[i]) != 0) --ans;
    return ans;
}
int main()
{
    int n;
    scanf("%d",&n);
    for(int i = 1;i <= n + 1; ++i) pos[i] = (1 << (i - 1));
    for(int i = 1;i <= n; ++i) { scanf("%d",&a[i]);  sumv[i] = sumv[i - 1] + 1; }
    for(int i = 1;i <= n; ++i) scanf("%d",&b[i]);
    for(int i = 1;i <= n; ++i) dp[pos[i]][i] = 1;
    for(int i = 1;i < pos[n + 1]; ++i)               
    {
        for(int j = 1;j <= n; ++j)               
        {
            if((i & pos[j]) == 0) continue;
            int mx = (i ^ pos[j]);
            if(mx == 0)  continue;
            for(int k = 1;k <= n; ++k)
            {
                if(k == j || (mx & pos[k]) == 0 || a[j] < a[k]) continue;
                if(dis(k, j,mx) > b[k]) continue;
                dp[i][j] += dp[mx][k];
                dp[i][j] %= mod;
            }
        }
    }
    long long  fin = 0;
    for(int i = 1;i <= n; ++i) fin += dp[pos[n + 1] - 1][i] , fin %= mod;
    printf("%lld",fin);
    return 0;
}

  

posted @ 2018-10-02 11:14  EM-LGH  阅读(160)  评论(0编辑  收藏  举报