0

Possible Duplicate:
I want to generate the nth term of the sequence 1,3,8,22,60 ,164 in Order(1) or order of (nlogn)

I am trying to generate a sequence 1,3,8,22,60... which is basically a[n]=2*(a[n-1]+a[n-2]); Please refer to my question here I want to generate the nth term of the sequence 1,3,8,22,60 ,164 in Order(1) or order of (nlogn)

Here is my implementation of it in c++.But I think it is too slow.The code runs in around 5 seconds for the worst case whereas I want it to run it in less than 1 second. The method is order of log n. So 10^9 takes only 29 steps. Here is my code.Please suggest ways to speed it up or any mistake I am doing

#include <iostream>
#define big long long unsigned int
#include<vector>
#include<stdio.h>
#define _SECURE_SCL 0
big m =1000000007;
using namespace std;
vector <vector <big> > vectin(vector<vector <big> > a)
{
    for(int i=0;i<2;i++)
    {
        vector <big> t;
        for(int j=0;j<2;j++)
        {
            t.push_back(0);
        }
        a.push_back(t);
    }
    return a;
}
vector <vector <big> > unit(vector<vector <big> > a)
{
    for(int i=0;i<2;i++)
    {
        vector <big> t;
        for(int j=0;j<2;j++)
        {   if(i!=j)
            t.push_back(0);
            else
            t.push_back(1);
        }
        a.push_back(t);
    }
    return a;
}
vector<vector <big> > multi(vector<vector <big> > a,vector<vector <big> >b )
{
    vector<vector <big> > c;
    c=vectin(c);
    for(big i=0;i<2;i++){

    for(big j=0;j<2;j++)
    {
        for(big k=0;k<2;k++)
            {
                c[i][j]+=((a[i][k])*(b[k][j]))%m;
            }
    }
}
return c;
}

big modexp_rl(big a,big b, big n)
{
    big r = 1;
    while (1){
        if (b&1)
            r = ((r )*(a) ) % n;
        b /= 2;
        if (!b )
            break;
        a = ((a )* (a) )% n;
    }
    return r;
}

vector <vector <big> > modexs (big b,vector <vector <big> > a )
{
    vector < vector <big > > r;
    r=unit(r);
    while(1)
    {  // cout<<b<<endl;
        if(b&1)
        r=multi(r,a);
        b/=2;

        if(!b)
        break;
        a=multi(a,a);
    }
    return r;
}

void displayvector(vector < vector <big> > s)
{
    for(big i=0;i<2;i++)
    {
        for(big j=0;j<2;j++)
        {
            cout<<s[i][j]<<"\t";
        }
    cout<<endl;
    }

}

vector <big> mul2( vector < vector <big> > a)
{
    vector <big> d;
    d.push_back(3*a[0][0]+1*a[0][1]);
    d.push_back(3*a[1][0]+1*a[1][1]);
return d;
}
int main()
{

  vector < vector <big> > a;
  vector <big> t1,t2;
  t1.push_back(2);
  t1.push_back(2);
  a.push_back(t1);
  t2.push_back(1);
  t2.push_back(0);
  a.push_back(t2);
  //dv(a);
  vector < vector <big> > ans;
  big t,n;
  //cin>>t;
  //scanf("%lld,&t);
  t=10000;
  while(t--){
  //cin>>n;
  //scanf("%lld",&n);
  n=1000000000;
  ans=modexs(n-1,a);
  vector <big> p;
  p=mul2(ans);
  //cout<<p[1]<<endl;
  printf("%lld\n",p[1]);
  //dv(ans);
  }
    return 0;
}
Community
  • 1
  • 1

3 Answers3

3

If you know how many elements are going into your a vector, you should reserve space for that many before you run your algorithm. This is to avoid data copying when the vectors do dynamic resizing.

jxh
  • 69,070
  • 8
  • 110
  • 193
1

I plugged the generating function of the series into Wolfram Alpha, and it appears that the n th term is:

[ (1 + Q)^(1+n) - (1 - Q)^(1+n) ] / (4Q) ,

where Q = sqrt(3).

You may observe that 1 - Q is smaller than unity in absolute value, and so the entire term is exponentially small for large n. That means that for large n you can just compute the first term and take the next biggest integer.

Kerrek SB
  • 464,522
  • 92
  • 875
  • 1,084
0

If there is no way to enhance the function, you can try to research multi-core programming and learn how to implement it. Take this as a reference. It will increase performance of processing. However, it will consume resources. Hope this helps.

SƲmmēr Aƥ
  • 2,374
  • 6
  • 24
  • 29