1. 程式人生 > >湖南大學第十四屆ACM程式設計大賽 C Sleepy Kaguya

湖南大學第十四屆ACM程式設計大賽 C Sleepy Kaguya

連結:https://ac.nowcoder.com/acm/contest/338/C
來源:牛客網
時間限制:C/C++ 1秒,其他語言2秒
空間限制:C/C++ 262144K,其他語言524288K
64bit IO Format: %lld

題目描述

Houraisan☆Kaguya is the princess who lives in Literally House of Eternity. However, she is very playful and often stays up late. This morning, her tutor, Eirin Yagokoro was going to teach her some knowledge about the Fibonacci sequence. Unfortunately, the poor princess was so sleepy in class that she fell asleep. Angry Eirin asked her to complete the following task:

This sequence can be described by following equations:
    1.F[1]=F[2]=1

    2.F[n]=F[n-1]+F[n-2]  (n>2)

 

Now, Kaguya is required to calculate F[k+1]*F[k+1]-F[k]*F[k+2] for each integer k that does not exceed 10^18.

Kaguya is so pathetic. You have an obligation to help her.

(I love Houraisan Kaguya forever!!!)

image from pixiv,id=51208622

輸入描述:

Input
Only one integer k.

輸出描述:

Output
Only one integer as the result which is equal to F[k+1]*F[k+1]-F[k]*F[k+2].

示例1

輸入

複製

2

輸出

複製

1

說明

F[2]=1,F[3]=2,F[4]=3

2*2-1*3=1

備註:

0 < k ≤ 10^18

If necessary, please use %I64d instead of %lld when you use "scanf", or just use "cin" to get the cases.

The online judge of HNU has the above feature, thank you for your cooperation.

題目大意:

這個題是圍繞斐波那契擴充套件而來的 F[1]=F[2]=1,F[n]=F[n-1]+F[n-2],

輸入一個數k代表第k個斐波那契數,讓你求 F[k+1]*F[k+1]-F[k]*F[k+2]的結果,暴力肯定是不行了,斐波那契數的遞增速度非常快,且資料範圍k是1e18.嘗試遞推幾次後會發現結果不是-1就是1,奇數為-1,偶數為1,後來看了熱心博友STZG發的題解才明白原理,感謝申哥的指導!

遞推公式:

G(k)=F[k+1]*F[k+1]-F[k]*F[k+2]

=F[k+1]*F[k+1]-F[k]*(F[k]+F[k+1])

=(F[k+1]-F[k])*F[k+1]-F[k]*F[k]

=F[k-1]*F[k+1]-F[k]*F[k]

所以G(k)=-G(k-1)

#include<iostream>
using namespace std;
int main()
{
    unsigned long long a;
    cin>>a;
    if(a&1)
    cout<<"-1"<<endl;
    else
    cout<<"1"<<endl;
}