Problem Description

A numbersequence is defined as follows:

f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7.

Given A, B, and n, you are to calculate the value of f(n).

 

Input

The inputconsists of multiple test cases. Each test case contains 3 integers A, B and non a single line (1 <= A, B <= 1000, 1 <= n <= 100,000,000). Threezeros signal the end of input and this test case is not to be processed.

 

Output

For each testcase, print the value of f(n) on a single line.

 

Sample Input

1 1 3

1 2 10

0 0 0 

Sample Output

2

5

#include <iostream>

#include <memory.h>

using namespace std;

int f[101];

int main()

{

   int A,B,n;

   while(cin>>A>>B>>n)

    {

       if(A==0&&B==0&&n==0)

           break;

       memset(f,0,sizeof(f));

       f[1]=1;f[2]=1;

       int i,j,start,end,xxx,flag=0;

       for(i=3;i<=n&&!flag;i++)

       {

           f[i]=(A*f[i-1]%7+B*f[i-2]%7)%7;

           for(j=2;j<=i-1;j++)

               if(f[i-1]==f[j-1]&&f[i]==f[j])

                   {   start=j;

                       end=i;

                       flag=1;

                       break;

                   }

       }

       xxx=end-start;

       if(flag)

          cout<<f[start+(n-start)%xxx]<<endl;

       else

           cout<<f[n]<<endl;

    }

    return 0;

}

posted on 2015-04-24 10:28  星斗万千  阅读(110)  评论(0编辑  收藏  举报