【题解】ICPC2020上海 Fibonacci 【水题】

链接:https://codeforces.com/gym/102900/problem/G

G - Fibonacci

 In mathematics, the Fibonacci numbers, commonly denoted as fnfn, is a sequence such that each number is the sum of the two preceding numbers, starting with 1 and 1. That is, f1=1, f2=1and fn=fn−2+fn−1 (n≥3).

Thus, the beginning of the sequence is 1,1,2,3,5,8,13, 21, …

Given n, 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 integer n (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;
int main(){
    scanf("%d",&n);
    k=n / 3;
    ans=(2*n*k-k*k-k) / 2;
    printf("%lld",ans);
    return 0;
} 

 

上一篇:springcloud分布式事务TXLCN


下一篇:Spring Cloud + TX-LCN分布式事务框架 亲测