2015 HUAS Provincial Select Contest #1~F

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
解题思路:这个题目的范围比较大,如果直接用递归就应该进行一个转换,不然提交不了。它输出的值总是会小于7,所以可以设置一个循环,先判断这个数除以7的余数等于多少,然后再进行递归运算。
程序代码:
#include<cstdio>
#include<iostream>
using namespace std;
int A,B,n;
int f(int j)
  {
   if(j==1||j==2)
    j=1;
   else j=(A*f(j-1)+B*f(j-2))%7;
   return j;
  }
int main()
{
 while(scanf("%d%d%d",&A,&B,&n)==3&&A&&B&&n)
 {
  int t;
  t=n%49;
  for(int i=1;i<=t;i++)
   f(i);
   
  printf("%d ",f(t));
 }
 return 0;
}
原文地址:https://www.cnblogs.com/chenchunhui/p/4657249.html