题目链接:

  http://codeforces.com/gym/100526

  http://acm.hunnu.edu.cn/online/?action=problem&type=show&id=11672&courseid=0

题目大意:

  给定任意一个N,(N<=109)求斐波那契—卢卡斯数列的前两项A和B。(先满足B最小再满足A最小,A<=B)

  斐波那契—卢卡斯数列是斐波那契数列的推广,斐波那契数列f[0]=0,f[1]=1,斐波那契—卢卡斯数列f[0]=A,f[1]=B。

  二者均满足f[i]=f[i-1]+f[i-2],i>=2。

题目思路:

  【数论】【扩展欧几里得】

  首先如果数列S是斐波那契数列,则A*S,S+S也满足f[i]=f[i-1]+f[i-2]。

  那么考虑A=1,B=0的斐波那契—卢卡斯数列S1,为第一个数对最终答案的影响。

  同样,A=0,B=1的斐波那契—卢卡斯数列S2,为第二个数对最终答案的影响。

  容易得到这两个数列是错位的斐波那契数列

  S1=1,0,1,1,2,3,5...

  S2=0,1,1,2,3,5,8...

  S2[i]=S1[i+1].

  而把S1*A+S2*B如果能含有N,则A B的最小解即为所求。

  所以只需要求出斐波那契数列的前45项(109内),接下来就是枚举N是由斐波那契数列中哪两个相邻的数分别乘A和B得到的。

  即A*f[i]+B*f[i-1]=N。可以对f[i],f[i-1]扩展欧几里得,求出对应的A和B,看看能否把X,Y调成满足题意得(0<B<=A)如果行则为答案。

 //
//by coolxxx
//#include<bits/stdc++.h>
#include<iostream>
#include<algorithm>
#include<string>
#include<iomanip>
#include<map>
#include<memory.h>
#include<time.h>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//#include<stdbool.h>
#include<math.h>
#define min(a,b) ((a)<(b)?(a):(b))
#define max(a,b) ((a)>(b)?(a):(b))
#define abs(a) ((a)>0?(a):(-(a)))
#define lowbit(a) (a&(-a))
#define sqr(a) ((a)*(a))
#define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
#define mem(a,b) memset(a,b,sizeof(a))
#define eps (1e-8)
#define J 10
#define mod 1000000007
#define MAX 0x7f7f7f7f
#define PI 3.14159265358979323
#define N 54
using namespace std;
typedef long long LL;
int cas,cass;
int n,m,lll,ans;
int f[N]={,};
LL exgcd(LL a,LL b,LL &x,LL &y)
{
if(!b){x=,y=;return a;}
LL d=exgcd(b,a%b,y,x);
y-=a/b*x;
return d;
}
int main()
{
#ifndef ONLINE_JUDGE
// freopen("1.txt","r",stdin);
// freopen("2.txt","w",stdout);
#endif
int i,j,k;
LL a,b,c,d,x,y,lcm,ii;
for(i=;i<;i++)
f[i+]=f[i-]+f[i];
for(scanf("%d",&cas);cas;cas--)
// for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
// while(~scanf("%s",s+1))
// while(~scanf("%d",&n))
{
scanf("%d",&n);
for(k=;k && f[k]>n;k--);
if(f[k]==n)
{
puts("1 1");
continue;
}
for(i=k;i>;i--)
{
a=f[i];b=f[i-];c=n;
lcm=a*b;
d=exgcd(a,b,x,y);
x=x%b+b;
y=(-x*a)/b;
x*=c;y*=c;
if(y<=)
{
ii=(y-a+)/(-a);
y+=ii*a;
x-=ii*b;
}
while((x-b)>=(y+a))x-=b,y+=a;
if(x<= || y<= || y>x)continue;
printf("%I64d %I64d\n",y,x);
break;
}
}
return ;
}
/*
// //
*/

