f[i][j]=f[i-j][j]+f[i][j-1],结果很大需要高精度。
//#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> #include<cstring> #include<cstdlib> #include<algorithm> #include<iostream> #include<sstream> #include<cmath> #include<climits> #include<string> #include<map> #include<queue> #include<vector> #include<stack> #include<set> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair<int,int> pii; #define pb(a) push(a) #define INF 0x1f1f1f1f #define lson idx<<1,l,mid #define rson idx<<1|1,mid+1,r #define PI 3.1415926535898 template<class T> T min(const T& a,const T& b,const T& c) {return min(min(a,b),min(a,c)); } template<class T> T max(const T& a,const T& b,const T& c) {return max(max(a,b),max(a,c)); } void debug() { #ifdef ONLINE_JUDGE #elsefreopen("in.txt","r",stdin);//freopen("d:\\out1.txt","w",stdout); #endif } int getch() {int ch;while((ch=getchar())!=EOF) {if(ch!=' '&&ch!='\n')return ch;}return EOF; } const int MAX_LEN=20; const int BASE=100000; struct BigInt {int bit[MAX_LEN];int len;BigInt(int n=0){bit[0]=n;len=1;}BigInt operator + (const BigInt &ant){int next=0;for(int i=0;i<len||i<ant.len;i++){bit[i]=(i<len?bit[i]:0)+(i<ant.len?ant.bit[i]:0)+next;next=bit[i]/BASE;bit[i]%=BASE;}len=max(len,ant.len);if(next)bit[len++]=next;return *this;}void output(){printf("%d",bit[len-1]);for(int i=len-2;i>=0;i--)printf("%05d",bit[i]);printf("\n");} }; BigInt dp[1005][105]; BigInt f(int n,int k) {if(n==0)return BigInt(1);if(k==1)return BigInt(1);if(n<k)return f(n,n);if(dp[n][k].len!=1||dp[n][k].bit[0]!=0)return dp[n][k];dp[n][k]=f(n-k,k)+f(n,k-1);return dp[n][k]; } int main() {int n,k;while(scanf("%d%d",&n,&k)!=EOF){for(int i=0;i<=n;i++)for(int j=0;j<=k;j++)dp[i][j]=BigInt(0);f(n,k);dp[n][k].output();}return 0; }