标签:算法
Undoubtedly you know of the Fibonacci numbers. Starting with842831057
Output
1 12 7
解题:斐波那契第n项:a[n]=f[n-1]*x+f[n]*y; // f[n]:f[1]=0,f[2]=1;的斐波那契数列。枚举n与y看能否整除f[n-1],且除数<=y。x:斐波那契第一项,y:斐波那契第二项。
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #define Max(a,b) (a>b?a:b) using namespace std; #define ll long long int main (void) { int f[1005] , ans ; int y ,x; f[1]=0; f[2]=1; int i=3; for( i=3; i<=46; i++) { f[i]=f[i-1]+f[i-2]; } int T; scanf("%d",&T); while(T--) { scanf("%d",&ans); if(ans==1||ans==2) { printf("1 1\n"); continue; } bool bb=0; for(int i=45 ; i>2&&!bb; i--) for(int ty=1; ty<=1000000; ty++) if(ty*f[i]+f[i-1]>ans) break; else if((ans-ty*f[i])%f[i-1]==0&&(ans-ty*f[i])/f[i-1]<=ty) { y=ty , x=(ans-ty*f[i])/f[i-1] , bb=1; break; } printf("%d %d\n",x,y); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。
HUNAN Interesting Integers(爆力枚举)
标签:算法
原文地址:http://blog.csdn.net/u010372095/article/details/47448259