HDOJ 1005

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的大小,所以递归的时候栈溢出了.....

我的代码:

 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 }

网上大神的代码:

#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;
}

时间: 2024-08-14 17:36:30

HDOJ 1005的相关文章

【HDOJ 1005】 CRB and His Birthday

[HDOJ 1005] CRB and His Birthday 背包 商场卖东西 没件物品有对应的价值 同时由于超市老板跟你是好绩优...每买一件物品给你a个糖果 同时如果购买某物品 会给对应的b种糖果 即买x个i 可以得到ai*x+bi个糖果 问怎么能得到最多糖果 开始是想开个bool标记每个状态某糖果买每买 还有在该状态是否第一次买某种糖果 写着写着写不好了-- 后来突然想到 可以把每件物品拆开 由于每种物品只赠送一次b 可以把赠b的物品作为"特卖品" 其余为正常送a的物品 这样

【hdoj 1005】有限状态机

题目:传送门 解答:f(1) = 1, f(2) = 1, f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7 乍看之下像是递归.但是数据范围太大了,一定会超时.可以想到找规律. 如果将 f(n) 视为一个状态,那么决定它的是谁?是前两个状态.而且因为 mod 7,所以这个函数的定义域.值域都是{0,1,2,3,4,5,6}. 这样,我们可以构造一个 7*7的有限状态机(二维数组),每个状态填写出现的次数.当我们即将填写一个状态时发现里面已经出现了次数,当前次数

【HDOJ】4328 Cut the cake

将原问题转化为求完全由1组成的最大子矩阵.挺经典的通过dp将n^3转化为n^2. 1 /* 4328 */ 2 #include <iostream> 3 #include <sstream> 4 #include <string> 5 #include <map> 6 #include <queue> 7 #include <set> 8 #include <stack> 9 #include <vector>

HDOJ 3790 双权值Dijkstra

1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #include <cstring> 5 using namespace std; 6 7 const int INF = 1000000; 8 const int MAXSIZE = 1005; 9 10 int map[MAXSIZE][MAXSIZE]; 11 int price[MAXSIZE][MAXSIZE]; 1

HDOJ 1217 Floyed Template

1 #include <iostream> 2 #include <stdio.h> 3 #include <string.h> 4 #include <cstring> 5 #include<map> 6 using namespace std; 7 8 map<string,int>name; 9 const int INF = 1000000; 10 const int MAXSIZE = 1005; 11 const int

hdoj 2391 Filthy Rich 【DP】

题目大意:有个二维数组,你从(0,0)出发,最终到(n,m), 在这个二维数组中,每个位置dp[i][j]都有一定量的黄金,你可以拾取,问你最多能失去多少,并且,你的方向有下,右, 斜向下三个方向: 策略:就是每一个都加上它的上方向与左方向的最大值,这样到最后就是最大值.详情见代码 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2391 代码: #include<stdio.h> #include<string.h> int dp[1

【HDOJ】2428 Stars

先排序后二分. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <algorithm> 5 using namespace std; 6 7 #define MAXN 1005 8 9 typedef struct { 10 int x, y; 11 } node_st; 12 13 node_st nodes[MAXN]; 14 int n; 15 16 b

【HDOJ】1175 连连看

BFS.wa了一下午,原来是YES,写成了Yes. 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <queue> 5 using namespace std; 6 7 typedef struct node_st{ 8 int x, y; 9 int dir; 10 int turn; 11 node_st() {} 12 node_st(int xx, i

hdoj 1598 find the most comfortable road 【并查集】+【暴力枚举】

find the most comfortable road Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4022    Accepted Submission(s): 1732 Problem Description XX星有许多城市,城市之间通过一种奇怪的高速公路SARS(Super Air Roam Structure---超