* Fibonacci sequence (take surplus)

Source: Internet
Author: User
Tags printf time limit

Problem K
Time limit:1000 MS Memory limit:32 MB 64bit IO Format:%i64d
submitted:225 accepted:51
[Submit] [Status] [Web Board]
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 is 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 in a single line (1 <= A, b <=, 1 <= n <= 100,000,000). Three zeros signal the end of the input and this test case are not a is processed.

Output

For each test case, print the value of f (n) in a single line.

Sample Input

1 1 3
1 2 10
0 0 0

Sample Output

2
5

Since the value of MOD7,F (n) can only be taken in 0-6, and A and B are not changed, it is bound to loop, and the loop section will not exceed 7*7=49;

#include <stdio.h>
int main ()
{
    long  a,b,n,i,s,q,p;
    while (scanf ("%i64d%i64d%i64d", &a,&b,&n)!=eof)
    {
        if (a==0&&b==0&&n==0)
            break;
        P=1;
        Q=1;
        if (n==1| | n==2)
            printf ("1\n");
        else
        {
            n=n%49;
            for (i=3; i<=n; i++)
            {
                s= (a*q+b*p)%7;
                p=q;
                q=s;
            }
            printf ("%i64d\n", s);
        }

    }
    return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.