车站

来源:互联网 发布:中国移动关闭3g网络 编辑:程序博客网 时间:2024/04/20 04:54

这是1998年NOIP复赛提高组的题,有很多别名。

具体算法,可以先枚举,用数学的方法列出来;当然,如果你斐波那契数列掌握得比较好的话,不出20行代码,就可以实现。

众所周知,斐波那契数列递推公式如下:F[n]=F[n-1]+F[n-2](n>=2,F[0]=0,F[1]=1),由此就可以按照题意推出。具体代码实现如下:

#include<iostream>
#include<cstring>
#include<cmath>
#include<cstdio>
#include<algorithm>
using namespace std;
int b[20]={0,1,1};
int main()
{
int a,n,m,x,i,k=0;
scanf("%d%d%d%d",&a,&n,&m,&x);
if(x==1)
{
printf("%d",a);
return 0;
}
for(i=3;i<20;i++)
b[i]=b[i-1]+b[i-2];
if(n>4)
k=(m-(b[n-3]+1)*a)/(b[n-2]-1);
printf("%d",(b[x-1]-1)*k+(b[x-2]+1)*a);
return 0;
}


原创粉丝点击