HDU1021 Fibonacci Again (斐波那契数列)
原题地址:http://acm.hdu.edu.cn/showproblem.php?pid=1021
题意
斐波那契数列的变种,重新定义了一个数列 F(0) = 7, F(1) = 11, F(n) = F(n-1) + F(n-2) (n>=2).
给定n,要求输出F(n)能否被3整除
思路
水题。。。。。两种解法,一种是计算此斐波那契数列的值,但要注意需要对结果mod3;另一种简单粗暴,找规律,看n%4是否为2,是的话就可以被三整除,否则就不可以。
从上面可以看出,每四个为一组,如果索引为2的就是可以被3整除的。
AC代码
解法一:
#include <bits/stdc++.h>
using namespace std;
/**
* Created with IntelliJ Clion.
* @author wanyu
* @Date: 2018-02-27
* @Time: 16:39
* To change this template use File | Settings | File Templates.
*
*/
#define maxn 1000010
int fib[maxn];
void Fibonacci() {
fib[0] = 1;
fib[1] = 2;
for (int i = 2; i < maxn; i++) {
fib[i] = (fib[i - 1] + fib[i - 2]) % 3;
}
}
int main() {
Fibonacci();
int n;
while (~scanf("%d", &n)) {
if (fib[n]) {
printf("no\n");
} else {
printf("yes\n");
}
}
return 0;
}
解法二:
#include <bits/stdc++.h>
using namespace std;
/**
* Created with IntelliJ Clion.
* @author wanyu
* @Date: 2018-02-27
* @Time: 16:39
* To change this template use File | Settings | File Templates.
*
*/
int main() {
int n;
while (~scanf("%d", &n)) {
if (n%4==2) {
printf("yes\n");
} else {
printf("no\n");
}
}
return 0;
}