HDU 5155 Harry and Magic Box (DP)

Source: Internet
Author: User

Harry and Magic Box

Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): Accepted submission (s): 44


Problem Descriptionone Day, Harry got a magical box. The box is made of n*m grids. There is sparking jewel in some grids. But the top and bottom of the box are locked by amazing magic, so Harry can ' t see the inside from the top or bottom. However, four sides of the box is 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 have at least one jewel). And seeing from the front of the box, each column is shining (it means each column have at least one jewel). Harry wants to know how many kinds of jewel ' s distribution is there in the box. And the answer is too large, you should output the answer mod 1000000007.

Inputthere is several test cases.
For each test case,there is integers n and m indicating the size of the box. 0≤n,m≤

Outputfor Each test case, just output one line, contains an integer indicating the answer.

Sample INPUT1 12 22 3

Sample Output1725 HintThere is 7 possible arrangements for the second Test case.  They are:11 one-on-one-one-1011-one-one-one-01Assume-A-grids is ' 1 ' when it contains a jewel otherwise not. Set DP[I][J] indicates that the number of J columns has been selected in the previous I row.
#include <iostream>#include<cstdio>#include<cstring>#include<string>#include<cmath>#include<vector>#include<queue>#include<map>#include<Set>#include<stack>#include<algorithm>using namespacestd;#defineRoot 1,n,1#defineLson l,mid,rt<<1#defineRson mid+1,r,rt<<1|1#defineLR rt<<1#defineRR rt<<1|1typedefLong LongLl;typedef pair<int,int>PII;#defineX First#defineY SecondConst intOO = 1e9+7;Const DoublePI = ACOs (-1.0);Const DoubleEPS = 1e-6 ;Const intN = the ;Const intMoD = 1e9+7; LL Dp[n][n], f[n], c[n][n];intN, M;voidinit () {f[0] =1 ;  for(inti =1; I <= -; ++i) F[i] = f[i-1] *2 ;  for(inti =0; i < N; ++i) {c[i][0] = C[i][i] =1 ;  for(intj =1; J < I; ++j) {C[i][j]= (c[i-1][J] + c[i-1][j-1] ) %MoD; }    }}voidRun () {memset (DP,0,sizeofDP); dp[0][0] =1 ;  for(inti =0; I < n; ++i) {         for(intj =0; J <= M; ++j) {             for(intK =0; J + k <= m; k++) {LL cnt= F[j];if(!k) cnt--; CNT%=MoD; Dp[i+1][j+k] + = (dp[i][j] * CNT% mod * c[m-j][k])%MoD; Dp[i+1][J+K]%=MoD; }}} printf ("%i64d\n", Dp[n][m]);}intMain () {#ifdef LOCAL freopen ("In.txt","R", stdin); #endif //LOCALinit ();  while(~SCANF ("%d%d",&n,&m)) Run ();}
View Code

HDU 5155 Harry and Magic Box (DP)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.