HDU-5155 Harry and Magic Box

Source: Internet
Author: User

Problem description
A box is made up of n*m, and there are some glittering gems in the lattice.
Now there is a box from the left to see the past, each line is shining, from the front to see the past, each column is also flashing light.
Q: How many types of gems are in the box?
The answer is likely to be large, so the output answer is 1000000007 modulo.


Enter a description
Multiple sets of input data
One row per set of data, enter two number n m for the size of the box, 0≤n,m≤50


Output description
Each set of data outputs a row, an integer, representing the number of scenarios


Analytical:
This question looks best.
Coder above the puzzle, really is to see the dazed, and later asked a little seniors, just understand how to do.


The main use of the idea of recursion.
Draw a diagram to explain:
Scenario 1:



Scenario 2:



In both cases, the state transfer formula is obtained:

DP[I][J] + = dp[i][j-1]* (2^i-1) + dp[i-k][j-1] * c[k][i] * (2^ (i-k))

#include <cstdio> #include <cstring> #include <cmath> #include <algorithm>using namespace std; typedef __int64 LL;CONST int MOD = 1000000007;const int N = 51;ll c[n][n],dp[n][n],t[n];int n,m;void init () {memset (c,0,si Zeof (c)); C[0][0] = 1;for (int i = 1;i <= N; i++) {c[i][0] = 1;for (int j = 1;j <= i; j + +) C[i][j] = (C[i-1][j-1] + c[ I-1][J])% MOD;} T[0] = 1;for (int i = 1; I <= N; i++) {t[i] = t[i-1] *% MOD;}}  int main () {init (); while (scanf ("%d%d", &n,&m)! = EOF) {for (int i = 0; I <= N; i++) dp[i][1] = 1;for (int j = 0; j <= m; J + +) Dp[1][j] = 1;for (int i = 2; I <= n; i++) {for (int j = 2; J <= M; j + +) {Dp[i][j] = dp[i][j-1] * (T[i]-1)% MOD; for (int k = 1; k < i; k++) {Dp[i][j] = (Dp[i][j] + dp[i-k][j-1] * c[i][k]% mod * t[i-k]% mod)% mod;}} printf ("%i64d\n", Dp[n][m]);} return 0;}


HDU-5155 Harry and Magic Box

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.