Permutation and combination of combined mathematics

Source: Internet
Author: User
Tags cmath

Poj1833

Question connection: http://poj.org/problem? Id = 1833

Question: The meaning of the Chinese question is not much to be said, but the next_permutation () function in STL is used.

/* The arrangement function in next_permutation (OP1, OP2) STL. OP1 stores the array header address and OP2 stores the arrangement length. Each time this function is run, OP1 stores the next arrangement of the original arrangement. Return NULL */# include <cstdio> # include <iostream> # include <cmath> # include <cstring> # include <algorithm> # include <cstdlib> using namespace STD; int main () {int K; int M, N; int A [1025]; scanf ("% d", & K); While (k --) {scanf ("% d", & M, & N); For (INT I = 0; I <m; I ++) {scanf ("% d ", A + I) ;}for (INT I = 0; I <n; I ++) {If (next_permutation (A, A + M) = NULL) sort (, A + M) ;}for (INT I = 0; I <m; I ++) {If (! I) printf ("% d", a [I]); else printf ("% d", a [I]);} printf ("\ n ");} return 0 ;}

Poj1850 & poj1496

Question connection: http://poj.org/problem? Id = 1850 http://poj.org/problem? Id = 1496

Assume that a-Z is 1-26, respectively. Then, a lower-case letter segment with a length not greater than 10 is obtained. Enter a character segment to judge the serial number of the segment among all the segments.

Idea: the number of combinations C (m, n), mainly consider extracting N from the number of M.

/**************************** 1. first, we can calculate the sequence numbers of the first segment corresponding to each digit (the number of digits here refers to the length of the character segment). On this basis, the segments with the same digit are an incremental process, the Int num [] = {0, 26, 5311735, 2600, 14950,65780, 230230,657800, 1562275, 3124550,} is calculated. 2. for the ascending order clearly stated in the question, you can use a flag variable to judge and directly use the segment length. When the segment is not ascending, change its length to-13.c( m, n) = C (N-1 m-1) + C (n-1, m); 4. take a large string branch several strings, for example, the string length is 5, then divided into two parts, the first part is the length of 1 to 4 and then sum = C (26, 1) + C (26,2) + C (26,3) + C (26,4 ). the second part corresponds to the number of strings corresponding to each character ************************ *** */# Include <cstdio> # include <iostream> # include <cmath> # include <cstring> # include <algorithm> # include <cstdlib> using namespace STD; int num [] = {5311735,}; int combine (int n, int m) // calculation method of the number of combinations {int ans = 1; for (INT I = 1; I <= m; I ++) ans = ans * (N-I + 1) /I; return ans;} int main () {freopen ("C: \ Users \ Administrator \ Desktop \ in.txt", "r", stdin ); char STR [11]; // Input Segment int Len; // segment length int sum; // sum int P; while (scanf ("% s", STR )! = EOF) {sum = 0; Len = strlen (STR); For (INT I = 1; I <Len; I ++) // find the first segment based on the number of digits {sum + = num [I]; If (STR [I] <STR [I-1]) // In non-ascending order {Len =-1; sum = 0 ;}} if (LEN =-1) {printf ("0 \ n"); continue ;} for (INT I = 0; I <Len; I ++) // separate a piece into multiple parts, which are divided into one {if (I) P = STR [I-1]-'A' + 1; else p = 0; For (Int J = P; j <STR [I]-'A'; j ++) {sum + = combine (25-j, len-i-1) ;}} cout <sum + 1 <Endl;} return 0 ;}

Poj1942

Question connection: http://poj.org/problem? Id = 1942

Question: the question of finding a regular expression is a code of the combination number. The key is that this is a pitfall question. If I use Java to write a question, there will be a loss of precision, and I cannot use int to save it, only double can be used, but you must convert it to int in the output. Another point is that the two input numbers m and n are 32 bits, however, the AC code detection can only be calculated to 514 ,.

Not to mention, it is the implementation code of a combination of formulas.

This question is not recommended for practice.

# Include <cstdio> # include <iostream> # include <cmath> # include <cstring> # include <iomanip> # include <algorithm> # include <cstdlib> using namespace STD; // Mn should be less than 515 double ans, n, m; double mmin, mMax; int main () {cout <fixed <setprecision (0 ); while (CIN> N> m) {If (n = 0 & M = 0) break; mmin = min (m, n ); mMax = max (m, n); ans = 1; for (unsigned I = 1; I <= mmin; I ++) {ans = ans * (mMax + I) /I;} cout <ans <Endl;} return 0 ;}

Nkoj1038 & poj2245

Question connection: http://acm.nankai.edu.cn/p1038.html http://poj.org/problem? Id = 2245

For a sequence of N numbers, select the combination number of C (n, 6) and output the specific combination number.

One question with a large number of PES has been pitted several times. Note that the last group of test data has no line breaks. Just make an extra decision.

# Include <cstdio> # include <iostream> # include <cmath> # include <cstring> # include <algorithm> # include <cstdlib> using namespace STD; void combine (int A [], int N) {int * B = new int [6 + 1]; for (INT I = 0; I <= 6; I ++) {B [I] = I-1; // note here B [0] =-1 is used as the mark for loop judgment} int K = 6; bool flag = true; // locate a valid combination while (B [0] =-1) {If (FLAG) {for (INT I = 1; I <6; I ++) // output the combination of {cout <A [B [I] <"" ;}cout <A [B [6]; cout <Endl; flag = false;} B [k] + +; // Select a new number at the current position if (B [k] = N) // No number is available at the current position, backtracking {B [k --] = 0; continue;} If (k <6) // update the number of the next position for the current position {B [+ k] = B [k-1]; continue;} If (k = 6) Flag = true ;}} int main () {freopen ("C: \ Users \ Administrator \ Desktop \ in.txt", "r", stdin); int N; int A [14]; bool flag = true; while (CIN> N) {If (n = 0) break; If (! Flag) cout <Endl; flag = false; For (INT I = 0; I <n; I ++) {CIN> A [I];} combine (, n);} return 0 ;}

Nkoj1108

Question link http://acm.nankai.edu.cn/p1108.html

# Include <cstdio> # include <iostream> # include <cmath> # include <cstring> # include <iomanip> # include <algorithm> # include <cstdlib> # include <iomanip> using namespace STD; double ans; int n, m; int main () {// cout <fixed <setprecision (0); While (scanf ("% d", & N, & M )! = EOF) {If (n = 0 & M = 0) break; ans = 1; if (M> n/2) M = N-m; // This is the key. Otherwise, it will time out for (INT I = 1; I <= m; I ++) {ans = ans * (N-I + 1) /I;} printf ("%. 0lf \ n ", ANS);} 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.