哪种是基于矢量搜索加速算法的最佳方法?

时间:2016-04-17 17:28:55

标签: c++ algorithm vector indexing

我正在尝试解决Euler question 419

到目前为止,我认为我设法建立了一个算法来找到答案。或者至少它给出了前40步的正确结果。但我需要计算1,000,000,000,000步。解决前40步(使用我的算法)大约需要3-4秒。迭代次数越大,计算时间也越长。我不认为我的电脑一年可以解决1,000,000,000,000次迭代。

我所做的只是使用临时向量进行序号计数(form_1和form_2)并保持计算每次迭代的结果(testVec)。以下是我的代码:

#include <iostream>
#include <stdio.h>
#include <vector>
#include <cmath>

std::vector<int> form_1;
std::vector<int> form_2;
std::vector<int> testVec;

void showVec(std::vector<int>& vec)
{
    //
    for (unsigned long int i = 0; i < vec.size(); i++)
    {
        //
        std::cout << vec[i] << std::endl;
    }
}

void resFin(int start, int stop, std::vector<int>& vec)
{
    //
    for (unsigned long int i = 0; i < vec.size(); i++)
    {
        //
        if (i == 0)
        {
            //
            form_1.push_back(vec[0]);
            //std::cout << "form_1 pushed " << vec[0] << std::endl;
        }
        else
        {
            //
            if (i != vec.size() - 1)
            {
                //
                if (vec[i] == vec[i - 1])
                {
                    //
                    form_1.push_back(vec[i]);
                    //std::cout << "form_1 pushed " << vec[i] << std::endl;
                }
                else
                {
                    //
                    form_2.push_back(form_1.size());
                    form_2.push_back(vec[i - 1]);
                    form_1.clear();
                    form_1.push_back(vec[i]);
                }
            }
            else
            {
                //
                if (vec[i] == vec[i - 1])
                {
                    //
                    form_1.push_back(vec[i]);
                    //std::cout << "form_1 pushed " << vec[i] << std::endl;
                    form_2.push_back(form_1.size());
                    //std::cout << "form_2 pushed " << form_1.size() << std::endl;
                    form_2.push_back(vec[i - 1]);
                    //std::cout << "form_2 pushed " << vec[i - 1] << std::endl;
                    form_1.clear();
                }
                else
                {
                    //
                    form_2.push_back(form_1.size());
                    //std::cout << "form_2 pushed " << form_1.size() << std::endl;
                    form_2.push_back(vec[i - 1]);
                    //std::cout << "form_2 pushed " << vec[i - 1] << std::endl;
                    form_2.push_back(1);
                    //std::cout << "form_2 pushed " << 1 << std::endl;
                    form_2.push_back(vec[i]);
                    //std::cout << "form_2 pushed " << vec[i] << std::endl;
                    form_1.clear();
                }
            }
        }
    }
    vec.clear();
    for (unsigned long int k = 0; k < form_2.size(); k++)
    {
        //
        vec.push_back(form_2[k]);
        //std::cout << "vec pushed " << form_2[k] << std::endl;
    }
    //showVec(vec);
    if (start + 1 != stop)
    {
        //
        form_1.clear();
        form_2.clear();
        std::cout << "recursed to " << start + 1 << std::endl;
        resFin(start + 1, stop, vec);
    }
}

void stepFind(int stop, std::vector<int>& vec)
{
    //
    resFin(1, stop, vec);
}

void trimmVec(std::vector<int>& vec)
{
    //
    int a = 0;
    int b = 0;
    int c = 0;

    for (unsigned long int i = 0; i < vec.size(); i ++)
    {
        //
        switch (vec[i])
        {
        case 1:
            a++;
            a = a % 1073741824;
            break;
        case 2:
            b++;
            b = b % 1073741824;
            break;
        case 3:
            c++;
            c = c % 1073741824;
            break;
        default:
            break;
        }
    }
    std::cout << "a is " << a << "; b is " << b << "; c is " << c << std::endl;
}

int main()
{
    //
    testVec.push_back(1);
    testVec.push_back(1);
    stepFind(39, testVec);
    //showVec(testVec);
    trimmVec(testVec);
    getchar();
    return 0;
}

我认为没有人应该等待几个小时来解决欧拉问题吧?所以我在这里做错了。那么,是否存在这样的方法来最小化计算时间,特别是在搜索内的向量中(我认为这最耗费时间)?

0 个答案:

没有答案