Combination number problem (combination number formula conversion + prefix sum)

Title Portal

The definition of the number of combinations
gives you n numbers, and takes out m numbers from them. There are a total of C n m and C n m = n! / (M! * (Nm)!).
Important formula
C n m = C n-1 m + C n-1 m-1

The main idea of ​​the
data is that there are t sets of data, giving you n, m, k. Ask you how many pairs of C i j can divide k for all i ∈ [0, n], j ∈ [0, min (i, m)] .
t <= 1e4, n, m <= 2000;

Ideas
violence running time complexity is O (TN 2 ), it times out, so we need to find a way to time complexity is O (1) method of inquiry.
See the formula above, C n m = C n-1 m + C n-1 m-1 , and then look at the data range, n, m are within 2000, then we can pre-process a 2000 * 2000 matrix Come out, store C i j inside , this operation only needs 4e6 time, and then for each t, ​​O (1) query.

#include<bits/stdc++.h>
using namespace std;
const int N=2e3+5;
const int inf=0x7fffffff;
const int mod=1e9+7;
const int eps=1e-6;
typedef long long ll;
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
//#define int long long
#define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
#define endl '\n'
int t,k,n,m;
int c[N][N],s[N][N];
void fun()
{
    c[1][1]=1;
    for(int i=0;i<=2000;i++)
        c[i][0]=1;
    for(int i=2;i<=2000;i++)
    {
        for(int j=1;j<=i;j++)
        {
            c[i][j]=(c[i-1][j]+c[i-1][j-1])%k;
        }
    }
    for(int i=2;i<=2000;i++)
    {
        for(int j=1;j<=i;j++)
        {
            s[i][j]=s[i-1][j]+s[i][j-1]-s[i-1][j-1];
            if(!c[i][j])
                s[i][j]+=1;
        }
        s[i][i+1]=s[i][i];
    }
}
signed main()
{
    IOS;
    cin>>t>>k;
    fun();
    while(t--)
    {
        cin>>n>>m;
        if(m>n)
            m=n;
        cout<<s[n][m]<<endl;
    }
}

Published 93 original articles · won praise 9 · views 4203

Guess you like

Origin blog.csdn.net/Joker_He/article/details/104806781