Bestcoder Round #25 1002 Harry and Magic Box [DP]

Source: Internet
Author: User

Transmission Door

Harry and Magic Box


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


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 3 Sample Output1725 HintThere is 7 possible arrangements for the second Test case. They are:1111111011011011011101101001assume that a grids was ' 1 ' when it contains a jewel otherwise not.

Official:

1002 Harry and Magic BOXDP, our line of thinking. DP[I][J], which represents the first row, satisfies the condition of at least one gem in each row, and only the J column satisfies the condition of the gem. Enumerating the number of jewels in line i+1 K, where the T in K is placed on a column without a gem, we can get the transfer equation: dp[i+1][j+t]+=dp[i][j]*c[m-j][t]*c[j][k-t], where C[x][y], The number of all combinations of y elements that are randomly selected in x different elements.

1#include <iostream>2#include <cstring>3#include <cstdlib>4#include <cstdio>5#include <algorithm>6#include <cmath>7#include <queue>8#include <map>9#include <Set>Ten#include <string> One  A #defineN 55 - #defineM 10 - #defineMoD 1000000007 the //#define P 10000007 - #defineMOD2 1000000000 - #definell Long Long - #defineLL Long Long + #defineEPS 1e-9 - #defineMaxi (a) (a) > (b)? (a): (b) + #defineMini (A) (a) < (b)? (a): (b) A  at using namespacestd; -  - ll N,m; - ll Dp[n][n]; - ll ans; - ll C[n][n]; in ll Sum[n][n]; -  to voidIni1 () + { -Memset (c,0,sizeof(c)); thememset (SUM,0,sizeof(sum)); *     inti,j; $      for(i=0; i<=n-5; i++){Panax Notoginsengc[i][0]=c[i][i]=1; -     } the      for(i=2; i<=n-5; i++){ +          for(j=1; j<i;j++){ AC[i][j]= (c[i-1][j-1]+c[i-1][J])%MoD; the         } +     } -     //For (i=1;i<=m-5;i++) { $     //For (j=0;j<=i;j++) { $     //printf ("i=%d j=%d c=%i64d\n", I,j,c[i][j]); -     //    } -     //} the      for(i=1; i<=n-5; i++){ -sum[i][0]=1;Wuyi          for(j=1; j<=i;j++){ theSum[i][j]= (sum[i][j-1]+C[I][J])%MoD; -         } Wu     } - } About  $ voidINI () - { -Memset (DP,0,sizeof(DP)); -ans=0; Adp[0][0]=1; + } the  - voidSolve () $ { the     intI,j,k,o; theI=1; the      for(j=1; j<=m;j++){ thedp[i][j]=C[m][j]; -     } in      for(i=2; i<=n;i++){ the          for(j=1; j<=m;j++){ theDp[i][j]= (dp[i-1][j]* (sum[j][j]-1) )%MoD; About              for(k=1; k<j;k++){ theo=j-K; theDp[i][j]= (dp[i][j]+ (dp[i-1][k]* (Sum[k][k])) (%mod) * (C[m-k][o]))%MoD; the             } +         } -     } the }Bayi  the void  out() the { -     //int i,j; -  the     //For (i=1;i<=n;i++) { the     //For (j=1;j<=m;j++) { the      //printf ("i=%d j=%d dp=%i64d\n", I,j,dp[i][j]); the      //  } -    // } theAns= (Dp[n][m])%MoD; theprintf"%i64d\n", ans); the }94  the intMain () the { the ini1 ();98    //freopen ("data.in", "R", stdin); About     //freopen ("Data.out", "w", stdout); -     //scanf ("%d", &t);101     //for (int ccnt=1;ccnt<=t;ccnt++)102    //While (t--)103      while(SCANF ("%i64d%i64d", &n,&m)! =EOF)104     { the ini ();106 solve ();107          out();108     }109  the     return 0;111}

Bestcoder Round #25 1002 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.