原题: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)应该就会爆),所以这种解法肯定不行。
之后我就找了题解,这道题最简单的就是找规律了;
n | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
F(n) | 7 | 11 | 18 | 29 | 47 | 76 | 123 | 199 | 322 | 521 | 843 | 1364 | 2207 | 3571 | 5778 |
结果 | no | no | yes | no | no | no | yes | no | no | no | yes | no | no | no | yes |
可以看出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;
}