杭电1005题

//思路:使用周期实现,周期的开始不一定从头开始
#include <iostream>
using namespace std;
int main()
{
 int a,b;
 long n;
 while(cin>>a>>b>>n && (a+b+n)!=0)
 {
  int result[200] = {0,1,1};
  if(n<3)
  {
   cout<<1<<endl;
  }
  else
  {
   for(int i = 3;i<n+1;i++)
   {
    //使用布尔变量判断是否出现了周期
    bool is_time = false;
    result[i] = (a*result[i-1]+b*result[i-2])%7;
    for(int j = 1;j<i-1;j++)
    {
     //判断是否出现了周期
     if(result[j] == result[i-1] && result[j+1] == result[i])
     {
      is_time = true;
      int t = i-j-1;
         n = (n-j) % t + j;
      break;
     }
    }
    if(is_time)
    {
     break;
    }
   }
   cout<<result[n]<<endl;
  }
 }
 return 0;
}

posted @ 2010-04-25 20:37  北海小龙  阅读(288)  评论(0编辑  收藏  举报