初始代码(超时)
#include<iostream>
using namespace std;
int main(){
int n;
while (scanf("%d", &n) != EOF){
int res = 0;
int a = 7, b = 11;
if (n < 2){
cout << "No\n";
continue;
}
for (int i = 2; i <= n; i++){
res = a + b;
a = b;
b = res;
}
cout << (res % 3 == 0) ? "Yes\n" : "No\n";
}
return 0;
}
不超时思路
F[0] = 7
F[1] = 11
F[2] = 18 可整除
.
.
.
F[6] = 123 可整除
得出规律:数组下标对4求余结果为2的可被3整除
也可对F[2]之后的数分解为18 * a + 11 * b的形式,b是3的幂次的数组元素才能被3整除。
代码
#include<iostream>
using namespace std;
int main(){
int n;
while (scanf("%d", &n) != EOF){
printf((n % 4 == 2)?"Yes\n":"No\n");
}
return 0;
}