http://acm.hdu.edu.cn/showproblem.php?pid=1297

公式的推导:

1)m

2)mff

3)mfff

a:安全序列后加ff或者m,结果仍然安全。

b:不安全序列后加ff可使其安全,虽然mf加f也能得到安全序列,但与a情况重复。

故:公式a[n]=a[n-1]+a[n-2]+a[n-4];

另外:还要考虑大数相加的问题:因为:n=1000时输出结果:

12748494904808148294446671041721884239818005733501580815621713101333980596197474

74433619974245291299822523591089179822154130383839594330018972951428262366519975

47955743099808702532134666561848656816661065088789701201682837073071502397487823

19037

 

1297 Children’s Queue
1 #include<stdio.h>
2 #include<string.h>
3 #include<stdlib.h>
4  #define N 1001
5
6  char str1[N][N];
7  void big_add(char *str1,char *str2,char *str3)
8 {
9 int i,j,temp,k,carry;
10 char ch;
11 int len1=strlen(str1),len2=strlen(str2);
12 k=carry=0;
13 for(i=len1-1,j=len2-1;i>=0,j>=0;i--,j--,++k)
14 {
15 temp=str1[i]-'0'+str2[j]-'0'+carry;
16 carry=temp/10;
17 str3[k]=temp%10+'0';
18 }
19 while(i>=0)
20 {
21 temp=str1[i--]-'0'+carry;
22 str3[k++]=temp%10+'0';
23 carry=temp/10;
24 }
25 while(j>=0)
26 {
27 temp=str2[j--]-'0'+carry;
28 str3[k++]=temp%10+'0';
29 carry=temp/10;
30 }
31 if(carry)
32 str3[k++]=carry+'0';
33 str3[k]='\0';
34 for(i=0,j=k-1;i<j;++i,--j){ ch=str3[i];str3[i]=str3[j];str3[j]=ch;}
35 }
36  int main()
37 {
38 int i,n;
39
40 memset(str,'0',sizeof(str));//初始化a,打表
41 strcpy(str[0],"1");
42 strcpy(str[1],"1");
43 strcpy(str[2],"2");
44 strcpy(str[3],"4");
45 for(i=4;i<=1000;i++)
46 {
47 char te[1001];
48 big_add(str[i-1],str[i-2],te);
49 big_add(te,str[i-4],str[i]);
50 }
51
52 while(scanf("%d",&n)!=EOF)
53 printf("%s\n",str[n]);
54 return 0;
55 }
56
57
58
59
60
61
62
63
64

 

 

3034273 2010-10-04 22:06:21 Accepted 1297 15MS 1140K 1053 B G++

 

 

posted on 2010-10-04 22:51  role  阅读(874)  评论(0编辑  收藏  举报