递推关系,寻找循环段,然后找出其中规律输出下标
#include<cstdio>
#include<vector>
using namespace std;
int main()
{
int A, B;
size_t n;
while (scanf("%d %d %d", &A, &B, &n), A || B || n)
{
vector<int> arr;
arr.push_back(1);
arr.push_back(1);
A %= 7;
B %= 7;
bool find = false;
size_t i;
while (!find)
{
arr.push_back(
(A * arr[arr.size() - 1] + B * arr[arr.size() - 2]) % 7);
for (i = 0; i < arr.size() - 2; i++)
if (arr[i] == arr[arr.size() - 2]
&& arr[i + 1] == arr[arr.size() - 1])
{
find = true;
break;
}
}
int period = arr.size() - 2 - i;
if (n - 1 < i)
printf("%d\n", arr[n - 1]);
else
printf("%d\n", arr[(n - 1 - i) % period + i]);
}
return 0;
}