Interesting Integers Gym - 101512I
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 first 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 first 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
这道题应该是有正规高级算法可以用的吧,我写的介个算法,坑点比较多,数据卡的紧,稍不留神就容易写错
奇葩思路A的,代码:
1 #include <stdio.h> 2 #include <string.h> 3 4 long long INF = 1e9+5; 5 6 //有数据用到了long long,就干脆把所有整数都开成longlong了,丢... 7 int main() 8 { 9 long long t, x, a, b, re1, re2; 10 long long data1, data2, t1, t2; 11 long long aa[55], i, j; 12 aa[1] = 1; 13 aa[2] = 1; 14 for(i=3; i<50; i++) 15 { 16 aa[i] = aa[i-1] + aa[i-2]; 17 } 18 scanf("%lld", &t); 19 20 while(t--) 21 { 22 scanf("%lld", &x); 23 for(i=48; i>=0; i--) 24 { 25 if(aa[i]+aa[i+1]<=x) //这句剪枝不能没有,没有会T, 26 //想一下,这样就可以减少好多没有意义的80000次循环,何乐而不为? 27 { 28 for(j=1; j<=80000; j++) //在不T的条件下尽量扩大范围 29 { 30 if((x-aa[i+1]*j)%aa[i]==0&&(x-aa[i+1]*j)/aa[i]>0&&(x-aa[i+1]*j)/aa[i]<=j) break; 31 } 32 if(j<=80000) break; 33 } 34 } 35 printf("%lld %lld\n", (x-aa[i+1]*j)/aa[i], j); 36 } 37 return 0; 38 }
原文地址:https://www.cnblogs.com/0xiaoyu/p/11435579.html
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 大型项目技术栈第八讲 Redis
- 大型项目技术栈第七讲 Chosen的使用
- 大型项目技术栈第六讲 EasyExcel的使用
- JavaWeb第二讲 重定向与转发 & doGet()与doPost()
- JavaWeb第一讲 Servlet的工作原理及生命周期
- 多系列数据核密度图
- leetcode树之二叉树的层平均值
- 大型项目技术栈第二讲 ES6(ECMAScript 6.0)的使用
- 大型项目技术栈第三讲 ztree的使用
- JavaWeb新手训练经典项目 & 半小时高效开发 & 海量知识点涵盖 = 从这里开始
- Java反射_笔记分享
- Java注解详细总结
- 文档驱动 —— 表单组件(六):基于AntDV的Form表单的封装,目标还是不写代码
- 这就是你日日夜夜想要的docker!!!---------Docker资源控制--Cgroup
- 2020-09-26:请问rust中的&和c++中的&有哪些区别?