题解 P3986 【斐波那契数列】
题意:给定
即要求出关于
显而易见,当
注意到:
-
-5 \cdot 3 + 8 \cdot 2 = 1 -
8 \cdot 5 - 13 \cdot 3 = 1 -
-13 \cdot 8 + 21 \cdot 5 = 1 -
21 \cdot 13 - 34 \cdot 8 = 1 - ……
即
(这里我们定义
所以
代码:
#include<cstdio>
int n,ans;
long long tx,ty,fx,fy,sx,sy;
int main(){
scanf("%d",&n);
for(int p0=1, p1=1, x=0, y=1; p0+p1<=n; p1=p0+p1, p0=p1-p0, x=y-x, y=y-x){
tx=1ll*x*n, ty=1ll*y*n;
fx=tx%p1; if(fx<=0) fx+=p1; fy=ty-(fx-tx)/p1*p0;
sy=ty%p0; if(sy<=0) sy+=p0; sx=tx-(sy-ty)/p0*p1;
if(fy<=0||sx<=0) break;
ans=(ans+(sx-fx)/p1+1)%1000000007;
}
printf("%d",ans);
}