hadu1005 Number Sequence

来源:互联网 发布:电脑编程培训机构 编辑:程序博客网 时间:2024/06/03 19:50

Number Sequence
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 102574    Accepted Submission(s): 24809


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.
题目大意:给出一个递推公式,f(1)=f(2)=1,算题中所给f(n)
难点:数据太大
关键点:找出规律,即根据数论取余运算一般都有规律
解题时间:2014,07,29
 解题思路:找到最小周期
 *************************/

#include<stdio.h>int dashu(int m,int n,int a){if(a==1||a==2) return 1;return (m*dashu(m,n,a-1)+n*dashu(m,n,a-2))%7;}int main(){    int a,b,n;    while(scanf("%d%d%d",&a,&b,&n),!(a==0&&b==0&&n==0))    {printf("%d\n",dashu(a,b,n%49));}return 0;}

0 0
原创粉丝点击