前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >HDOJ 1005

HDOJ 1005

作者头像
用户1154259
发布2018-01-17 14:16:15
4870
发布2018-01-17 14:16:15
举报

Number Sequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 63888    Accepted Submission(s): 14701 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

我自己做的时候 忽略了n的大小,所以递归的时候栈溢出了.....

我的代码:

代码语言:javascript
复制
 1 #include <iostream>
 2 using namespace std;
 3 static int count;
 4 int f(int a,int b,int n);
 5 int mod(int n);
 6 int main()
 7 {
 8     int a,b,n;
 9     while(cin>>a>>b>>n)
10     {
11         if(a>=1 && a<=1000 && b>=1 && b<=1000)
12         {
13             count=0;
14             cout<<f(a,b,n)<<endl;
15         }
16         else
17             break;
18     }
19     return 0;
20 }
21 int f(int a,int b,int n)
22 {
23     if( n == 1 )
24         return 1;
25     else if( n == 2)
26         return 1;
27     else
28         return mod(a*f(a,b,n-1)+b*f(a,b,n-2));
29 }
30 int mod(int n)
31 {
32     while(n>7)
33     {
34         n=n-7;
35     }
36     return n;
37 }

网上大神的代码:

代码语言:javascript
复制
#include <iostream>
#include <vector>
using namespace std;
vector<int> ivec;
//布尔数组元素flag[i][j]如果为true,则说明前面已经出现了i 和 j两者的组合
bool flag[7][7];
void init(void)
{
    int i,j;
    for(i = 0;i < 7;++i)
        for(j = 0;j< 7;++j)
            flag[i][j] = false;
}
int main()
{
    int a,b,n;
    while(cin>>a>>b>>n)
    {
        if(a == 0&&b == 0&&n == 0)
            break;
        ivec.clear();
        init();
        ivec.push_back(1);
        ivec.push_back(1);
        flag[1][1] = true;
        int count = 1,f;
        while(1)
        {
            f = (a*ivec.at(count)%7 + b*ivec.at(count - 1)%7)%7;
            ivec.push_back(f);
            ++count;
            //如果flag变量为true,则说明前面已经出现了这两者的组合,出现重复,无需下一步计算,直接break退出即可
            if(flag[ivec.at(count)][ivec.at(count - 1)] == true)
                break;
            else
                flag[ivec.at(count)][ivec.at(count - 1)] = true;
        }
        //count中存放的是ivec中出现循环前的元素总个数,注意ivec中的下标是从0开始计数的
        count = count - 1;
        if(n < count)
            cout<<ivec.at(n-1)<<endl;
        else
        {
            int j;
            //for循环的目的是找出从那个地方开始重复,此处应该是从j处开始循环,注意j是从0下标开始计数的
            for(j = 0;;++j)
                if(ivec.at(count) == ivec.at(j) && ivec.at(count + 1) == ivec.at(j+1))
                    break;
            n = (n - j)%(count - j);
            if(n == 0)
                n = count - j;
            n += j;
            cout<<ivec.at(n-1)<<endl;
        }
    }
    return 0;
}
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2012-09-21 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Number Sequence
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档