Hdoj 1005

原题链接

问题描述

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).

输入描述

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.

输出描述

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

输入样例

1 1 3
1 2 10
0 0 0

输出样例

2
5

思路

暴力枚举,因为是除7,余数最多49个一组循环,枚举找到循环后,即可快速算出

代码

#include <iostream>
using namespace std;

int main()
{
    int a[50] = {1, 1};
	int x, y, n;
	ios::sync_with_stdio(false);
	while(cin >> x >> y >> n)
	{
		if(x * y * n == 0) break;
		for(int i = 2; i < 49; i++)
			a[i] = (x * a[i - 1] + y * a[i - 2]) % 7;
		cout << a[(n - 1) % 49] << endl;
	}
    return 0;
}
posted @ 2018-01-15 20:34  HackHarry  阅读(116)  评论(0编辑  收藏  举报