优化::素数环


0x00 问题本身及分析

素数环,即从1到20这20个数摆成一个环,要求相邻的两个数的和是一个素数.
从1开始,每个空位有20种可能,只要填进去的数合法,即与前面的数不相同;与左边相邻的数的和是一个素数.第20个数还要判断和第1个数的和是否素数.

0x01 首次编写

刚接到问题时写的的代码(均使用g++[version:4.8.1],都没有优化参数):

#include <iostream>
#include <cmath>
using namespace std;

void printer(int num[]);
void searcher(int t, int a[], bool b[]);
bool pd(int p, int q);
int total = 0;

int main(void) {
    int a[21] = { 0 };//0不用,容易造成错乱.
    bool b[21] = { false };//0不用,容易造成错乱.
    searcher(1, a, b);
    cout << total << endl;
    cin.get();
    cin.get();
    return 0;
}

void searcher(int t, int a[], bool b[]) {
    int i;
    if (t == 21) { //这里不加花括号你试试?
        if (pd(a[20], a[1]))
            printer(a);
    }
    else {
        for (i = 1; i <= 20; i++) {
            if (pd(a[t - 1], i) && (!(b[i]))) {
                a[t] = i;
                b[i] = true;
                searcher(t + 1, a, b);
                b[i] = false;
            }
        }
    }
}

void printer(int num[]) {
    total++;
    cout << total << " : ";
    for (int i = 1; i <= 20; i++)
        cout << num[i] << " ";
    cout << endl ;
    return;
}


bool pd(int p, int q) { //不推荐这种用法,但没想到要用什么来替代
    int k = 2;
    int i = p + q;
    while (k <= sqrt(i) && i%k != 0)
        k++;
    if (k>sqrt(i))
        return true;
    else
        return false;
}

比较习惯用搜索和回溯.
这个是最慢的(56783700个结果!).
先想想该怎么优化
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

0x02初步优化

看到了std::cin和std::cout了吗?
因为要和std::printf兼容,所以有同步的选项.并且endl和”\n”也有区别,时间上的区别,所以我们应该替换endl.(理论上std::cin和std::cout也应换成printf()和scanf()的,因为不使用printf()和scanf()其实是懒,于是关闭了同步.至少关了以后时间差不多嘛23333)

#include <iostream>
#include <cmath>
using namespace std;

void printer(int num[]);
void searcher(int t, int a[], bool b[]);
bool pd(int p, int q);
int total = 0;

int main(void) {
    ios::sync_with_stdio(false);
    int a[21] = { 0 };
    bool b[21] = { false };
    searcher(1, a, b);
    cout << total << "\n";
    cin.get();
    cin.get();
    return 0;
}

void searcher(int t, int a[], bool b[]) {
    int i;
    if (t == 21) {
        if (pd(a[20], a[1]))
            printer(a);
    }
    else {
        for (i = 1; i <= 20; i++) {
            if (pd(a[t - 1], i) && (!(b[i]))) {
                a[t] = i;
                b[i] = true;
                searcher(t + 1, a, b);
                b[i] = false;
            }
        }
    }
}

void printer(int num[]) {
    total++;
    cout << total << " : ";
    for (int i = 1; i <= 20; i++)
        cout << num[i] << " ";
    cout << "\n";
    return;
}


bool pd(int p, int q) {
    int k = 2;
    int i = p + q;
    while (k <= sqrt(i) && i%k != 0)
        k++;
    if (k>sqrt(i))
        return true;
    else
        return false;
}

至少在输出方面快多了.
但是三三有令,说这码子还是慢!
再次想想该怎么优化
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

0x03再次优化

每一次都要计算一遍前后两个数这种无聊活才会慢的 .
对了!1~20,其中最大的二数之和为39.
0~39之间,素数有{1,3,5,7,11,13,17,19,23,25,29,31,37}(2应该不是,数学不好(:3」∠))这几个,所以如果直接返回不就快了很多吗?

#include <iostream>
#include <cmath>
using namespace std;

void printer(int num[]);
void searcher(int t, int a[], bool b[]);
bool pd(int p, int q);
bool pdo(int num);
int total = 0;
bool detected[40] = { false };//0不用,容易造成错乱.

int main(void) {
    //初始化detected
    //你也可以用 detected[1] = true; 来初始化.
    //偷懒一下下
    for (int i = 1; i <= 39; i++)
        if (pdo(i, 0))
            detected[i] = true;
    //Done!
    ios::sync_with_stdio(false);
    int a[21] = { 0 };
    bool b[21] = { false };
    searcher(1, a, b);
    cout << total << "\n";
    cin.get();
    cin.get();
    return 0;
}

void searcher(int t, int a[], bool b[]) {
    int i;
    if (t == 21) {
        if (pd(a[20], a[1]))
            printer(a);
    }
    else {
        for (i = 1; i <= 20; i++) {
            if (pd(a[t - 1], i) && (!(b[i]))) {
                a[t] = i;
                b[i] = true;
                searcher(t + 1, a, b);
                b[i] = false;
            }
        }
    }
}

void printer(int num[]) {
    total++;
    cout << total << " : ";
    for (int i = 1; i <= 20; i++)
        cout << num[i] << " ";
    cout << "\n";
    return;
}

bool pd(int p,int q){
    return detected[p+q];
}

bool pdo(int num) { //老的判断,初始化用的. 
    int k = 2;
    while (k <= sqrt(num) && num%k != 0)
        k++;
    if (k>sqrt(num))
        return true;
    else
        return false;
}

等等..!除了快以外还要节约内存.(即使内存大到TB也要节约,能节省一点是一点)

#include <iostream>
#include <cmath>
using namespace std;

void printer(int num[]);
void searcher(int t, int a[], bool b[]);
bool pdo(int num);
int total = 0;
bool detected[40] = { false };

int main(void) {
    for (int i = 1; i <= 39; i++)
        if (pdo(i))
            detected[i] = true;
    ios::sync_with_stdio(false);
    int a[21] = { 0 };
    bool b[21] = { false };
    searcher(1, a, b);
    cout << total << "\n";
    cin.get();
    cin.get();
    return 0;
}

void searcher(int t, int a[], bool b[]) {
    int i;
    if (t == 21) {
        if (detected[a[20] + a[1]])//这是改动
            printer(a);
    }
    else {
        for (i = 1; i <= 20; i++) {
            if (detected[a[t - 1]+ i] && (!(b[i]))) {//这也是
                a[t] = i;
                b[i] = true;
                searcher(t + 1, a, b);
                b[i] = false;
            }
        }
    }
}

void printer(int num[]) {
    total++;
    cout << total << " : ";
    for (int i = 1; i <= 20; i++)
        cout << num[i] << " ";
    cout << "\n";
    return;
}

bool pdo(int num) { 
    int k = 2;
    while (k <= sqrt(num) && num%k != 0)
        k++;
    if (k>sqrt(num))
        return true;
    else
        return false;
}

0x04一些杂项

貌似Linux下程序跑的最快比西方记者还要快,Windows下g++([version:4.8.1],关闭了同步选项)最快,而VC([cl.exe version:19.00.23026];估计默认是关闭同步选项的,感觉差不多)最慢…
Windows下将控制台的输入法切到英文状态下有神奇的效果,Windows10下使用旧版控制台速度又会快不少.
码子可能有误,欢迎纠错.


知识共享许可协议
*注意:本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值