xdoj-1117(记忆化搜索+组合数学)

因为我是从上到下,所以就不叫动态规划而叫记忆化搜索吧 

(不过运行时间只有3ms....应该是很不错的吧)

排版怎么那么难看。。。编辑的时候不是这样子的啊?!

思想 : 大眼一看应该是一道很裸的状压dp; 状态为填完每一列 每行%2的值 ; 状态的改变用异或就可以啦

我不是很会状压。。。有一个想法------状态是无序的 【我们只需要两个值】

k0(0的个数),k1(1的个数)来描述状态就可以啦。。

  1)状态描述:f[x][k0][k1]   在【1~(x-1)】列填好基础上填满整个方格

   要求填满方格之后 每行panda个数(从x列开始)%2==0个数为k0 ; ==1个数为k1

      2) 状态转移:每列要求填a[i]个,a[i]个怎么分配呢?

         要求为0的方格填k个,要求为1的方格填a[i]-k;      

//状态转移
LL ans=0; for (int i=0;i<=a[t];i++) {//有i个数放在了0的位置 if ( i<=k0 ) { int t1=k0-i*2+a[t]; //t1=k0-i+a[i]-0 0填一个表情变成1 1填一个变成0 int t2=k1+2*i-a[t]; if (f[t+1][t1][t2]<0) //记忆化搜索 f[t+1][t1][t2]=dfs (t1,t2,t+1,sum-a[t]); ans=( ans+C(k0,i)*C(k1,a[t]-i)%mod*f[t+1][t1][t2]%mod )%mod;// c(k0,i)--》 从k0个位置选i个位置 } }

3) 有个小优化的彩蛋哦。。。

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int N=100+7;
const int mod=1e9+7;
int a[N];
int n,m;
int cnt;
LL f[110][15][15];
LL b[15][15];
LL C (int x, int y) {
    if (y==0) return 1;
    if (x-y<y) y=x-y;
    if (b[x][y]) return b[x][y];
    LL sum=1;
    LL k=1;
    for (int i=x-y+1;i<=x;i++) 
        sum=sum*i/(k++);
    b[x][y]=sum;
    return sum;
}
LL dfs (int k0,int k1,int t,int sum) {
    if (t==m)  {
        if (k1==a[t]) return 1;
        return 0;
    }
    if (sum%2!=k1%2) return 0;// 优化。。想想为什么呢?!
    LL ans=0;
    for (int i=0;i<=a[t];i++) {
        if ( i<=k0 ) {
            int t1=k0-i*2+a[t]; 
            int t2=k1+2*i-a[t];
            if (f[t+1][t1][t2]<0)  
                f[t+1][t1][t2]=dfs (t1,t2,t+1,sum-a[t]);
            ans=( ans+C(k0,i)*C(k1,a[t]-i)%mod*f[t+1][t1][t2]%mod )%mod;
        }
    }
    return ans;
}
int main ()
{
    while (~scanf ("%d %d",&n,&m)) {
        int k0,k1;
        memset (f,-1,sizeof(f));
        k1=k0=0;
        int sum=0;
        for (int i=1;i<=m;i++) { 
            scanf ("%d",&a[i]);
            sum+=a[i];
        }
        for (int i=1;i<=n;i++) {
            int x; scanf ("%d",&x);
            if (x) k1++;
            else   k0++;
        }
        LL ans=dfs (k0,k1,1,sum);
        printf ("%lld\n",ans);
    }
    return 0;
}

        

1117: Insert Orz Pandas

时间限制: 2 Sec  内存限制: 128 MB
提交: 32  解决: 12
[ 提交][ 状态][ 讨论版]

题目描述

Orz panda emotion is a famous emotion in XDU/ACM-ICPC QQ groups.
Big Big Xi loves to create new Orz panda emotions.
Now he has a matrix with n lines and m columns,form n*m cells.
And he wants to insert some small Orz pandas to this matrix to create a big emotion.
In each cell of the matrix,he will determine whether put a small Orz panda or not.
For some reasons,he has some special needs of the emotions:
1.In the ith column,there must be a[i] small Orz pandas. (1<=i<=m)
2.In the ith line,assume the total number of Orz pandas is x, x mod 2 must be b[i]. (1<=i<=n)
For example, if n=2 and m=3,a[1..3]={1,1,1},b[1..2]={1,0}

Now, Big Big Xi wants to know there are how many adapted ways to insert the pandas.

 

输入

There are multiple test cases (no more than 100,and no more than 10 test cases with large n and m), please process to EOF.

In each test case,there are two numbers N and M at the first line.(0<n<=10, 0<m<=100)

Then m lines, the ith line has a number indicates a[i].

And then n lines,the ith line has a number indicates b[i].

输出

One number which is the answer of the question (mod by 1e9+7)

样例输入

1 1
1
1
2 3
1
1
1
1
0

样例输出

1
4

猜你喜欢

转载自www.cnblogs.com/xidian-mao/p/9453813.html