I. Tower defense

Source: Internet
Author: User

Here are P heavy towers and Q light towers. Put these towers in the N * m graph. These towers will attack each other in the same column, and the light towers will not be attacked, heavy Tower can withstand attacks from one tower,

Number of question placement methods.

Assume n <m.

You can first enumerate the number of light towers as S. Obviously, the number of methods is C (N, S) * m * (S-1 )*... * (m-S + 1), we can see that the graph is actually reduced to a (n-s) * (m-S) graph.

Then put the Heavy Tower in place. Since the Heavy Tower can withstand the attack of a single tower, DP finds a solution to make dp (I, j, k) the method used to place K repeated towers in the graph of I * j. The conditions are enumerated by the first row of the graph.

It can be divided into three parts:

1. The first line does not put the Heavy Tower dp (I, j, k) + = dp (I-1, j, k)

2. Put a heavy tower in the first line, and there are two situations:

A: The same column does not put the Heavy Tower dp (I, j, k) + = J * dp (I-1, J-1, k-1)

B: Put the same column weight tower dp (I, j, k) + = J * (I-1) * dp (I-2, J-1, K-2)

3. Put two towers in the first line

DP (I, j, k) + = C (J, 2) * dp (I-1, J-2, K-2)

After the DP array is obtained, the total number of methods is segma (0, Q, I) segma (0, P, j) C (n, I) * m *... * (M-I + 1) * dp (n-I, m-I, j)

Because it cannot be left alone, we need to subtract 1 from the end.

The time complexity is K * 200 ^ 3, and K is a constant.

# Include <cmath> # include <cstdio> # include <cstdlib> # include <cassert> # include <cstring> # include <set> # include <map> # include <list> # include <queue> # include <string> # include <iostream> # include <algorithm> # include <functional> # include <stack> # include <bitset> using namespace STD; typedef long ll; # define Inf (0x3f3f3f3f) # define maxn (1000005) # define mod 1000000007 # define ull unsigned long longll C [205] [205], DP [205] [205] [205]; void Init () {for (INT I = 0; I <= 200; ++ I) c [I] [0] = 1; for (INT I = 1; I <= 200; ++ I) {for (Int J = 1; j <= I; + + J) {C [I] [J] = C [I-1] [J] + C [I-1] [J-1]; if (C [I] [J]> = mod) C [I] [J] % = mod ;}}for (INT I = 0; I <= 200; ++ I) for (Int J = 0; j <= 200; ++ J) DP [I] [J] [0] = 1; for (INT I = 1; I <= 200; ++ I) {for (Int J = 1; j <= 200; ++ J) {for (int K = 1; k <= 200; + + k) {// The first row does not take if (I = 2 & J = 2) {int T = 1 ;} DP [I] [J] [k] + = DP [I-1] [J] [k]; // obtain a DP [I] [J] [k] + = J * DP [I-1] [J-1] [k-1] % MOD; // The corresponding column does not take if (DP [I] [J] [k]> = mod) DP [I] [J] [k] % = MOD; if (I> = 2 & K> = 2) DP [I] [J] [k] + = J * (I-1) * DP [I-2] [J-1] [K-2] % MOD; // the corresponding column gets if (DP [I] [J] [k]> = mod) DP [I] [J] [k] % = MOD; // The first line obtains two if (j> = 2 & K> = 2) DP [I] [J] [k] + = C [J] [2] * DP [I-1] [J-2] [K-2] % MOD; if (DP [I] [J] [k]> = mod) DP [I] [J] [k] % = mod ;}}} ll quickpow (LL X, ll y) {ll ans = 1; while (y) {If (Y & 1) {ans = ans * X; If (ANS> = mod) ans % = MOD ;} x * = x; If (x> = mod) x % = MOD; y >>=1;} return ans;} int main () {int t; int N, m, P, Q; Init (); scanf ("% d", & T); While (t --) {scanf ("% d ", & N, & M, & P, & Q); If (n> m) Swap (n, m); int li = min (Q, N ); ll S = 1, ANS = 0; For (INT I = 0; I <= Li; ++ I) {for (Int J = 0; j <= P; ++ J) {ans = ans + C [N] [I] * S % mod * DP [(n-I)] [(m-I)] [J] % MOD; If (ANS> = mod) ans % = MOD;} s = S * (M-I); If (S> = mod) S % = MOD;} -- ans; If (ANS <0) ans + = MOD; printf ("% LLD \ n", ANS);} return 0 ;}

 

I. Tower defense

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.