Problem 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
//#include <bits/stdc++.h>//wrong 内存过大
//using namespace std;
//const int maxn = 1e8 + 5;
//int a[maxn];
//int main()
//{
// int A, B, n;
// while (scanf("%d %d %d", &A, &B, &n), A + B + n)
// {
// int pos1 = 1, pos2 = 2;
// a[pos1] = 1;
// a[pos2] = 1;
// for (int i = 3; i <= n; i++)
// {
// a[i] = (A * a[pos2] + B * a[pos1]) % 7;
// printf("%d ", a[i]);
// pos1++;
// pos2++;
// }
// printf("%d\n", a[n]);
// }
// return 0;
// }
//#include <bits/stdc++.h>//时间超时
//using namespace std;
//vector<int> v;
//int main()
//{
// int A, B, n;
// while (scanf("%d %d %d", &A, &B, &n), A + B + n)//上亿数量超时
// {
// v.push_back(1);
// v.push_back(1);
// for (int i = 3; i <= n; i++)
// {
// v.push_back((A * v[1] + B * v[0]) % 7);
// v.erase(v.begin(), v.begin() + 1);
// }
// printf("%d", v[1]);
// }
// return 0;
//}
//#include <bits/stdc++.h>//考虑周期性,余数 //反例7 7 100000000
//using namespace std;
//vector<int> v;
//int main()
//{
// int A, B, n;
// while (scanf("%d %d %d", &A, &B, &n), A + B + n)
// {
// v.push_back(1);
// v.push_back(1);
// int pos = 1;
// for (int i = 2; i < n; i++)
// {
// v.push_back((A * v[pos] + B * v[pos - 1]) % 7);
// pos++;//增加循环节
// if (v[pos - 1] == v[pos] && v[pos - 1] == 1 )//如果出现重复的1
// {
// pos--; //找到循环节
// break;
// }
// if (pos == n - 1)//没有找到循环节
// {
// pos++;
// }
// }
// if (n % pos == 0)//考察余数
// {
// printf("%d\n", v[pos - 1]);
// }
// else
// {
// printf("%d\n", v[n % pos - 1]);
// }
// v.clear();//清空容器
// }
// return 0;
// }
//AC代码
#include <bits/stdc++.h>
using namespace std;
vector<int> v;//容器
int main()
{
int A, B, n;
while (scanf("%d %d %d", &A, &B, &n), A + B + n)
{
v.push_back(1);
v.push_back(1);
int flag = 0;//用来判断是否足以达到循环的地方
int len, pos1, pos2;//记录循环节大小,一个循环起点,一个循环终点
for (int i = 2; i < n; i++)//遍历试图找到循环节
{
v.push_back((A * v[i - 1] + B * v[i - 2]) % 7);
for (int j = 1; j < i; j++)
{
if (v[i] == v[j] && v[i - 1] == v[j - 1])//循环节检验
{//这里遍历所有的在i前面的数
flag = 1;
len = i - j;//循环节长度
pos1 = j;//循环节起点
pos2 = i;//循环节终点
break;//退出,防止超时
}
}
if (flag)
{
break;//退出,防止超时
}
}
if (flag)
{
n = (n - 1 -pos1) % len;//注意n是个数不是下标,先转换成下标再减去非循环部分
if (n == 0)//余数为零是特殊的
{
printf("%d\n", v[pos2]);//恰好就是第一个循环终点的数
}
else
{
printf("%d\n", v[pos1 + n]);//从第一个循环起点开始数
}
}
else//这里是长度短已经到了尽头还没有找到循环节,直接输出最后一个数
{
printf("%d\n", v[n - 1]);
}
v.clear();//注意清空容器
}
return 0;
}