2015 HUAS Provincial Select Contest #1~F

本文介绍了一种解决特定数列问题的方法,该数列定义为f(1)=1,f(2)=1,f(n)=(A*f(n-1)+B*f(n-2))mod7,并给出了相应的C++代码实现。

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\n",f(t));
 }
 return 0;
}

转载于:https://www.cnblogs.com/chenchunhui/p/4657249.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值