hdu练习

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

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
做这道题就和吃粑粑一样的难受,我没有想到一个很好的方案,待会儿会放个能运行通过链接。
第一个版本,用了递归,估计不行,内存肯定着不住。

#include <iostream>
using namespace std;
int Process(int A,int B,int n){
   if(n==1||n==2){
    return 1;
   }
   return (A*Process(A,B,n-1)+B*Process(A,B,n-2))%7;
}
int main()
{
    int A,B,n;
    cin>>A>>B>>n;
    while(A!=0&&B!=0&&n!=0){
        cout<<Process(A,B,n)<<endl;
        cin>>A>>B>>n;
    }
    return 0;
}

第二个版本,嗯,好像时间效率上不行。

#include <iostream>
using namespace std;
int Process(int A,int B,int n){
   int result1=1,result2=1,temp_result;
   if(n==1||n==2){
    return 1;
   }
   for(int i=3;i<=n;i++){
    temp_result=result2;
    result2=(A*result2+B*result1)%7;
    result1=temp_result;
   }
   return result2;
}
int main()
{
    int A,B,n;
    cin>>A>>B>>n;
    while(A!=0&&B!=0&&n!=0){
        cout<<Process(A,B,n)<<endl;
        cin>>A>>B>>n;
    }
    return 0;
}

第三个版本,既然时间效率上不够,肯定可以形成循环,问题来了,周期是多少。在不知道周期大小的情况下,我要多少内存空间去存这个数组,ok。我想到了vector。在内存空间上还是满足不了条件,我真的崩溃了。

#include <iostream>
#include<vector>
using namespace std;
vector<int> result;
int Process(int A,int B,int n){
   int temp;
   result.clear();
   result.push_back(1);
   result.push_back(1);
   temp=(A*1+B*1)%7;
   result.push_back(temp);
   while(result[result.size()-1]!=1||result[result.size()-2]!=1){
    result.push_back((A*result[result.size()-1]+B*result[result.size()-2])%7);
   }
   return result[(n-1)%(result.size()-2)];
}
int main()
{
    int A,B,n;
    cin>>A>>B>>n;
    while(A!=0||B!=0||n!=0){
        cout<<Process(A,B,n)<<endl;
        cin>>A>>B>>n;
    }
    return 0;
}

第四个版本应运而生,我先找出周期,我先不存之前求得的值,当我求得周期之后,我再取模求一遍之前的值,居然居然居然,时间上又通不过。我真的要哭了。。。。

#include <iostream>
#include<vector>
using namespace std;
int Process(int A,int B,int n)
{
    if(n==1||n==2)
    {
        return 1;
    }
    int i,result1=1,result2=1,temp_result,key;
    for(i=3; i<=n; i++)
    {
        key=result1;
        temp_result=result2;
        result2=(A*result2+B*result1)%7;
        result1=temp_result;
        if(result2==1&&result1==1)
        {
            break;//此时已经得到周期
        }
    }
    if(i==n+1)
    {
        return result2;
    }
    else if(n%(i-2)==0) {
        return key;
    }
    else
    {
        return Process(A,B,n%(i-2));
    }
}
int main()
{
    int A,B,n;
    cin>>A>>B>>n;
    while(A!=0||B!=0||n!=0)
    {
        cout<<Process(A,B,n)<<endl;
        cin>>A>>B>>n;
    }
    return 0;
}

这里贴上了别人运行的版本,成功运行通过的代码
真的好希望有一个道友能指点我一二呀。。。。。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值