3 0 3 1 3 3 0 0
Test #1: 6 Test #2: 18 Test #3: 180
大数运算:
#include <iostream> using namespace std; #include <queue> const int N=400; char store [203][N]; //整数n转化为字符串s void itos(int n,char *s){ int i,j,t[5]; if(n==0){ s[0]='0'; s[1]=0; return; } i=0; while(n){ t[i++]=n%10; n/=10; } i--; j=0; while(i>=0) s[j++]=t[i--]+'0'; s[j]=0; } //计算m*c=res.默认m和c不为0 void mul(char *m,char *c,char *res){ int i,j,len1,len2; len1=strlen(m),len2=strlen(c); int *r=new int[len1+len2+1]; for(i=0;i<=len1+len2;i++) r[i]=0; for(i=0;i<len1;i++) for(j=0;j<len2;j++) r[i+j+1]+=(m[i]-'0')*(c[j]-'0'); for(i=len1+len2-1;i>=1;i--)//处理进位 if(r[i]>9){ int tmp=r[i]/10; r[i]%=10; r[i-1]+=tmp; } for(i=0;i<len1+len2&&!r[i];i++);//处理前导0 if(i==len1+len2){ res[0]='0'; res[1]=0; return; } j=0; while(i<len1+len2)res[j++]=r[i++]+'0'; res[j]=0; delete[] r; } //计算阶乘 void factorial(int n,char *res){ char s[N],t[N]; if(n==0){ res[0]='1'; res[1]=0; } itos(n,res); while(--n>1){ itos(n,s); mul(res,s,t); strcpy(res,t); } } //小数d去除大数m void div(char *m,int d,char *res){ int i,len; int r,tmp; len=strlen(m); r=0;//余数 for(i=0;i<len;i++){ r=r*10+m[i]-'0'; res[i]=r/d+'0';//商 r%=d; } res[len]=0; //忽略前导0 i=0; while(res[i]=='0') i++; if(i!=0) strcpy(res,res+i); } int main() { freopen("C:\\in.txt","r",stdin); int m,n,i; int cnt=0; char r[N],s[N],t[N]; for(i=1;i<=200;i++){ factorial(i,store[i]); } while(scanf("%d %d",&m,&n)!=EOF){ if(m==0&&n==0)break; if(m<n){ printf("Test #%d:\n%d\n",++cnt,0); continue; } i=0; while((s[i]=store[m+n][i])!=0)i++; itos(m+1-n,t); mul(s,t,r); div(r,m+1,t); printf("Test #%d:\n%s\n",++cnt,t); } return 0; }