General permutation and combination counting formula

Source: Internet
Author: User

Among n different elements:

If the number of r entries is sorted in order, the number of r entries is obtained from N, and the number is P (n, R) = (n! )/(N-R )! .

If the order is not taken into consideration, the R combination is called from N. The combination number is C (n, R) = (n! )/[(R! ) * (N-R )! ].

 

The number of factorial operations is large. If the numerator and denominator are directly calculated, the efficiency is low and the switch is easy to overflow.

I. Adopt: Company multiplication R overall Commercial Law

C (n, R) = [(n-r-1)/R] * [(n-r-2)/(r-1)] * ...... * [N/1].

Ii. Binary method

C (J, I) = C (J, I-1) + C (J-1, I-1 ).

Directly recursive C [I] [J] through the double loop of I and J;

Example:

Poj 2249

Because blockcodes is commonly used to compile code, long is selected for the big data type. For this question, the _ int64 type is used (because it is not commonly used, it cannot be wrong)

Review: Define _ int64 (Double underline + int + 64) Output: printf ("i64d \ n", a); i64d !!!!

Binomial showdown
Time limit:1000 ms   Memory limit:65536 K
Total submissions:18112   Accepted:5514

Description

In how many ways can you choose k elements out of n elements, not taking order into account?
Write a program to compute this number.

Input

The input will contain in one or more test cases.
Each test case consists of one line containing two integers n (n> = 1) and K (0 <= k <= N ).
Input is terminated by two zeroes for N and K.

Output

For each test case, print one line containing the required number. This number will always fit into an integer, I. e. It will be less than 231.
Warning: Don't underestimate the problem. the result will fit into an integer-but if all intermediate results arising during the computation will also fit into an integer depends on your algorithm. the test cases will go to the limit.

Sample Input

4 210 549 60 0

Sample output

625213983816

Source

Ulm Local 1997 uses the entire concatenation method C (n, k) = (n-k + 1)/K * (n-k + 2)/(k-1) *...... * (N)/1.
# Include <stdio. h> # include <string. h> # include <stdlib. h >__ int64 work (_ int64 N, _ int64 K) {If (k> n/2) K = n-k; // reduce enumeration (pruning)
_ Int64 A = 1, B = 1; // represents the numerator and denominator int I; for (I = 1; I <= K; I ++) // cyclic K operation {A = A * (N-I + 1); // molecule B = B * I; if (a % B = 0) // indicates that the integer can be divided into {A = A/B; B = 1 ;}return a/B ;}int main () {int N, K; while (scanf ("% d", & N, & K )! = EOF) {If (n = 0) {break;} printf ("% i64d \ n", work (n, k);} return 0 ;}

  

General permutation and combination counting formula

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.