Luogu3904 三只小猪 (组合数学,第二类斯特林数,高精)

即使\(n<=50\),斯特林数也会爆long long。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define R(a,b,c) for(register int  a = (b); a <= (c); ++ a)
#define nR(a,b,c) for(register int  a = (b); a >= (c); -- a)
#define Max(a,b) ((a) > (b) ? (a) : (b))
#define Min(a,b) ((a) < (b) ? (a) : (b))
#define Fill(a,b) memset(a, b, sizeof(a))
#define Abs(a) ((a) < 0 ? -(a) : (a))
#define Swap(a,b) a^=b^=a^=b
#define ll long long

#define ON_DEBUG

#ifdef ON_DEBUG

#define D_e_Line printf("\n\n----------\n\n")
#define D_e(x)  cout << #x << " = " << x << endl
#define Pause() system("pause")
#define FileOpen() freopen("in.txt","r",stdin);

#else

#define D_e_Line ;
#define D_e(x)  ;
#define Pause() ;
#define FileOpen() ;

#endif

struct ios{
    template<typename ATP>ios& operator >> (ATP &x){
        x = 0; int f = 1; char c;
        for(c = getchar(); c < '0' || c > '9'; c = getchar()) if(c == '-')  f = -1;
        while(c >= '0' && c <= '9') x = x * 10 + (c ^ '0'), c = getchar();
        x*= f;
        return *this;
    }
}io;
using namespace std;

const int N = 1007;

int a[N], b[N];
string add(string A, string B){
    string S;
    Fill(a, 0), Fill(b, 0);
    int lenA = A.size(), lenB = B.size();
    R(i,0,lenA - 1) a[i] = A[lenA - i - 1] ^ '0';
    R(i,0,lenB - 1) b[i] = B[lenB - i - 1] ^ '0';
    int len = Max(lenA, lenB);
    R(i,0,len - 1){
        a[i] += b[i];
        a[i + 1] += a[i] / 10;
        a[i] %= 10;
    }
    if(a[len]) ++len;
    nR(i,len - 1,0) S += a[i] + '0';
    return S;
}
string mul(string A, int B){
    string S;
    int len = A.size();
    Fill(a, 0);
    R(i,0,len - 1) a[i] = A[len - i - 1] ^ '0';
    int res = 0;
    R(i,0,len - 1){
        a[i] = a[i] * B + res;
        res = a[i] / 10;
        a[i] = a[i] % 10;
    }
    while(res){
        a[len++] = res % 10;
        res /= 10;
    }
    nR(i,len - 1, 0) S += a[i] + '0';
    return S;
}
string f[107][107];
int n, m;
int main(){
    //FileOpen();
    int n, m;
    io >> n >> m;
    if(n < m){
        printf("0");
        return 0;
    }
    if(n == m){
        printf("1");
        return 0;
    }
    R(i,1,n){
        f[i][1] = "1";
        R(j,2,m){
            f[i][j] = add(f[i - 1][j - 1], mul(f[i - 1][j], j));
        }
    }
    
    cout << f[n][m];
        
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/bingoyes/p/11258224.html