HDU 4779 tower defense (thinking + combined mathematics)

Source: Internet
Author: User
Tower defense Time Limit: 4000/2000 MS (Java/others) memory limit: 132768/132768 K (Java/Others) Total submission (s): 474 accepted submission (s): 126

Problem description DRD loves playing computer games, especially tower Defense Games. tower defense is a famous computer game with a number of variations. in general, you are to build some defense towers to guard your territory in this game.
However, in most tower Defense Games, your defending towers will not attack each other. you will see the shells flying through your towers and finally hit the target on your screen. DRD thinks it to be absurd, and he designed a new tower defense game.
In DRD's game, you have two kinds of defending Tower, Heavy Tower and light tower. you can put the tower on a grid with N rows and M columns and each cell in the grid can hold one tower at most. both two kinds of towers can attack the cells in the same column or the same row as it is located in, and your towers may attack each other. moreover, light towers shoshould not be attacked by other towers while heavy towers can be attacked by at most one other tower.
You can put some of your towers (at least one tower) in the grid to build a tower formation satisfying the restriction above. and now, DRD wants you to calculate that how many different Tower formations cocould be designed. note that all the towers of the same type are considered to be identical. while the answer cocould be quite large, you shocould output the number Mod (109 + 7 ).
Input there are multiple test cases in the input. the first line of the input file is an integer t demonstrating the number of test cases. (0 <t <= 200 ).
For each test case, there is only one line containing 4 integers, n, m, p and q, meaning that the grid has n rows and M columns, and you have P heavy towers and Q light towers. you do not have to put all the towers in the grid. (1 <= n, m <= 200, 0 <= p, q <= 200)
 
Output for each test case, output the number of different formations Mod (109 + 7) in a single line.
Sample Input
32 2 0 12 2 2 03 3 2 1
 
Sample output
410144
 
Source2013 Asia Hangzhou Regional Contest

Thought Source: Click to open the link
There are two types of towers: Heavy Tower and light tower. Each tower can attack its row and column. The light tower cannot be attacked, and the heavy tower can be attacked by at most one tower, that is to say, the heavy tower can only be attacked by the heavy tower. In a matrix of N * m, there must be at least one tower and multiple columns.
Q: How many release methods are there for a given p-weight tower and Q-weight tower.

Idea: 1. There are two towers in one row,
2. There are two towers in one column
3. There is only one tower, Heavy Tower or light tower in this row and in the column where the tower is located.
Handle the above three situations one by one:
1. If row I has two towers, and column J has two towers, the column J occupies the total I + 2 * j and column J + 2 * I, share 2 x (I + J) Heavy towers, because one row or one column of Two Towers
2. enumerate the remaining towers. 0 <--> the number of the remaining towers is enumerated.

For 1: There are two towers in the row C (n, I) * C (m, 2 * I) * (2 * I )! /2 ^ I) means to select rows I in N and columns 2 * I in m, and divide the columns 2 * I into I groups, A full arrangement is required, but the group does not need to be fully arranged. So for (2 * I )! /2 ^ I has two repeated towers in the column, C (m-2 * I, j) * C (n-I, 2 * j) * (2 * j )! /2 ^ J) Same principle as above

For 2: There are K towers and M-(2 * I + J) rows in the remaining N-(I + 2 * j) select K points in the column (the existing combination of columns and columns * K !), The maximum K value is P-2 * (I + J) + q. For K towers, the maximum number of heavy towers is B = min (K, P-2 * (I + j, there are at least a = max (0, K-Q. K towers, with a minimum of a and a maximum of B. The method for obtaining a weight tower is (C [k] [0] + C [k] [1]... + C [k] [B])-(C [k] [0] + C [k] [1] +... + C [k] [A-1]);
In the end, you can subtract 1 from the unplaced situation;

Code:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <string>#include <map>#include <stack>#include <vector>#include <set>#include <queue>#define maxn 405#define MAXN 200005#define INF 0x3f3f3f3f#define mod 1000000007#define eps 1e-6const double pi=acos(-1.0);typedef long long ll;using namespace std;ll n,m,ans,cnt,p,q;ll c[maxn][maxn],sum[maxn][maxn],fac[maxn],odd[maxn];void presolve(){    ll i,j;    for(i=0;i<=200;i++) c[i][0]=sum[i][0]=1;    for(i=1;i<=200;i++)    {        for(j=1;j<=i;j++)        {            c[i][j]=c[i-1][j]+c[i-1][j-1];            c[i][j]%=mod;        }    }    for(i=0;i<=200;i++)    {        for(j=1;j<=200;j++)        {            sum[i][j]=sum[i][j-1]+c[i][j];            sum[i][j]%=mod;        }    }    fac[0]=odd[1]=1;    for(i=1;i<=400;i++)    {        fac[i]=(fac[i-1]*i)%mod;        if(i>1&&(i&1)) odd[i]=(odd[i-2]*i)%mod;    }}void solve(){    ll i,j,t,k;    ans=0;    for(i=0; i<=n; i++)    {        for(j=0; j<=m; j++)        {            if(i+2*j>n||(j+2*i)>m||2*(i+j)>p) break ;            if(2*i-1>0) t=odd[2*i-1];            else t=1;            ll res=(((((c[n][i]*c[m][2*i])%mod)*t)%mod)*fac[i])%mod;            if(2*j-1>0) t=odd[2*j-1];            else t=1;            res=((((res*c[m-2*i][j])%mod*c[n-i][2*j])%mod*t)%mod*fac[j])%mod;            ll tp=p-2*(i+j),tq=q,tn=n-(i+2*j),tm=m-(j+2*i);            for(k=0;k<=tp+tq;k++)            {                if(k>tn||k>tm) break ;                ll minp=max(0LL,k-tq),maxp=min(k,tp);                if(minp==0) t=0;                else t=sum[k][minp-1];                ll tmp=((((c[tn][k]*c[tm][k])%mod*fac[k])%mod)*(sum[k][maxp]-t))%mod;                ans=(ans+tmp*res)%mod;            }        }    }    ans=(ans-1+mod)%mod;    printf("%lld\n",ans);}int main(){    ll i,j,t;    presolve();    scanf("%lld",&t);    while(t--)    {        scanf("%lld%lld%lld%lld",&n,&m,&p,&q);        solve();    }    return 0;}/*32 2 0 12 2 2 03 3 2 1*/

 

HDU 4779 tower defense (thinking + combined mathematics)

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.