HDU5155 Harry And Magic Box
2017-10-29 19:51
288 查看
Harry And Magic Box
传送门1传送门2
One day, Harry got a magical box. The box is made of n*m grids. There are sparking jewel in some grids. But the top and bottom of the box is locked by amazing magic, so Harry can’t see the inside from the top or bottom. However, four sides of the box are transparent, so Harry can see the inside from the four sides. Seeing from the left of the box, Harry finds each row is shining(it means each row has at least one jewel). And seeing from the front of the box, each column is shining(it means each column has at least one jewel). Harry wants to know how many kinds of jewel’s distribution are there in the box.And the answer may be too large, you should output the answer mod 1000000007.
[b]Input[/b]
There are several test cases.
For each test case,there are two integers n and m indicating the size of the box.0≤n,m≤50.
[b]Output[/b]
For each test case, just output one line that contains an integer indicating the answer.
[b]Sample Input[/b]
1 1
2 2
2 3
[b]Sample Output[/b]
1
7
25
[b]Hint[/b]
There are 7 possible arrangements for the second test case.
They are:
11
11
11
10
11
01
10
11
01
11
01
10
10
01
Assume that a grids is ‘1’ when it contains a jewel otherwise not.
题意
在n*m的矩阵内每一行每一列都有钻石,问钻石分布的种类。分析
[b]法一 [/b]定义dp[i][j]表示前i行,都满足了每一行至少有一个宝石的条件,且只有j列满足了有宝石的条件的情况有多少种。
枚举第i+1行放的宝石数k,这k个当中有t个是放在没有宝石的列上的,那么我们可以得到转移方程:
dp[i+1][j+t]+=dp[i][j]∗Ctm−j∗Ck−tj
[b]法二 [/b]
定义f(i)为每一行有i列必不存在钻石,则有Cim种。
而对于其他m−i列可放可不放,但不能全都不放,且有n行。于是有f(i)=Cim∗(2m−i−1)n种放法。
再根据容斥原理:得出结果ans=f(0)−f(1)+f(2)−......f(n)
CODE
[b]法一 [/b]#include<cstdio> #include<memory.h> #define mod 1000000007 #define N 55 #define FOR(i,a,b) for(int i=(a),i##_END_=(b);i<=i##_END_;i++) typedef long long LL; int n,m; LL C ,dp ; int main() { C[0][0]=1; FOR(i,1,50){ C[i][i]=C[i][0]=1; FOR(j,1,i-1)C[i][j]=(C[i-1][j-1]+C[i-1][j])%mod; } while(~scanf("%d%d",&n,&m)) { memset(dp,0,sizeof dp); FOR(i,1,m)dp[1][i]=C[m][i]; FOR(i,2,n)FOR(k,1,m)FOR(z,0,k)FOR(j,k-z,m-z) dp[i][j+z]=(dp[i][j+z]+dp[i-1][j]*C[j][k-z]%mod*C[m-j][z])%mod; printf("%lld\n",dp [m]); } return 0; }
[b]法二 [/b]
#include<cstdio> #include<memory.h> #define mod 1000000007 #define N 55 #define FOR(i,a,b) for(int i=(a),i##_END_=(b);i<=i##_END_;i++) typedef long long LL; int n,m; LL C ,pow ; int main() { pow[0]=C[0][0]=1; FOR(i,1,50) { C[i][i]=C[i][0]=1; pow[i]=(pow[i-1]<<1)%mod; FOR(j,1,i-1)C[i][j]=(C[i-1][j-1]+C[i-1][j])%mod; } while(~scanf("%d %d",&n,&m)) { if(n<=1||m<=1) { puts("1"); continue; } int f=1; int ans=0,s; FOR(i,0,m) { s=C[m][i]; FOR(j,1,n)s=1LL*s*(pow[m-i]-1)%mod; ans=(ans+f*s)%mod; f=-f; } if(ans<0)ans+=mod; printf("%d\n",ans); } return 0; }
相关文章推荐
- hdu5155---Harry And Magic Box
- HDU 5155 Harry And Magic Box --DP
- [HDOJ 5155] Harry And Magic Box
- HDU - 5155 Harry And Magic Box
- hdu 5155 Harry And Magic Box(DP)
- 【HDOJ】5155 Harry And Magic Box
- Hdu 5155 Harry And Magic Box
- 【CUGBACM15级BC第25场 B】hdu 5155 Harry And Magic Box
- HDU-5155 Harry And Magic Box(DP,组合数)
- [BestCoder Round #25 1002]Harry And Magic Box 解题报告
- 【HDU】5155 Harry And Magic Box 【容斥原理】
- hdu 5155 Harry And Magic Box(递推)
- Harry And Magic Box
- HDU 5155 Harry And Magic Box(容斥+组合数学)
- HDU 5155 Harry And Magic Box
- HDU 5155 Harry And Magic Box(组合+容斥)
- HDU 5155 Harry And Magic Box --DP
- HDOJ 5155 Harry And Magic Box DP
- Harry And Magic Box (dp题)
- HDU 5155 Harry And Magic Box( DP )