hdu1005 Number Sequence

来源:互联网 发布:php class 魔术方法 编辑:程序博客网 时间:2024/06/07 09:46

题目链接:点击打开链接

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 31 2 100 0 0
 

Sample Output
25
 
ps:这个题是个有循环结果的,以48为周期循环(循环结果的题型)
#include <iostream>#include<cstdio>using namespace std;int main(){    int n;    int a,b;    int f1=1,f2=1,f;    while(scanf("%d%d%d",&a,&b,&n))    {        if(a==0&&b==0&&n==0)break;        f1=f2=1;        if(n%48!=0)        {            if(n%48<=2)            {                cout<<f1<<endl;                continue;            }            for(int i=3; i<=n%48; i++)            {                f=(a*f2+b*f1)%7;                f1=f2;                f2=f;            }        }        else        {            for(int i=1; i<=48; i++)            {                f=(a*f2+b*f1)%7;                f1=f2;                f2=f;            }        }        cout<<f<<endl;    }    return 0;}



0 0
原创粉丝点击