http://acm.hdu.edu.cn/showproblem.php?pid=1021

按常理计算f[n]的时候会出现越界。。所以利用模运算f[n]=(f[n-1]+f[n-2])%3=(f[n-1]%3+f[n-2]%3)%3;

View Code
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
long long f[1000002];
void init(){
int i;
f[0]=7;f[1]=11;
f[0]%=3;
f[1]%=3;
for(i=2;i<=1000000;i++){
f[i]=(f[i-1]+f[i-2])%3;
}

}
int main(){
int n;
init();
while(~scanf("%d",&n)){
if(!f[n]) printf("yes\n");
else printf("no\n");
}
return 0;
}

还有就是规律了:每个数模三后的余数是一个周期数列 :

1,2,0,2,2,1,0,1,1,2,0,2,2……7个一周期:

View Code
#include <cstdio>
int main()
{
int n;
while(~scanf("%d",&n))
{
if(n%4==2) puts("yes");
else puts("no");
}
return 0;
}





相关文章:

  • 2021-10-09
  • 2022-12-23
  • 2022-02-17
  • 2022-03-07
  • 2022-12-23
  • 2021-12-17
  • 2022-02-02
猜你喜欢
  • 2022-01-04
  • 2021-09-29
  • 2021-07-16
  • 2022-12-23
  • 2021-06-29
相关资源
相似解决方案