序列
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 7915 | Accepted: 3562 |
Description
数列A满足An = An-1 + An-2 + An-3, n >= 3
编写程序,给定A0, A1 和 A2, 计算A99
编写程序,给定A0, A1 和 A2, 计算A99
Input
输入包含多行数据
每行数据包含3个整数A0, A1, A2 (0 <= A0, A1, A2 <= 32767)
数据以EOF结束
每行数据包含3个整数A0, A1, A2 (0 <= A0, A1, A2 <= 32767)
数据以EOF结束
Output
对于输入的每一行输出A99的值
Sample Input
1 1 1
Sample Output
69087442470169316923566147
Source
#include<stdio.h>
#include<string.h>
const int MAX=100,M=100000000;
int a[MAX][100];int b[MAX];
void ans(int a0,int a1,int a2)
{
memset(a,0,sizeof(a));
a[0][0]=a0,a[1][0]=a1,a[2][0]=a2,b[2]=0;
for(int i=3;i<MAX;i++)
{
int k;for(k=0;k<=b[i-1];k++)
a[i][k]+=a[i-1][k]+a[i-2][k]+a[i-3][k],a[i][k+1]=a[i][k]/M,a[i][k]%=M;
b[i]=k-(a[i][k]?0:1);
}
int k=b[99];
printf("%d",a[99][k]);
while(k--&&k>=0)
printf("%0.8d",a[99][k]);
printf("\n");
}
int main()
{
int a0,a1,a2;
while(scanf("%d%d%d",&a0,&a1,&a2)!=EOF)
ans(a0,a1,a2);
return 0;
}