Stirling number template of the second kind (high precision)

The second type of Stirling number: Divide n different elements into m sets (the number of schemes for putting n different small balls into m identical boxes) and record the number of sets of n
elements that define m equivalence classes Do S(n,m)S(n,m) is the Stirling number of the second kind.
s[n][m]=s[n-1][m-1]+m*s[n-1][m] Consider the nth
element: create a new set, or enter an already opened set
Example: luoguP3904 The three little pigs

#include <bits/stdc++.h>
using namespace std;
#define N 100
int n,m,a[N][N][N];
void add(int x,int y){
    
    
    a[x][y][0]=max(a[x-1][y-1][0],a[x-1][y][0]);//!!!!!
    for(int i=1;i<=a[x][y][0];i++){
    
    
        a[x][y][i]+=a[x-1][y-1][i];
        a[x][y][i]+=y*a[x-1][y][i];
        a[x][y][i+1]+=a[x][y][i]/10;
        a[x][y][i]%=10;
    }
    while(a[x][y][a[x][y][0]+1]){
    
    
        a[x][y][0]++;
        a[x][y][a[x][y][0]+1]+=a[x][y][a[x][y][0]]/10;
        a[x][y][a[x][y][0]]%=10;
    }
        
}
int main(){
    
    
    scanf("%d%d",&n,&m);
    a[1][1][1]=1;
    a[1][1][0]=1;
    for(int i=2;i<=n;i++)
        for(int j=1;j<=i;j++)
            add(i,j);
    if(!a[n][m][0])	printf("0");
    for(int i=a[n][m][0];i>=1;i--)	
        printf("%d",a[n][m][i]);
    return 0;
}

High-precision writing: Note that the length must be updated at the beginning! If it is updated at the end, it will exit when a bit in the middle is 0.

Guess you like

Origin blog.csdn.net/weixin_42754727/article/details/88344303