• 【扩展欧几里得】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)如果行则为答案。

     1 //
     2 //by coolxxx
     3 //#include<bits/stdc++.h>
     4 #include<iostream>
     5 #include<algorithm>
     6 #include<string>
     7 #include<iomanip>
     8 #include<map>
     9 #include<memory.h>
    10 #include<time.h>
    11 #include<stdio.h>
    12 #include<stdlib.h>
    13 #include<string.h>
    14 //#include<stdbool.h>
    15 #include<math.h>
    16 #define min(a,b) ((a)<(b)?(a):(b))
    17 #define max(a,b) ((a)>(b)?(a):(b))
    18 #define abs(a) ((a)>0?(a):(-(a)))
    19 #define lowbit(a) (a&(-a))
    20 #define sqr(a) ((a)*(a))
    21 #define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
    22 #define mem(a,b) memset(a,b,sizeof(a))
    23 #define eps (1e-8)
    24 #define J 10
    25 #define mod 1000000007
    26 #define MAX 0x7f7f7f7f
    27 #define PI 3.14159265358979323
    28 #define N 54
    29 using namespace std;
    30 typedef long long LL;
    31 int cas,cass;
    32 int n,m,lll,ans;
    33 int f[N]={1,0};
    34 LL exgcd(LL a,LL b,LL &x,LL &y)
    35 {
    36     if(!b){x=1,y=0;return a;}
    37     LL d=exgcd(b,a%b,y,x);
    38     y-=a/b*x;
    39     return d;
    40 }
    41 int main()
    42 {
    43     #ifndef ONLINE_JUDGE
    44 //    freopen("1.txt","r",stdin);
    45 //    freopen("2.txt","w",stdout);
    46     #endif
    47     int i,j,k;
    48     LL a,b,c,d,x,y,lcm,ii;
    49     for(i=1;i<45;i++)
    50         f[i+1]=f[i-1]+f[i];
    51     for(scanf("%d",&cas);cas;cas--)
    52 //    for(scanf("%d",&cas),cass=1;cass<=cas;cass++)
    53 //    while(~scanf("%s",s+1))
    54 //    while(~scanf("%d",&n))
    55     {
    56         scanf("%d",&n);
    57         for(k=45;k && f[k]>n;k--);
    58         if(f[k]==n)
    59         {
    60             puts("1 1");
    61             continue;
    62         }
    63         for(i=k;i>2;i--)
    64         {
    65             a=f[i];b=f[i-1];c=n;
    66             lcm=a*b;
    67             d=exgcd(a,b,x,y);
    68             x=x%b+b;
    69             y=(1-x*a)/b;
    70             x*=c;y*=c;
    71             if(y<=0)
    72             {
    73                 ii=(y-a+1)/(-a);
    74                 y+=ii*a;
    75                 x-=ii*b;
    76             }
    77             while((x-b)>=(y+a))x-=b,y+=a;
    78             if(x<=0 || y<=0 || y>x)continue;
    79             printf("%I64d %I64d
    ",y,x);
    80             break;
    81         }
    82     }
    83     return 0;
    84 }
    85 /*
    86 //
    87 
    88 //
    89 */
    View Code
  • 相关阅读:
    Java实现数组去除重复数据的方法详解
    java枚举和constant使用区别
    如何健壮你的后端服务
    entityframework学习笔记--001
    MongoDB配置服务--MongoDB安装成为windows服务
    MongoDB基础入门003--使用官方驱动操作mongo,C#
    MongoDB基础入门002--基本操作,增删改查
    MongoDB基础入门001--安装
    webapi的返回类型,webapi返回图片
    C#异步下载文件--基于http请求
  • 原文地址:https://www.cnblogs.com/Coolxxx/p/5807097.html
Copyright © 2020-2023  润新知