1. 程式人生 > >HDU 1005Number Sequence(迴圈節)

HDU 1005Number Sequence(迴圈節)

Number Sequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 182573    Accepted Submission(s): 45387


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 3 1 2 10 0 0 0
Sample Output 2 5
Author CHEN, Shunbao
Source 感覺程式碼有點不妥,但是能過,。。。。。。。 程式碼:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
int s[100];
int main()
{
    int a,b,n;
    while(scanf("%d%d%d",&a,&b,&n)&&a+b+n)
    {
        int count=0;
        if(a%7==0&&b%7==0)
        {
          if(n==1||n==2)
          {
             printf("1\n");
          }
          else
          {
            printf("0\n");
          }
          count=1;
        }
        if(count)
         continue;
        s[1]=s[2]=1;
        for(int i=3;i<50;i++)
        {
            s[i]=(a*s[i-1]+b*s[i-2])%7;
        }
        printf("%d\n",s[n%48]);

    }
    return 0;

}


相關推薦

HDU 1005Number Sequence(迴圈)

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

2017多校聯合第一場 1006題 hdu 6038 Function 迴圈

題目連結 題意: Please calculate the quantity of different functions f satisfying that f(i)=bf(ai) for each i from 0 to n−1. (嗯...就是這樣) 思路:

hdu 1005Number Sequence

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, y

hdu 1005Number Sequence (矩陣快速冪)

F(n)                                 a                     b                                                   F(n-1)                    

hdu 3221 (指數迴圈)

x^k=x^(k%phi(p)+phi(p))%p( k>=phi(p))  #include <utility> #include <algorithm> #include <string> #include <cstri

HDU——1005Number Sequence(模版題 二維矩陣快速冪+操作符過載)

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

HDU 1005 Number Sequence迴圈(取模)】

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

HDU 3746 Cyclic Nacklace (KMP:補齊迴圈)

Cyclic Nacklace Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submissio

HDU 3746 Cyclic Nacklace(KMP:補齊迴圈)

CC always becomes very depressed at the end of this month, he has checked his credit card yesterday, without any surprise, there are only

2015 ACM/ICPC Asia Regional Shenyang Online & HDU 5451 Best Solver【迴圈+數論||共軛矩陣冪+廣義斐波那契降冪】

通過一道名為So easy!的共軛構造矩陣冪取模的問題來聯想到這個題,不難發現應該用同樣的套路來進行共軛構造。 但是問題來了,指數過分強大,取模數超小,於是一開始考慮尤拉降冪再矩陣快速冪。資料一大,發現答案全錯!後請教得知無理數無法尤拉降冪。 那麼就只有

HDU 3524 Perfect Squares 迴圈+快速冪取模+找規律

Problem Description A number x is called a perfect square if there exists an integer b satisfying x=b^2. There are many beautiful theorem

HDU-1358-Period(KMP求字首迴圈

博主連結 題目 題意: 給定一個字串,求出所有迴圈的字首串, 輸出字首串的長度和迴圈的次數(大於一才算迴圈串) 解題思路: 思路是先構造出 next[] 陣列,下標為 i, 定義一個變數 j = i - next[i] 就是next陣列下標和下標對應值的差

hdoj 1005 Number Sequence (斐波那契數列 找迴圈)

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

hdu 4704 Sum 指數迴圈

#include<cstdio> #include<string> #include<cstring> #include<iostream> #include<cmath> #include<algorithm> #include<

HDU—3746—kmp(迴圈2)

Cyclic Nacklace CC always becomes very depressed at the end of this month, he has checked his credit card yesterday, without any surprise

hdu 2522 求1/n的迴圈 小模擬 另外memset節省時間的特殊寫法

A simple problem Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2567    Accepte

hdu 3977 Evil teacher 求fibonacci數列的迴圈

In the math class, the evil teacher gave you one unprecedented problem! Here f(n) is the n-th fibonacci number (n >= 0)! Where f(0) = f(1) = 1 and for

HDU 5674 Function(斐波那契模數列迴圈)

Problem Description There is a function f(n)=(a+√b)^n+(a−√b)^n. a and b are integers (1≤a,b≤1,000,000). Maybe the function lo

Cyclic Nacklace(HDU-3746)(KMP迴圈

CC always becomes very depressed at the end of this month, he has checked his credit card yesterday, without any surprise, there are only 99.9 yuan left.

hdu 1215 七夕

spa output alt 表輸入 system.in 七夕節 數字 們的 ring 題意:求因子之和。 註意:1的因子之和是1。 數字N的因子就是全部比N小又能被N整除的全部正整數,如12的因子有1,2,3,4,6。 import java.util.Sca