2009-03-09 00:02:49來源不明

96北市資訊學科能力競賽 序列長度問題(Sequence)

/************************************************************/

#include<stdio.h>
#include<stdlib.h>
#define N 2600
main()
{
 int n;
 while(scanf("%d",&n)==1)
  {
   if(n==1) {printf("1\n");continue;}
   int ans[2603]={0},temp[2603]={0};
   int a,b,c,d;
   temp[0]=n;
   ans[0]=n;
   for(a=2;a<n;a++)
    {
     for(b=0;b<N;b++)
      temp[b]=temp[b]*(n-a+1);
     for(b=0;b<N;b++)
     if(temp[b]>=10)
      {
       temp[b+1]=temp[b+1]+temp[b]/10;
       temp[b]=temp[b]%10;
      }
     for(b=0;b<N;b++)
      ans[b]=ans[b]+temp[b]*a;
    }
    for(b=0;b<N;b++)
      temp[b]=temp[b]*n;
     for(b=0;b<N;b++)
      ans[b]=ans[b]+temp[b]; 
    for(a=0;a<N;a++)
     if(ans[a]>=10)
      {
       ans[a+1]=ans[a+1]+ans[a]/10;
       ans[a]=ans[a]%10;
      }
     for(a=N-1;a>=0;a--)
      {
       if(ans[a]!=0)
        {
         for(b=a;b>=0;b--)
          printf("%d",ans[b]);
          break;
        }
      }
    printf("\n");
  }
  return 0;
}