Font Size: ← →
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
/* *Copyright (c)2014,烟台大学计算机与控制工程学院 *All rights reserved. *文件名称:test.cpp *作 者:冷基栋 *完成日期:2015年2月12日 *版 本 号:v1.0 *问题描述:数学求模 */ #include <iostream> using namespace std; int arr[100]; int main() { std::ios::sync_with_stdio(false); int A,B,n; arr[1] = arr[2] = 1; while(cin>>A>>B>>n,A||B||n) { int i; for(i=3; i<100 ; i++) { arr[i]=(A*arr[i-1]+B*arr[i-2])%7; if(arr[i]==1&&arr[i-1]==1) break; } n=n%(i-2); arr[0]=arr[i-2]; cout << arr[n] << endl; } return 0; }
运行结果:
知识点总结:
找到隐藏的循环节 注意arr【0】
学习心得:
好好学习 天天向上
时间: 2024-10-07 00:12:44