标签:偶数 ICPC2020 数列 题解 sum 斐波 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; }
标签:偶数,ICPC2020,数列,题解,sum,斐波,Fibonacci,那契 来源: https://www.cnblogs.com/tzw2698/p/14248342.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。