【扩展欧几里得】BAPC2014 I Interesting Integers (Codeforces GYM 100526)的更多相关文章

  1. 【最短路】BAPC2014 B Button Bashing (Codeforces GYM 100526)

    题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...

  2. 【线段树】BAPC2014 E Excellent Engineers (Codeforces GYM 100526)

    题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...

  3. 【宽搜】BAPC2014 J Jury Jeopardy (Codeforces GYM 100526)

    题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...

  4. 【模拟】BAPC2014 G Growling Gears (Codeforces GYM 100526)

    题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...

  5. interesting Integers(数学暴力||数论扩展欧几里得)

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAwwAAAHwCAIAAACE0n9nAAAgAElEQVR4nOydfUBT1f/Hbw9202m0r8

  6. Intel Code Challenge Final Round (Div. 1 + Div. 2, Combined) C.Ray Tracing (模拟或扩展欧几里得)

    http://codeforces.com/contest/724/problem/C 题目大意: 在一个n*m的盒子里,从(0,0)射出一条每秒位移为(1,1)的射线,遵从反射定律,给出k个点,求射 ...

  7. UVA 12169 Disgruntled Judge 枚举+扩展欧几里得

    题目大意:有3个整数 x[1], a, b 满足递推式x[i]=(a*x[i-1]+b)mod 10001.由这个递推式计算出了长度为2T的数列,现在要求输入x[1],x[3],......x[2T- ...

  8. UVA 10090 Marbles 扩展欧几里得

    来源:http://www.cnblogs.com/zxhl/p/5106678.html 大致题意:给你n个球,给你两种盒子.第一种盒子每个盒子c1美元,可以恰好装n1个球:第二种盒子每个盒子c2元 ...

  9. POJ 1061 青蛙的约会 扩展欧几里得

    扩展欧几里得模板套一下就A了,不过要注意刚好整除的时候,代码中有注释 #include <iostream> #include <cstdio> #include <cs ...

随机推荐

  1. Django模型之Meta选项详解

    Django模型类的Meta是一个内部类,它用于定义一些Django模型类的行为特性.而可用的选项大致包含以下几类 abstract 这个属性是定义当前的模型是不是一个抽象类.所谓抽象类是不会对应数据 ...

  2. Java批量文件打包下载zip

    网上看了很多,本文使用ant.jar中的org.apache.tools.zip,页面用js表单提交 代码供参考: ACTION: /* * 另存为 */ @RequestMapping(" ...

  3. 处理移动端click事件300ms延迟的好方法—FastClick

    下载地址:https://github.com/ftlabs/fastclick 1.click事件为什么有延迟? “...mobile browsers will wait approximatel ...

  4. android 蓝牙4.0 开发介绍

    最近一直在研究一个蓝牙功能 由于本人是菜鸟  学起来比较忙 一直搞了好久才弄懂 , 网上对蓝牙4.0也就是几个个dome 抄来抄去,全是英文注解 , 对英语不好的朋友来说 真是硬伤 , 一些没必要的描 ...

  5. ASP+MYSQL的配置及乱码解决

    TempStr = "driver={MySQL ODBC 3.51 Driver};database="&strDB&";server="&a ...

  6. iOS9 application:application openURL: sourceApplication: annotation: 方法不执行

    - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url NS_DEPRECATED_IOS(2_0, 9 ...

  7. C# string转int

    1,int转成string用toString 或者Convert.toString()如下 例如:int varInt = 1; string varString = Convert.ToString ...

  8. windows编程中 一些前缀区分 IDR和IDD

    IDC_:控件的ID命名前缀(Control) IDM_:菜单的ID命名前缀(Menu) IDD_:对话框的ID命名前缀(Dialog) IDR_:资源的ID命名前缀(Resource) IDS_:字 ...

  9. IPython学习笔记

    IPython 前言 Life is short, you need Python 最近开始学习数据挖掘,需要使用到Python,其实大学时代就有接触过Python,但是却始终没有系统的进行学习过. ...

  10. newman安装时遇到问题的解决

    npm安装newman时系统提示需要安装.net framwork环境 通过查询文档安装visual studio express: 于是安装visual studio 2012 express. 安 ...