Number Sequence

Description

A number sequence 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 input consists of multiple test cases. Each test case contains 3 integers A, B and n on a single line (1 <= A, B <= 1000, 1 <= n <= 100,000,000). Three zeros signal the end of input and this test case is not to be processed.

Output

For each test case, 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


题目:

   就是找循环节的位置,之后输出对应的值,主要就是找循环点的位置,

思路:

   一般的题目,看见第一个是1,第二个是1,之后就是找那个连续的数字是1的,就可以找到循环的长度了。

这个也是一种办法,不过就是循环的长度,有点长,时间有点久,因为又可能数据的循环位置不是以两个1,为循环开始点的。所以循环的寻找还是的慢慢的找,

   最简单的就是数组,标记。这个办法就是最简单的,一个二维数组,就可以了。题目模7了,那么就是数组总空间大小,不超过49,所以我们可以开一个map[7][7]去标记,一个search[50]去存储的,如果出现了重复,那么就可以直接输出循环的位置。

  循环位置:到刚开始出现循环节的长度j,循环长度i,总的长度为n,循环长度对应的位置就是seach[j+n%i];

当然,如果未出现循环节的话,那就是直接输出search[n]了。


注:

   下面我版署一个与上面思路,有点区别的,就是我下面的办法,比较好看得懂。因为我的代码,主要就是为了以后的题目,知晓思路的。不好意思啊。代码主要就是学习思路的,如果通不过,那么就是测试用例,更加的苛刻,就需要你稍微的去修改修改了。


代码如下:

#include<iostream>
#include<stdio.h>
using namespace std;

int str[60];

int main()
{
    int a,b,n,i,j;
    bool flag;
    while(cin>>a>>b>>n,a||b||n)
    {
        str[0]=str[1]=str[2]=1;
        flag=true;
        for(i=3;i<=n&&flag;i++)
        {
            str[i]=(a*str[i-1]+b*str[i-2])%7;
            for(j=2;j<=i-1;j++)
            {
                if(str[i]==str[j]&&str[i-1]==str[j-1])   //找循环点
                {
                    printf("%d\n",str[j+(n-i)%(i-j)]);
                    flag=false;
                    break;
                }
            }
        }
        if(flag)
        printf("%d\n",str[n]);
    }
    return 0;
}

posted @ 2017-11-27 20:46  让你一生残梦  阅读(258)  评论(0编辑  收藏  举报