Number Sequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 179495 Accepted Submission(s): 44623
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).
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中文翻译:
问题描述一个数字序列被定义如下: F(1)= 1,F(2)= 1,F(N)=(A * F(N - 1)+ B * F(N - 2))模7. 鉴于 A,B和n,你要计算f(n)的值。
输入输入由多个测试用例组成。每个测试用例在单行(1 <= A,B <= 1000,1 <= n <= 100,000,000)中包含3个整数A,B和n。三个零信号表示输入的结束,此测试用例不被处理。
产量对于每个测试用例,在单行上打印f(n)的值。
样品输入1 1 3 1 2 10 0 0 0
样品输出2 5
代码:
#include <stdio.h>
int main()
{
int A,B,N;
int f[50],i;
while(scanf("%d%d%d",&A,&B,&N)!=EOF&&(A+B+N)!=0)
{
f[1]=f[2]=1;
for(i=3;i<=49;i++)
{
f[i]=(A*f[i-1]+B*f[i-2])%7;
}
printf("%d\n",f[N%49]); //规律就是模7之后的周期为49
}
return 0;
}
时间限制:2000/1000 MS(Java / Others)内存限制:65536/32768 K(Java / Others)
总提交:179495接受提交:44623
总提交:179495接受提交:44623
问题描述
一个数字序列被定义如下:
F(1)= 1,F(2)= 1,F(N)=(A * F(N - 1)+ B * F(N - 2))模7.
鉴于 A,B和n,你要计算f(n)的值。
F(1)= 1,F(2)= 1,F(N)=(A * F(N - 1)+ B * F(N - 2))模7.
鉴于 A,B和n,你要计算f(n)的值。
输入
输入由多个测试用例组成。每个测试用例在单行(1 <= A,B <= 1000,1 <= n <= 100,000,000)中包含3个整数A,B和n。三个零信号表示输入的结束,此测试用例不被处理。
产量
对于每个测试用例,在单行上打印f(n)的值。
样品输入
1 1 3 1 2 10 0 0 0
样品输出
2 5