Fibonacci Again HDU-1021

原题:https://cn.vjudge.net/problem/HDU-1021
本来是非常简单的,我原来的思路就是把从0到1 000 000F(n)计算出来然后存起来,然后在判断是否可以被3整除。

#include<iostream>
#include<cstdio>
#include<iomanip>
#include<algorithm>
#include<vector>
#include<string>
#include<cstring>
#include<cmath>

#define vint vector<int>
#define vstr vector<string>
#define vll vector<long long>
#define ll long long
#define pf printf
#define sf scanf
#define pft	printf("\t")
#define pfn printf("\n")
#define pfk printf(" ")
#define N 1000000007
#define PI 3.1415926

using namespace std;

int a[1000100];

int main() {
	int n;
	a[0] = 7;
	a[1] = 11;
	for( int i=2; i<1000100; i++ ) {
		a[i] = a[i-1]+a[i-2];
	}
	while( cin >> n ) {
//		cout << a[n];
//		pfn;
		if( a[n]%3==0 ) {
			cout << "yes";
			pfn;
		}else {
			cout << "no";
			pfn;
		}
	}
	return 0;
}

最后提交结果是WA,我很纳闷,就问了同学,大佬告诉我n接近1 000 000时long long 都爆了(实际上F(90)应该就会爆),所以这种解法肯定不行。
之后我就找了题解,这道题最简单的就是找规律了;

n01234567891011121314
F(n)711182947761231993225218431364220735715778
结果nonoyesnononoyesnononoyesnononoyes

可以看出n = 2 + 4*k(k是非负整数)时,F(n)%3==0即“yes”
所以AC代码:

#include<iostream>
#include<cstdio>
#include<iomanip>
#include<algorithm>
#include<vector>
#include<string>
#include<cstring>
#include<cmath>

#define vint vector<int>
#define vstr vector<string>
#define vll vector<long long>
#define ll long long
#define pf printf
#define sf scanf
#define pft	printf("\t")
#define pfn printf("\n")
#define pfk printf(" ")
#define N 1000000007
#define PI 3.1415926

using namespace std;

int main() {
	int n;
	while( cin >> n ) {
		if( (n-2)%4==0 ) {
			cout << "yes";
		}else {
			cout << "no";
		}
		pfn;
	}
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值