Number of combinations Ⅳ

To link title
meaning of the questions : Enter a, b, seeking C [a] [b] value.
Note that the result may be large, need to use high-precision calculations.
Input format
common line, comprising two integers a and b.
Output format
common line, the output C [a] [b] value.
Data range
1≤b≤a≤5000
Input Sample:
53
sample output:
10
ideas : decomposition of the quality factor:
C [A] [B] ^ p1 = M1 * M2 * P2 ^ ... ^ * PK MK; wherein p1 pk are primes. 1 ~ a ~ in; m1 ~ mk is the number of prime numbers each of which corresponds, in the number of times in a - b is the number - the number of - (a b) of;
code implementation:

#include<iostream>
#include<vector>
#include<algorithm>
using namespace std;
const int N = 5010;
int primes[N], cnt;
int sum[N];
bool st[N];
//筛质数
void get_primes(int n)
{
    for(int i = 2; i <= n; i ++){
        if(!st[i]) primes[cnt ++] = i;
        for(int j = 0; primes[j] <= n / i; j ++){
            st[primes[j] * i] = 1;
            if(i % primes[j] == 0) break;
        }
    }
}
//求出n!里面包含p的个数
int get(int n, int p)
{
    int res = 0;
    while(n){
        res += n / p;
        n /= p;
    }
    return res;
}
//高精度乘法
vector<int> mul(vector<int> a, int b)
{
    vector<int> c;
    int t = 0;
    for(int i = 0; i < a.size(); i ++){
        t += a[i] * b;
        c.push_back(t % 10);
        t /= 10;
    }
    while(t){
        c.push_back(t % 10);
        t /= 10;
    }
    return c;
}

int main()
{
    int a, b;
    cin >> a >> b;
    get_primes(a);
    //枚举一下每一个质数
    for(int i = 0; i < cnt; i ++){
        int p = primes[i];
        sum[i] = get(a, p) - get(b, p) - get(a - b, p);
    }
    vector<int> res;
    res.push_back(1);
    
    for(int i = 0; i < cnt; i ++)
        for(int j = 0; j < sum[i]; j ++)
            res = mul(res, primes[i]);

    for(int i = res.size() - 1; i >= 0; i --)
        printf("%d", res[i]);
    cout << endl;
    return 0;
}

Published 113 original articles · won praise 5 · Views 3310

Guess you like

Origin blog.csdn.net/Satur9/article/details/104347678