URAL 1133 Fibonacci Sequence(数论)

题目链接

题意 :给你第 i 项的值fi,第 j 项的值是 fj 让你求第n项的值,这个数列满足斐波那契的性质,每一项的值是前两项的值得和。

思路 :知道了第 i 项第j项,而且还知道了每个数的范围,二分求第 i+1项,然后根据性质求下去,求到第 j 项的时候看看通过二分求出来的值与给定的第j项的值大小关系,来确定下一次二分的值,输出的时候注意方向。

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>

using namespace std ;
#define LL long long

int main()
{
    LL i,fi,j,fj,n ;
    while(~scanf("%I64d %I64d %I64d %I64d %I64d",&i,&fi,&j,&fj,&n))
    {
        if(i > j)
        {
            swap(i,j) ;
            swap(fi,fj) ;
        }
        LL start,endd,mid,a,b,c ;
        start = -2000000000LL ,endd = 2000000000LL ;
        while(start + 1 < endd)
        {
            mid = (start + endd) / 2 ;
            a = fi ;
            b = mid ;
            for(int k = i+2 ; k <= j ; k++)
            {
                c = a + b ;
                a = b ;
                b = c ;
                if(c > 4000000001LL || c < -4000000001LL)
                    break ;
            }
            if(b < fj)
            {
                start = mid ;
            }
            else endd = mid ;
        }
        a = fi ;
        b = endd ;
        if(n >= i + 1)
        {
            for(int k = i + 2 ; k <= n ; k++)
            {
                c = a+b ;
                a = b ;
                b = c ;
            }
            printf("%I64d\n",b) ;
        }
        else
        {
            for(int k = i-1 ; k >= n ; k--)
            {
                c = b-a ;
                b = a ;
                a = c ;
            }
            printf("%I64d\n",a) ;
        }
    }
    return 0 ;
}

时间: 2024-10-07 20:00:38

URAL 1133 Fibonacci Sequence(数论)的相关文章

ural 1133 Fibonacci Sequence 二分枚举

给出Fibonacci的第i项和第j项.求第n项. Input The input contains five integers in the following order: i, Fi, j,Fj, n.−1000 ≤ i, j, n ≤ 1000, i ≠ j,−2·109 ≤ Fk ≤ 2·109 (k = min(i, j, n), …, max(i, j, n)). Output The output consists of a single integer, which is th

codeforces 446C DZY Loves Fibonacci Numbers 数论+线段树成段更新

DZY Loves Fibonacci Numbers Time Limit:4000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Appoint description:  System Crawler  (2014-07-14) Description In mathematical terms, the sequence Fn of Fibonacci numbers is defi

SQL Server -&gt;&gt; 斐波那契数列(Fibonacci sequence)

斐波那契数列(Fibonacci sequence)的T-SQL实现 ;WITH T AS ( SELECT 1 AS NUM, CAST(1 AS BIGINT) AS curr, CAST(NULL AS BIGINT) AS prv UNION ALL SELECT curr.NUM + 1 AS NUM, CAST(CASE WHEN prv IS NULL THEN curr ELSE curr + prv END AS BIGINT) AS curr, CAST(curr AS BI

HDU4675-GCD of Sequence(数论+组合计数)

GCD of Sequence Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 949    Accepted Submission(s): 284 Problem Description Alice is playing a game with Bob. Alice shows N integers a1, a2, -, aN, an

记忆化搜索+DFS URAL 1183 Brackets Sequence

题目传送门 1 /* 2 记忆化搜索+DFS:dp[i][j] 表示第i到第j个字符,最少要加多少个括号 3 dp[x][x] = 1 一定要加一个括号:dp[x][y] = 0, x > y; 4 当s[x] 与 s[y] 匹配,则搜索 (x+1, y-1); 否则在x~y-1枚举找到相匹配的括号,更新最小值 5 */ 6 #include <cstdio> 7 #include <algorithm> 8 #include <cmath> 9 #include

Sicily 1779. Fibonacci Sequence Multiplication

1779. Fibonacci Sequence Multiplication Constraints Time Limit: 1 secs, Memory Limit: 63.9990234375 MB Description Maybe all of you are familiar with Fibonacci sequence. Now you are asked to solve a special version of Fibonacci sequence: The Multipli

python实现斐波那契数列(Fibonacci sequence)

使用Python实现斐波那契数列(Fibonacci sequence) 斐波那契数列形如 1,1,2,3,5,8,13,等等.也就是说,下一个值是序列中前两个值之和.写一个函数,给定N,返回第N个斐波那契数字.例如,1返回1 6返回8 我选择了两种方法,一种是将list变成一个队列,另一个则是使用环形队列.不多说,直接上代码:后面我会对为什么这样实现做一个解释 第一个是使用队列的方式: 1 def fibonacciSeq(num): 2 fibonacciSeqList = [] 3 for

python3 求斐波那契数列(Fibonacci sequence)

输出斐波那契数列的前多少个数. 利用函数 #!/usr/bin/env python # -*- coding:utf-8 -*- # Author:Hiuhung Wan # ----斐波那契数列(Fibonacci sequence)----- def check_num(number:str): ''' 对输入的字符串检查,正整数,返回Ture,否则返回False :param number: 输入的字符串 :return: 符合要求,返回Ture,不符合返回False ''' # 输入不

HDU 1005 Number Sequence(数论)

HDU 1005 Number Sequence(数论) Problem Description: A number sequence is defined as follows:f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7. Given A, B, and n, you are to calculate the value of f(n). Input The input consists of multiple