这题真是神坑啊,经过递归、for循环的思路,发现都会超时,而程序不能再化简了,就想到规律,不过本题的规律不是很好找,随着a、b不同,周期也不同,这个题神坑的一点在于当n%zhouqi == 0的情况,需要单独挑出来,以下是AC代码:
#include<stdio.h> #include<string> #include<sstream> #include<math.h> #include<string.h> #include<iostream> #include<algorithm> using namespace std; int biao[10000006]; int main() { int a,b,n; while(cin>>a>>b>>n) { if(a == 0 && b == 0 && n == 0) break; int e,zhouqi; biao[1] = 1;biao[2] = 1; for(int i = 3;i <= 100;i++) { biao[i] = (a * biao[i - 1] + b * biao[i - 2]) % 7; if(biao[i] == 1 && biao[i - 1] == 1) {zhouqi = i - 2;break;} } biao[0] = biao[zhouqi]; printf("%d\n",biao[n%zhouqi]); } return 0; }
hdu-1005-Number Sequence,布布扣,bubuko.com
原文:http://blog.csdn.net/glqglqglq2/article/details/38534251