斐波那契首项 BAPC2014 I&&HUNNU11589:Interesting Integers
2015-11-21 16:09
351 查看
题目大意:给定一个数n,按斐波那契数规则生成,求最小的b,并使a最小。G1 = a , G2 = b; b >= a.
思路分析:
因为n<10^9,所以斐波那契数列的项数不可能超过46.题目转换为求ax+by=n的最小的x,y。根据欧几里得
思路分析:
因为n<10^9,所以斐波那契数列的项数不可能超过46.题目转换为求ax+by=n的最小的x,y。根据欧几里得
#include <iostream> #include <cmath> #include <string> #include <cstring> #include <algorithm> #include <cstdio> using namespace std; #define maxn 99999999 #define mem(a) memset(a , 0 , sizeof(a)) #define LL __int64 LL fib[65]; void init() { fib[0] = 1; fib[1] = 1; fib[2] = 2; for(int i = 3 ; i < 65 ; i ++) { fib[i] = fib[i-1] + fib[i-2]; } return ; } void gcd2(LL a , LL b , int &d ,LL &x , LL &y) { if(!b) { d = a; x = 1 ; y = 0; } else { gcd2(b , a%b , d , y , x); y -= x*(a/b); } } int main() { int t; LL n; init(); scanf("%d" , &t); while(t-- ) { scanf("%I64d" , &n); int pos; pos = 65; LL ansx = (LL)maxn*1000, ansy = (LL)maxn*1000; LL x , y ;int d; while(pos > 2) { LL a = fib[pos - 2], b = fib[pos - 1]; pos -- ; gcd2(a , b , d , x , y); x *= n/d ; y *= n/d; LL t1 = b / d; LL t2 = a / d; x = (x % t1 + t1) % t1; //找到最小正整数x y = (n - a * x) / b; //对应的y; ////////////并没有看懂这个 ll k = (y-x-1) / (ma+mb);//优化 x += ma*k, y -= mb*k; /* int abc; if(x <= 0) { abc = abs(x / b); if(x % b != 0) abc ++; x = x + abc * b / d; y = y - abc * a / d; } if(y <= 0) { abc = abs(y / a); if(y % a != 0) abc ++; x = x - abc * b / d; y = y + abc * a / d; } if(x < 0 || y + x < 0) continue; if( x == 0) swap(x , y); else if(y != 0 && x > y) swap(x , y); if(x == 0 && y == 0) continue;*/ while(y >= x && y < ansy) { if(y < ansy || (y == ansy && x < ansx) && x > 0) ansx = x , ansy = y; x += t1 , y -= t2; } } if(ansx == 0) ansx = ansy ; printf("%I64d %I64d\n" , ansx ,ansy); } }
相关文章推荐
- Android Studio配置教程
- zabbix搭建与基本配置
- nginx多站点配置
- 关闭error, forbidden warning
- 浅谈在三级城市如何运营一家网络公司
- JSP 、J2EE 、C# 、asp.net 、vb、VB.net、php、 Android,FPGA,MATLAB,QT,程序开发
- DIV+CSS规范命名
- 分享一个基于长连接+长轮询+原生的JS及AJAX实现的多人在线即时交流聊天室
- 关于读取数据库进行数据处理的一些小问题
- jquery效果队列
- 新入此地,请多关照
- 用dblink从源数据库导出expdp文件到目标数据库
- MatConvNet教程----- Win7下编译和配置详解
- cocos2d-x 植入广告(二) 有米横屏、插屏广告和积分墙广告
- PHP文件上传大小限制修改
- HTTP状态码
- FaceBook 扎克伯格还有两个月就当爸爸了 Mark Zuckerberg to take two months paternity leave 翻译 by oralusa.com
- 聚会的目的 동창회의 목적 (2015)
- Map集合中的方法(添加、删除、判断、获取)
- rman backup