牛客小白月赛13 A题

来源:牛客网

这是一道签到题,主要考验比赛时的手速。
接下来是一段很简单的代码,或许你提交它就可以AC。
#include<bits/stdc++.h>
using namespace std;
int main() {
    long long n;
    scanf("%lld",&n);
    long long f1=1,f2=1,f3;
    for(int i=3;i<=n;i++) {
        f3=f1+f2;
        f1=f2;
        f2=f3;
    }
    printf("%lld\n",f3*f3-f1*f1-f1*f3);
    return 0;
}

显然复制代码并不能AC,实际上要求的是f^2n−fn−1fn+1
其中 fn 是斐波那契数列的第n项
单观察或推导可以得出结论:∀n≥3, 若为奇数则为-1,否则就为1。

#include<bits/stdc++.h>
using namespace std;
int main() {
    long long n;
    cin >>n;
    if (n == 1 || n == 2) {
        cout << "-1" << endl;
        return 0;
    }
    if (n%2==1) {
        cout << "1" << endl;
    } else {
        cout <<"-1"<<endl;
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_37520038/article/details/89356427