【題解】ICPC2020上海 Fibonacci 【水題】
連結:https://codeforces.com/gym/102900/problem/G
G - Fibonacci
In mathematics, the Fibonacci numbers, commonly denoted asfnfn, is a sequence such that each number is the sum of the two preceding numbers, starting with1and1. That is,f1=1, f2=1andfn=fn−2+fn−1(n≥3).
Thus, the beginning of the sequence is1,1,2,3,5,8,13, 21, …
Givenn, please calculate\(\sum_{i=1}^{n}\sum_{j=i+1}^{n}g(fi,fj)\), where\(g(x,y)=1\)when\(x\times y\)is even, otherwise\(g(x,y)=0\).
Input
The only line contains one integern(1≤n≤1e9).
Output
Output one number –\(\sum_{i=1}^{n}\sum_{j=i+1}^{n}g(fi,fj)\)
題目大意:
求長度為n的斐波那契數列中滿足x < y, 且x × y為偶數的點對的數目。
題目分析:
x × y為偶數當且僅當x和y不同時為奇數。本題只需要統計斐波那契數列中奇數和偶數的個數,用所有點對數目減去奇數點對的數目即可。
根據斐波那契數列的規律,數列中元素的奇偶性為:奇,奇,偶,奇,奇,偶……則長度為n的斐波那契數列中偶數的個數為 n / 3。
因此,所求滿足條件的點對數目為:\(\textrm{C}_{n}^{2}-\textrm{C}_{n-n/3}^{2}\)
程式碼實現:
#include <bits/stdc++.h> using namespace std; int n; typedef long long ll; int k; ll ans; intmain(){ scanf("%d",&n); k=n / 3; ans=(2*n*k-k*k-k) / 2; printf("%lld",ans); return 0; }