Noip2016day2 Combinatorial number problem problem

Source: Internet
Author: User

Title Description

The number of combinations represents the number of programs that select m items from n items. For example, select two items from three items (1,3), (2,3), and three options to choose from. Based on the definition of combinatorial number, we can give a general formula for calculating the number of combinations:

where n! = 1x2x Xn

Shallot would like to know if given n,m and K, for all 0 <= i <= n,0 <= j <= min (i,m) How many pairs (i,j) satisfy is a multiple of K.

Input/output format

Input format:

The first line has two integer t,k, where T represents the total number of test data for the test point, and the meaning of K is "problem description".

Next T line two integers per line n,m, where the meaning of n,m see "Problem description".

Output format:

T line, one integer per line represents the answer.

Input/Output sample

Input Sample # # :

1 2

7 ·

Output Example # # :

1

Input Example # # :

2 5

4 5

6 7

Output Example # # :

0

7

Description

"Sample 1 description"

In all possible cases, there is only a multiple of 2.

"Subtasks"

Ideas

This is a number proposition.

First, we need to know the general recursive formula of combinatorial number, and its recurrence formula is the same as the Yang Hui Triangle.

C[I][J]=C[I-1][J-1]+C[I-1][J]

(Explanation: C[i][j] is the number of options for selecting J pieces from the I item. If item I is not selected, the number of programmes becomes C[I-1][J], if Item I is selected, the number of programmes becomes c[i-1][j-1], the total programme number is the sum of the number of programmes in two cases)

In order not to explode long long, each time to find C[i][j] after the first model K

In order to save time, the two-dimensional summation, and finally directly find the answer

#include <iostream>using namespacestd;intn,m,t,k;intc[2001][2001],s[2001][2001];voidGet_c () { for(intI=0; i<= -; i++)    {         for(intj=0; j<=i;j++)        {            if(i==0&&j==0) c[i][j]=1%K; Else{C[i][j]= (c[i-1][j]+c[i-1][j-1])%K; }        }    }}voidget_s () {if(c[0][0]==0(s:0][0]=1;  for(intI=0; i<= -; i++)    {         for(intj=0; j<=i;j++)        {            if(i==0&&j==0)Continue; Else            {                if((i==0&AMP;&AMP;J) | | (i==j)) {S[i][j]=s[i][j-1]; if(c[i][j]==0) s[i][j]++; }                Else if(i&&j==0) {S[i][j]=s[i-1][j]; if(c[i][j]==0) s[i][j]++; }                Else if(i&&j) {S[i][j]=s[i-1][j]+s[i][j-1]-s[i-1][j-1]; if(c[i][j]==0) s[i][j]++; }            }        }    }}intMain () {CIN>>t>>K;    Get_c ();    get_s ();  while(t--) {cin>>n>>m; cout<<s[n][min (n,m)]<<Endl; }}

Noip2016day2 Combinatorial number problem problem

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.