Algorithm competition-Training Guide Chapter 2-2.3 _ ultraviolet A 11806

Source: Internet
Author: User

During this period of time, I have always encountered such a question, that is, the question of the so-called rejection principle, for example, Chapter 1.24 ...... I have to check it out later ).


I did my own case-based discussion on this topic. I also forgot what Lucas was used and didn't write it right at the end. When I read the problem, I collapsed again.

The topic is described as follows: Put a Lala member in an N * M matrix, but the Lala Member here can think of something like a stone. The question is imagined as a stone. Ask you, there must be at least one stone around you. Ask how many possibilities are there.


Imagine there are four sets: the first a represents the first row without stones, B represents the last row without stones, C represents the first column without stones, and D represents the last column without stones. Then the question is transformed into: under the condition that the complete set is neither a nor B, C, or D.

Then we use the law of content rejection, that is, S-A-B + A cross B, which is a simple law of content rejection. Note that Subtraction is used for odd numbers and addition is used for even numbers. This is useful for general enumeration.


Paste the code and review it frequently:

# Include <stdio. h> # include <string. h >#include <iostream >#include <string> using namespace STD; const int maxn = 511; const int mod = 1000000 + 7; int n, m, K; long long C [maxn] [maxn]; void Init () {memset (C, 0, sizeof (c); C [0] [0] = 1; // this should be noted, because the initialization is 0, and the actual value is 1, for (INT I = 1; I <maxn; I ++) // although C [0] [0] is assigned a value of 1, remind yourself. {c [I] [0] = C [I] [I] = 1; for (Int J = 1; j <I; j ++) {c [I] [J] = (C [I-1] [J] + C [I-1] [J-1]) % mod ;}} /* For (INT I = 0; I <10; I ++) {for (Int J = 0; j <I; j ++) {printf ("C [% d] [% d] = % d \ n", I, j, C [I] [J]);} */} int main () {int t; Init (); scanf ("% d", & T); For (INT case = 1; Case <= T; case ++) {scanf ("% d", & N, & M, & K); int sum = 0; For (INT I = 0; I <(1 <4); I ++) {int r = N; int c = m; int B = 0; if (I & 1) {r --; B ++;} if (I & 2) {r --; B ++;} if (I & 4) {c --; B ++ ;} if (I & 8) {c --; B ++;} If (B & 1) {sum = (Sum-C [R * C] [k] + mod) % MOD;} else {sum = (sum + C [R * C] [k]) % MOD;} printf ("case % d: % d \ n ", case, sum % mod);} // system ("pause"); 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.