HDU 5155 Harry and Magic Box (combinatorial math + repulsion)

Source: Internet
Author: User

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 Input
1 12 22 3

Sample Output
1725Fanshen + combination knowledge: Thank you for writing the puzzle click Open LinkThe first thing we have to make sure is that each row has 1, which is the premise. And to get the answer, we can enumerate I .columnis 0 (total C (m,i) selection), then consider the remaining (m-i) positions of each line, weyou can nowto arbitrarily place the 2^ (m-i), but in order to ensure that each line has 1 to reduce 1, this timefor (2^ (m-i)-1).Then there are n rows, so sum[i]= (2^ (m-i)-1) ^n, and then we're going fromThere are some things out there that areonly column I is all 0 (note that just I is not the only column)so let it be. Ans=sum[0]-sum[1]+sum[2]..
#include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <string > #include <iostream> #include <queue> #include <cmath> #include <map> #include <stack>  #include <bitset>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& lt;int,int>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;    } printf ("%i64d\n", ans); } return 0;}


HDU 5155 Harry and Magic Box (combinatorial math + repulsion)

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.