HUNAN Interesting Integers(爆力枚举)

Undoubtedly you know of the Fibonacci numbers. Starting with

F1 = 1 and F2 = 1, every next number is the sum of the two

previous ones. This results in the sequence 1, 1, 2, 3, 5, 8, 13, . . ..

Now let us consider more generally sequences that obey the

same recursion relation

Gi = Gi?1 + Gi?2 for i > 2

but start with two numbers G1 ≤ G2 of our own choice. We shall

call these Gabonacci sequences. For example, if one uses G1 = 1

and G2 = 3, one gets what are known as the Lucas numbers:

1, 3, 4, 7, 11, 18, 29, . . .. These numbers are – apart from 1 and 3 –

different from the Fibonacci numbers.

By choosing the ?rst two numbers appropriately, you can get

any number you like to appear in the Gabonacci sequence. For

example, the number n appears in the sequence that starts with 1

and n ? 1, but that is a bit lame. It would be more fun to start with numbers that are as small

as possible, would you not agree?

Input

On the ?rst line one positive number: the number of test cases, at most 100. After that per test

case:

? one line with a single integer n (2 ≤ n ≤ 109

): the number to appear in the sequence.

Output

Per test case:

? one line with two integers a and b (0 < a ≤ b), such that, for G1 = a and G2 = b,

Gk = n for some k. These numbers should be the smallest possible, i.e., there should be

no numbers a

0 and b

0 with the same property, for which b

0 < b, or for which b

0 = b and

a

0 < a.

Sample in- and output

Input

5

89

123

1000

1573655

842831057

Output

1 1

1 3

2 10

985 1971

2 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;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-12-20 02:03:08

HUNAN Interesting Integers(爆力枚举)的相关文章

Benelux Algorithm Programming Contest 2014 Final ACM-ICPC Asia Training League 暑假第一阶段第二场 E. Excellent Engineers-单点更新、区间最值-线段树 G. Growling Gears I. Interesting Integers-类似斐波那契数列-递推思维题

先写这几道题,比赛的时候有事就只签了个到. E. Excellent Engineers 传送门: 这个题的意思就是如果一个人的r1,r2,r3中的某一个比已存在的人中的小,就把这个人添加到名单中. 因为是3个变量,所以按其中一个变量进行sort排序,然后,剩下的两个变量,一个当位置pos,一个当值val,通过线段树的单点更新和区间最值操作,就可以把名单确定. 代码: 1 //E-线段树 2 #include<iostream> 3 #include<cstdio> 4 #incl

30:字符环

30:字符环总时间限制: 1000ms 内存限制: 65536kB描述有两个由字符构成的环.请写一个程序,计算这两个字符环上最长连续公共字符串的长度.例如,字符串“ABCEFAGADEGKABUVKLM”的首尾连在一起,构成一个环:字符串“MADJKLUVKL”的首尾连在一起,构成一个另一个环:“UVKLMA”是这两个环的一个连续公共字符串. 输入一行,包含两个字符串,分别对应一个字符环.这两个字符串之间用单个空格分开.字符串长度不超过255,且不包含空格等空白符.输出输出一个整数,表示这两个字

解剖SQLSERVER 第十七篇 使用 OrcaMDF Corruptor 故意损坏数据库(译)

解剖SQLSERVER 第十七篇 使用 OrcaMDF Corruptor 故意损坏数据库(译) http://improve.dk/corrupting-databases-purpose-using-orcamdf-corruptor/ 有时候你必须先作恶,后行善.情况就是 当你想磨练你的数据库修复技能 我现在添加了一个Corruptor 类到OrcaMDF里面 去测试新的RawDatabase 的功能.Corruptor 就跟他的名字一样--他会故意损坏数据库文件 Corruptor 本身

2018-2019 ACM-ICPC Pacific Northwest Regional Contest (Div. 1) Solution

A:Exam Solved. 温暖的签. 1 #include<bits/stdc++.h> 2 3 using namespace std; 4 5 const int maxn = 1e3 + 10; 6 7 int k; 8 char str1[maxn], str2[maxn]; 9 10 int main() 11 { 12 while(~scanf("%d",&k)) 13 { 14 scanf("%s", str1 + 1); 15

UVa - 12664 - Interesting Calculator

先上题目: 12664 Interesting CalculatorThere is an interesting calculator. It has 3 rows of button.• Row 1: button 0, 1, 2, 3, . . . , 9. Pressing each button appends that digit to the end of the display.• Row 2: button +0, +1, +2, +3, . . . , +9. Pressin

URAL 1792. Hamming Code (枚举)

1792. Hamming Code Time limit: 1.0 second Memory limit: 64 MB Let us consider four disks intersecting as in the figure. Each of the three shapes formed by the intersectionof three disks will be called a petal. Write zero or one on each of the disks.

ACM-ICPC北京赛区(2017)网络赛1【模拟+枚举+数组操作】

题目1 : Visiting Peking University 时间限制:1000ms 单点时限:1000ms 内存限制:256MB 描述 Ming is going to travel for n days and the date of these days can be represented by n integers: 0, 1, 2, …, n-1. He plans to spend m consecutive days(2 ≤ m ≤ n)in Beijing. During

URAL 2031. Overturned Numbers (枚举)

2031. Overturned Numbers Time limit: 1.0 second Memory limit: 64 MB Little Pierre was surfing the Internet and came across an interesting puzzle: What is the number under the car? It took some time before Pierre solved the puzzle, but eventually he u

zoj 1008 Gnome Tetravex (dfs+枚举)

Gnome Tetravex Time Limit: 10 Seconds      Memory Limit: 32768 KB Hart is engaged in playing an interesting game, Gnome Tetravex, these days. In the game, at the beginning, the player is given n*n squares. Each square is divided into four triangles m