亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

1005 Number Sequence(HDU)

系統 2173 0

題目鏈接: http://acm.hdu.edu.cn/showproblem.php?pid=1005

?

Number Sequence

Time Limit: 2000/1000 MS (Java/Others)????Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 85249????Accepted Submission(s): 20209

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

?

?

解析:

這是一道尋找循環點的問題,可能很多人在杭電上通過了這個題目,但是我建議大家將自己的代碼再貼到另一個OJ上進行測試 http://zju.acmclub.com/index.php?app=problem_title&id=1&problem_id=2603

很多人都認為周期是49,但是給出的解題報告都不是很有說服力。

所以,我們可以尋找循環的開頭以及周期,然后輸出,這樣能夠保證正確性,當然一開始的記錄數組最好能夠相對大一些,不然仍然不能通過測試。



代碼:

?

    #include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <math.h>

#define min(a,b) (a<b?a:b)

#define max(a,b) (a>b?a:b)

#define swap(a,b) {(a)=(a)^(b); (b)=(a)^(b); (a)=(a)^(b);}

#define MAXN 65535

#define INF 1e9



int f[1200];

int main(){

    int a,b,n;

    int i, j;

    int flag, term, temp, begin;

    while(~scanf("%d%d%d", &a, &b, &n), (a||b||n)){

        memset(f, 0, sizeof(f));

        f[1]=1;

        f[2]=1;

        term = n;

        flag = 0;

        for(i=3; i<=n&&!flag; i++){

            f[i] = (a*f[i-1]+b*f[i-2])%7;

            for(j = 2; j<i; j++){

                if(f[i]==f[j]&&f[i-1]==f[j-1]){

                    term = i-j;

                    begin = j-2;

                    flag = 1;

                    break;

                }

            }

        }

        if(flag)

            printf("%d\n", f[begin+(n-1-begin)%term+1]);

        else

            printf("%d\n", f[n]);

    }

    return 0;

}
  

?

1005 Number Sequence(HDU)


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦?。。?/p>

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 久久福利青草精品资源站免费 | 国产色资源 | 久久久精品波多野结衣 | 国产精品亚洲国产 | 欧美妇性猛交视频 | 国产激情对白一区二区三区四 | 久久久亚洲伊人色综合网站 | 亚洲五月婷 | 国产99re| 日本成人不卡视频 | 高清国产精品久久 | jizz国产美女 | 狠狠综合 | 成人国产在线24小时播放视频 | 欧美成人久久久免费播放 | 亚洲伦理中文字幕 | 牛牛影视在线观看片免费 | 老司机精品在线播放 | 欧美成人h精品网站 | 99久久999久久久综合精品涩 | 尤物视频一区二区 | 中文字幕久久精品 | 亚洲综合色站 | 九九热在线视频 | 日韩爱爱网站 | 97精品在线 | 亚洲欧洲精品在线 | 欧美 亚洲 一区 | 免费视频毛片 | 国产精品久久久久久一区二区 | 久久精品免费观看久久 | 亚洲精品国产精品乱码视色 | 狠狠躁夜夜躁人人爽天天miya | 日本最猛黑人xxxx猛交 | 性欧美一级毛片欧美片 | 国产高清福利91成人 | 亚洲欧美在线精品一区二区 | 992人人草| 欧美精品亚洲精品日韩一区 | 精品热久国产福利视频 | 久久久网久久久久合久久久久 |