ACM 数论 Interesting Integers

2024-02-03 04:08

本文主要是介绍ACM 数论 Interesting Integers,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目:

Undoubtedly you know of the Fibonacci numbers. Starting with F_1 = 1F1=1 and F_2 = 1F2=1,every next number is the sum of the two previous ones. This results in the sequence 1, 1, 2, 3, 5, 8, 13, \cdot\cdot\cdot1,1,2,3,5,8,13, .

Now let us consider more generally sequences that obey the same recursion relation

Gi =G_{i-1} +G_{i-2}Gi=Gi1+Gi2 for i>2i>2

but start with two numbers G_1 \le G_2G1G2 of our own choice. We shall call these Gabonacci sequences. For example, if one uses G_1 = 1G1=1and G_2 = 3G2=3, one gets what are known as the Lucas numbers: 1,3,4,7,11,18,29,\cdot\cdot\cdot1,3,4,7,11,18,29, . These numbers are – apart from 11 and 33 – 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 11 and n - 1n1, 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 Format

On the first line one positive number: the number of test cases, at most 100100. After that per test case:

  • one line with a single integer nn (2 \le n \le 10^9)(2n109): the number to appear in the sequence.

Output Format

Per test case:

  • one line with two integers aa and bb (0 < a \le b)(0<ab),such that,for G_1 = aG1=a and G_2 = b, G_k = nG2=b,Gk=n for some kk. These numbers should be the smallest possible, i.e., there should be no numbers a{'}a and b{'}bwith the same property, for which b{'} < bb<b, or for which b{'} = bb=b and a{'} < aa<a.
样例输入
5
89
123
1000
1573655
842831057
样例输出
1 1
1 3
2 10
985 1971
2 7


题意:

类斐波那契数列是改变前两个数生成一个新的数列,问题是给你一个数,让你求出他是以那两个数为首的数列中的数,如果有多组解,求出最小的。

分析:

设前两个数为G(1) G(2),我们不难发现一个规律,G(k)=F(k-2)G(1)+F(k-1)G(2);

那么问题就转化为已知r求满足r=xa+yb的最小一组a,b;

y由于有两个变量要求,我们不妨先设一个变量为自变量,根据自变量求出相应的另一个变量,然后判断是否符合题解;

代码:

#include<stdio.h>
using namespace std;//这里一定要用自己写的数组,不然会超时;
long long f[]={0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987, 1597, 2584, 4181, 6765, 10946, 17711, 28657, 46368, 75025, 121393, 196418, 317811, 514229, 832040, 1346269, 2178309, 3524578, 5702887, 9227465, 14930352, 24157817, 39088169, 63245986, 102334155, 165580141, 267914296, 433494437, 701408733, 1134903170};
int main()
{
    int k,i,j,flag;
    long long a,b,n;//ll形式
    scanf("%d",&k);
    while(k--)
    {
        a=0;
        b=0;
        flag=0;
        scanf("%lld",&n);
        for(b=1;b<=1000000000;b++)//让b从最小的开始,得到的结果自然就是最小的
        {
            for(j=2;j<=45;j++)//根据for(i=3;(f[i]=f[i-1]+f[i-2])<=1000000000;i++)记录下i即可发现i为46
            {
                if(f[j]*b>n)
                    break;
                a=(n-f[j]*b)/f[j-1];
                if((a>0)&&(a<=b)&&(f[j-1]*a+f[j]*b==n))//此处还要在判断一次是否相等,防止出现前面除法运算是的误差出现
                {
                    flag=1;
                    break;
                }
            }
            if(flag==1)
                break;
        }
        printf("%lld %lld\n",a,b);
    }
    return 0;
}

这篇关于ACM 数论 Interesting Integers的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/672944

相关文章

认识、理解、分类——acm之搜索

普通搜索方法有两种:1、广度优先搜索;2、深度优先搜索; 更多搜索方法: 3、双向广度优先搜索; 4、启发式搜索(包括A*算法等); 搜索通常会用到的知识点:状态压缩(位压缩,利用hash思想压缩)。

数论入门整理(updating)

一、gcd lcm 基础中的基础,一般用来处理计算第一步什么的,分数化简之类。 LL gcd(LL a, LL b) { return b ? gcd(b, a % b) : a; } <pre name="code" class="cpp">LL lcm(LL a, LL b){LL c = gcd(a, b);return a / c * b;} 例题:

数论ZOJ 2562

题意:给定一个数N,求小于等于N的所有数当中,约数最多的一个数,如果存在多个这样的数,输出其中最大的一个。 分析:反素数定义:对于任何正整数x,其约数的个数记做g(x).例如g(1)=1,g(6)=4.如果某个正整数x满足:对于任意i(0<i<x),都有g(i)<g(x),则称x为反素数。 性质一:一个反素数的质因子必然是从2开始连续的质数。 性质二:p=2^t1*3^t2*5^t3*7

POJ2247数论

p = 2^a*3^b*5^c*7^d 求形如上式的第n小的数。 import java.io.BufferedReader;import java.io.InputStream;import java.io.InputStreamReader;import java.io.PrintWriter;import java.math.BigInteger;import java.u

CSP-J基础之数学基础 初等数论 一篇搞懂(一)

文章目录 前言声明初等数论是什么初等数论历史1. **古代时期**2. **中世纪时期**3. **文艺复兴与近代**4. **现代时期** 整数的整除性约数什么样的整数除什么样的整数才能得到整数?条件:举例说明:一般化: 判断两个数能否被整除 因数与倍数质数与复合数使用开根号法判定质数哥德巴赫猜想最大公因数与辗转相除法计算最大公因数的常用方法:举几个例子:例子 1: 计算 12 和 18

CSP-J基础之数学基础 初等数论 一篇搞懂(二)

文章目录 前言算术基本定理简介什么是质数?举个简单例子:重要的结论:算术基本定理公式解释:举例: 算术基本定理的求法如何找出质因数:举个简单的例子: 重要的步骤:C++实现 同余举个例子:同余的性质简介1. 同余的自反性2. 同余的对称性3. 同余的传递性4. 同余的加法性质5. 同余的乘法性质 推论 总结 前言 在计算机科学和数学中,初等数论是一个重要的基础领域,涉及到整数

【转载】ACM感悟

今天看了一篇我们学校前辈的ACM的感悟,觉得写的十分有道理,这里转载,文章还会不断的改进和更新。 原文链接:http://www.cnblogs.com/Chierush/p/3760870.html?ADUIN=1339764596&ADSESSION=1401536826&ADTAG=CLIENT.QQ.5329_.0&ADPUBNO=26349 声明:本文是写给弱校ACM新手的一点

我们依旧在追梦的路上-山东省第六届ACM比赛总结

这场比赛从结果而言达到了预期(金牌),从过程而言和我的预期相差甚远(打的太乱,个人发挥很差),还好关键时刻队友抗住压力,负责后果真的不堪设想。 热身赛 热身赛纯粹测机器的,先把A,B,C草草水过(A题小写x打成大写的也是醉了),我和老高开始各种测机器,long long不出所料是lld的,试了一下除0和数组越界的re问题,发现没有re,只有wa(甚至数组越界还AC了),至于栈深的话也没过多追

2014年暑假培训 - 数论

A银河上的星星 /**************************************************************     Problem: 1014     User: DoubleQ     Language: C++     Result: Accepted     Time:190 ms     Memor

ZOJ1007(数论)

题目链接:点击打开链接 解题思路:   纯粹的数学题,没有输入,直接要求输出.直接给出的求和式子极限到无穷,无法直接计算.Hint里给出了提示,大意就是说求g(x) - g(1)的求和极限,最后再加上g(1)就能求出g(x).   由g(x)  - g(1) 能够得出 1 / k*(k+x) - 1 / k * (k + 1) = (1 - x) / k * ( k + 1) * (k