典型的高精度题,本着“和平共处五项原则”,我抄了一下ZXPN的代码,不过略加改进,中间也出了点问题,忘了算fac[0]了。

  1 #include<stdio.h>
  2 #include<string.h>
  3 char dp[105][105][500],fac[105][200];
  4 void add(char *s1,char *s2,char *s3)
  5 {//s3 = s1 + s2
  6     int len1 = strlen(s1);
  7     int len2 = strlen(s2);
  8     int a[10000];
  9     int k = len1 > len2 ? len1 : len2;
 10     int i,j;
 11     for(i = 0;i <= k;i++)
 12         a[i] = 0;
 13     for(i = len1 - 1,j = k;i >= 0;i--,j--)
 14     {
 15         a[j] = s1[i] - '0';
 16     }
 17     for(i = len2 - 1,j = k;i >= 0;i--,j--)
 18     {
 19         a[j] = a[j] + s2[i] - '0';
 20         a[j - 1] = a[j - 1] + a[j] / 10;
 21         a[j] %= 10;
 22     }
 23     if(a[j] >= 10)
 24     {
 25         a[j - 1] = a[j] / 10;
 26         a[j] %= 10;
 27     }
 28     i = 0;
 29     if(a[0] == 0)
 30         i = 1;
 31     for(j = i;j <= k;j++)
 32         s3[j - i] = a[j] + '0';
 33     s3[j - i] = '\0';
 34 }
 35 void mul(char *s1,char *s2,char *s3)
 36 {//s3 = s1 * s2
 37     int len1 = strlen(s1);
 38     int len2 = strlen(s2);
 39     int a[10000];
 40     int k = len1 + len2 - 1;
 41     int i,j;
 42     for(i = 0;i <= k;i++)
 43         a[i] = 0;
 44     for(i = len1 - 1;i >= 0;i--)
 45     {
 46         for(j = len2 - 1;j >= 0;j--)
 47         {
 48             a[i + j + 1] += (s1[i] - '0') * (s2[j] - '0');
 49             a[i + j] += a[i + j + 1] / 10;
 50             a[i + j + 1] %= 10;
 51         }
 52     }
 53     if(a[1] >= 10)
 54     {
 55         a[0] = a[1] / 10;
 56         a[1] %= 10;
 57     }
 58     i = 0;
 59     if(a[0] == 0)
 60         i = 1;
 61     for(j = i;j <= k;j++)
 62         s3[j - i] = a[j] + '0';
 63     s3[j- i] = '\0';
 64 }
 65 void facmul(char *s1,int n,char *s2)
 66 {//s2 = s1 * n
 67     int l,k,i,j,t,f=0;
 68     l = strlen(s1);
 69     k = l + 2;//本题n最大为100
 70     for(i = 0; i < k; i++)
 71         s2[i] = 0;
 72     for(i = l-1,j = k-1; i >= 0; i--,j--)
 73     {
 74         t = (s1[i]-'0') * n;
 75         s2[j] += t % 10;
 76         s2[j-1] += t / 10;
 77         if(s2[j] > 9)
 78             s2[j-1] += s2[j]/10,s2[j] %= 10;
 79     }
 80     if(s2[1] > 9)
 81         s2[0] += s2[1]/10,s2[1] %= 10;
 82     while(s2[f] == 0)
 83         f++;
 84     for(i = 0; i < k-f; i++)
 85         s2[i] = s2[i+f]+'0';
 86     s2[k-f] = '\0';
 87 }
 88 int main()
 89 {
 90     int i,j,a,b,n=1;
 91     strcpy(fac[0],"1");
 92     for(i = 1; i <= 100; i++)
 93         facmul(fac[i-1],i,fac[i]);
 94     for(i=0;i<=100;i++)
 95         strcpy(dp[i][0],"1");
 96     for(i=1;i<=100;i++)
 97         for(j=1;j<=i;j++)
 98             if(i==j)
 99                 strcpy(dp[i][j],dp[i][j-1]);
100             else
101                 add(dp[i][j-1],dp[i-1][j],dp[i][j]);
102     for(i = 0;i <= 100;i++)
103         for(j = 0;j <= i;j++)
104         {
105             mul(dp[i][j],fac[i],dp[i][j]);
106             mul(dp[i][j],fac[j],dp[i][j]);
107         }
108     while(~scanf("%d%d",&a,&b),a||b)
109     {
110         printf("Test #%d:\n",n++);
111         if(a < b)
112             printf("0\n");
113         else
114             printf("%s\n",dp[a][b]);
115     }
116     return 0;
117 }