[ICPC2020 Shanghai R] Fibonacci(数学规律)
题面翻译
在数学中,斐波拉契数列常被记为数列 f n f_n fn。该数列的首项 f 1 , f 2 f_1,f_2 f1,f2 均为 1 1 1,并满足递推公式 f n = f n − 2 + f n − 1 ( n ≥ 3 ) f_n=f_{n-2}+f_{n-1}(n\ge 3) fn=fn−2+fn−1(n≥3)。
因此,数列的前一些项为 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 , ⋯ 1,1,2,3,5,8,13,21,\cdots 1,1,2,3,5,8,13,21,⋯。
若 x ⋅ y x\cdot y x⋅y 为偶数,则函数 g ( x , y ) = 1 g(x,y)=1 g(x,y)=1,否则 g ( x , y ) = 0 g(x,y)=0 g(x,y)=0。求 ∑ i = 1 n ∑ j = i + 1 n g ( f i , f j ) \sum\limits_{i=1}^n{\sum\limits_{j=i+1}^n{g(f_i,f_j)}} i=1∑nj=i+1∑ng(fi,fj) 的值。
题目描述
In mathematics, the Fibonacci numbers, commonly denoted as f n f_n fn, is a sequence such that each number is the sum of the two preceding numbers, starting with 1 1 1 and 1 1 1. That is, f 1 = 1 , f 2 = 1 f_1 = 1, f_2 = 1 f1=1,f2=1 and f n = f n − 2 + f n − 1 ( n ≥ 3 ) f_n = f_{n-2} + f_{n-1}~(n \ge 3) fn=fn−2+fn−1 (n≥3).
Thus, the beginning of the sequence is 1 , 1 , 2 , 3 , 5 , 8 , 13 , 21 , … 1, 1, 2, 3, 5, 8, 13, 21,\ldots 1,1,2,3,5,8,13,21,… .
Given n n n, please calculate ∑ i = 1 n ∑ j = i + 1 n g ( f i , f j ) \sum_{i=1}^{n}{\sum_{j=i+1}^{n}{g(f_i,f_j)}} ∑i=1n∑j=i+1ng(fi,fj), where g ( x , y ) = 1 g(x,y) = 1 g(x,y)=1 when x ⋅ y x \cdot y x⋅y is even, otherwise g ( x , y ) = 0 g(x,y) = 0 g(x,y)=0.
输入格式
The only line contains one integer n ( 1 ≤ n ≤ 1 0 9 ) n~(1\le n\le 10^9) n (1≤n≤109).
输出格式
Output one number – ∑ i = 1 n ∑ j = i + 1 n g ( f i , f j ) \sum_{i=1}^{n}{\sum_{j=i+1}^{n}{g(f_i,f_j)}} ∑i=1n∑j=i+1ng(fi,fj).
样例 #1
样例输入 #1
3
样例输出 #1
2
样例 #2
样例输入 #2
10
样例输出 #2
24
样例 #3
样例输入 #3
100
样例输出 #3
2739
解析
找规律即可,斐波那契数列三个一组,奇奇偶,然后计算即可
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=2e5+5;
int n,a[N];
void solve(){
scanf("%lld",&n);
int x=n/3;
int y=n%3;
printf("%lld",x*(x+1)+x*y+x*(x-1)*3/2);
}
signed main(){
int t=1;
// scanf("%lld",&t);
while(t--) solve();
return 0;
}