【扩展欧几里得】BAPC2014 I Interesting Integers (Codeforces GYM 100526)
题目链接:
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)的更多相关文章
- 【最短路】BAPC2014 B Button Bashing (Codeforces GYM 100526)
题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...
- 【线段树】BAPC2014 E Excellent Engineers (Codeforces GYM 100526)
题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...
- 【宽搜】BAPC2014 J Jury Jeopardy (Codeforces GYM 100526)
题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...
- 【模拟】BAPC2014 G Growling Gears (Codeforces GYM 100526)
题目链接: http://codeforces.com/gym/100526 http://acm.hunnu.edu.cn/online/?action=problem&type=show& ...
- interesting Integers(数学暴力||数论扩展欧几里得)
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAAwwAAAHwCAIAAACE0n9nAAAgAElEQVR4nOydfUBT1f/Hbw9202m0r8
- 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个点,求射 ...
- 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- ...
- UVA 10090 Marbles 扩展欧几里得
来源:http://www.cnblogs.com/zxhl/p/5106678.html 大致题意:给你n个球,给你两种盒子.第一种盒子每个盒子c1美元,可以恰好装n1个球:第二种盒子每个盒子c2元 ...
- POJ 1061 青蛙的约会 扩展欧几里得
扩展欧几里得模板套一下就A了,不过要注意刚好整除的时候,代码中有注释 #include <iostream> #include <cstdio> #include <cs ...
随机推荐
- 如果 @s int 把它转成字符,可以这样 cast(@s as varchar)
如果 @s int 把它转成字符,可以这样 cast(@s as varchar)
- 程序从高版本降到2.0,数据集报错 TypedTableBase
错误 命名空间“System.Data”中不存在类型或命名空间名称“TypedTableBase”(是缺少程序集引用吗?) 解决: 该错误出现在自动生成的XXX.Designer.cs里. .NET ...
- MongoDB_1
突然想去看下MongoDB的东西,于是有了这篇文章.其实很早以前就看过一些关于NoSql的文章,还记得当时里面有介绍MongoDB的,多瞅了2眼,并且在Window下安装了MongoDB的驱动,小玩了 ...
- Dictionary的遍历和修改
/// <summary> /// 初始化一个Dic /// </summary> public static void mainTe ...
- 九、C# 合式类型
本章要描述如何最终完善类型声明. 1.重写Ojbect中的成员 重写ToString() 默认情况下,在任何对象上调用 ToString()会返回类的完全限定名称,所以有时候需要重载这个函数, ...
- c++实现的Array数据结构
1.Array.h,Array<T>的定义 template <class T> class Array { protected: T *data; //一个指向数组数据的指针 ...
- 【BZOJ2653】【主席树+二分】middle
Description 一个长度为n的序列a,设其排过序之后为b,其中位数定义为b[n/2],其中a,b从0开始标号,除法取下整. 给你一个长度为n的序列s. 回答Q个这样的询问:s的左端点在[a,b ...
- 24种设计模式--单例模式【Singleton Pattern】
这个模式是很有意思,而且比较简单,但是我还是要说因为它使用的是如此广泛,如此的有人缘,单例就是单一.独苗的意思,那什么是独一份呢?你的思维是 独一份,除此之外还有什么不能山寨的呢?我们举个比较难复制的 ...
- 获取IP城市
新浪的接口 : http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js 多地域测试方法:http://int.dpool.sina. ...
- Eclipse启动Tomcat报错,系统缺少本地apr库
Eclipse启动Tomcat报错,系统缺少本地apr库. Tomcat中service.xml中的设置情况. 默认情况是HTTP协议的值:protocol="HTTP/1.1" ...