HDU 5155 Harry And Magic Box (combined mathematics + rejection)

Source: Internet
Author: User
Tags bitset

HDU 5155 Harry And Magic Box (combined mathematics + rejection)
Problem DescriptionOne 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 shoshould output the answer mod 1000000007.
InputThere 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 .
OutputFor each test case, just output one line that contains an integer indicating the answer.
Sample Input

1 12 22 3

Sample Output
1725 rejection + combination knowledge: Thanks to fan Shen for his question solving and clicking the open link, we must first ensure that each line has 1, which is the premise. To obtain the answer, we can enumerate that all columns I are 0 (total C (m, I) options), and then consider the (m-I) locations left in each row, we can place the 01 (2 ^ (m-I) method at will, but to ensure that each row has 1, we need to subtract 1. At this time, it is (2 ^ (m-I) -1 ). then there are n rows, so sum [I] = (2 ^ (m-I)-1) ^ n, and then we need to remove something from it, it means that the column I is only 0 (note that the column I is not only. Ans = sum [0]-sum [1] + sum [2] ..
#include
  
   #include
   
    #include#include
    
     #include
     
      #include
      
       #include
       
        #include
        
         #include
         #include
          
           #include
           
            using namespace std;#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )#define CLEAR( a , x ) memset ( a , x , sizeof a )typedef long long LL;typedef pair
            
             pil;const int maxn=55;const int MOD=1000000007;LL c[maxn][maxn];//2^(m-i)-1;int n,m;void init(){ REPF(i,1,50) { c[i][0]=c[i][i]=1; REPF(j,1,i-1) c[i][j]=(c[i-1][j]+c[i-1][j-1])%MOD; }}LL pow_mod(LL a,int b){ a%=MOD; LL ans=1; while(b) { if(b&1) ans=ans*a%MOD; a=a*a%MOD; b>>=1; } return ans;}int main(){ init(); while(~scanf("%d%d",&n,&m)) { LL ans=0; REPF(i,0,m) { if(i&1) ans=(ans-(pow_mod((1LL<<(m-i))-1,n)*c[m][i])%MOD+MOD)%MOD; else ans=(ans+(pow_mod((1LL<<(m-i))-1,n)*c[m][i])%MOD)%MOD; } printf("%I64d\n",ans); } return 0;}
            
           
          
        
       
      
     
    
   
  

DP Syntax: dp [I] [j] indicates that the number of solutions for the first j column in the first I row has 1. Dp [I] [k] + = dp [I-1] [j] * c [m-j] [k-j] * c [j] [t-(k-j )]. t: indicates the number of rows selected.
# Include
  
   
# Include
   
    
# Include
    
     
# Include
     
      
# Include
      
       
# Include
       
         # Include
        
          # Include
         # Include
          
            # Include
           
             Using namespace std; # define REPF (I, a, B) for (int I = a; I <= B; ++ I) # define REP (I, n) for (int I = 0; I <n; ++ I) # define CLEAR (a, x) memset (a, x, sizeof a) const int MOD = 1e9 + 7; typedef long LL; LL c [55] [55]; LL dp [55] [55]; int n, m; void init () {for (int I = 0; I <= 51; 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 ;}int main () {init (); while (sca Nf ("% d", & n, & m )! = EOF) {CLEAR (dp, 0); dp [0] [0] = 1; // initialize for (int I = 1; I <= n; I ++) {for (int k = 1; k <= m; k ++) {for (int j = 0; j <= k; j ++) // ensure that k> = j {for (int t = 1; t <= k; t ++) {if (k-j> t) continue; dp [I] [k] = (dp [I] [k] + dp [I-1] [j] * c [m-j] [k-j] % MOD * c [j] [t-(k-j)] % MOD) % MOD ;}}} printf ("% I64d \ n", dp [n] [m] % MOD);} return 0 ;}
           
          
        
       
      
     
    
   
  


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.