hdu1005-sequence number-解题

这是一道在数字序列中寻找周期的题目。其中有令人叹为观止的技巧。

题目:http://acm.hdu.edu.cn/showproblem.php?pid=1005

解题思路:https://blog.csdn.net/hjd_love_zzt/article/details/9887003

代码:


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

int main()
{
	int a,b,n, i;
	while (1)
	{
		cin >> a >> b >> n;
		if (a == 0 && b == 0 && n == 0)
			break;
		int fib[50];
		fib[1] = fib[2] = 1;
		for (i = 3; i <= 49; i++)
		{
			fib[i] = (a*fib[i - 1] + b*fib[i - 2]) % 7;
		}

		cout << fib[n % 49] << endl;

	}

	
	return 0;
}

感言:用数学方法推导出周期,太牛了。

猜你喜欢

转载自blog.csdn.net/weixin_43917370/article/details/106918375