Project Euler problem 37

来源:互联网 发布:程序员屏幕设置 编辑:程序博客网 时间:2024/06/05 03:30

试探性的筛了个500W素数表

然后一个一个素数的试了试

11个都给找出来了

最大的也就70多W

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <vector>#include <queue>#include <set>#include <stack>#include <cmath>#include <map>#include <ctime>#define MAXN 111111#define INF 100000007using namespace std;bool tag[5000001];int p[3000001];int cnt;void get_prime(){    cnt = 0;    tag[1] = 1;    for (int i = 2; i < 5000000; i++)    {        if (!tag[i])        p[cnt++] = i;        for (int j = 0; j < cnt && p[j] * i < 5000000; j++)        {            tag[i*p[j]] = 1;            if (i % p[j] == 0)            break;        }    }}bool gao(int x){    int tx = x;    while(tx)    {        if(tag[tx]) return 0;        tx /= 10;    }    tx = x;    int ten = 1;    while(ten <= tx) ten *= 10;    ten /= 10;    if(ten == 1) return 0;    while(1)    {        if(tag[tx]) return 0;        if(tx < 10) break;        tx = tx % ten;        ten /= 10;    }    return 1;}int main(){    get_prime();    int ans = 0;    for(int i = 0; i < cnt; i++)        if(gao(p[i])) cout << p[i] << endl;    cout << ans << endl;    return 0;}


原创粉丝点击