Projext Euler Problem 49:Prime permutations【枚举】【暴力】

PE其他解题报告请参考这里,本题答案在留言首条

Prime permutations

Problem 49

The arithmetic sequence, 1487, 4817, 8147, in which each of the terms increases by 3330, is unusual in two ways: (i) each of the three terms are prime, and, (ii) each of the 4-digit numbers are permutations of one another.

There are no arithmetic sequences made up of three 1-, 2-, or 3-digit primes, exhibiting this property, but there is one other 4-digit increasing sequence.

What 12-digit number do you form by concatenating the three terms in this sequence?


题意:

让你求另外一组四位的素数,使得三个数从小到大为等差数列,且这四个数包含的数字都一样,就如同1487, 4817, 8147类似

分析:

先打个素数表,然后把所有一类的数字放到一个数组里面,这里我是这样设置的,比如1487,4817这两个数,是一类数(也就是说这两个数包含的数字相同),把他们俩放在编号为1478这个数组里,也就是把包含的数字按照从小到大的顺序排序下。打表后可知,范围很小,直接暴力的枚举符合的三个数即可

参考代码
#include<bits/stdc++.h>

using namespace std;

const int maxn = 1e4 + 10;

int f[maxn];

vector<int> E[(int)1e6];

void init() {
    f[0] = f[1] = 1;
    for(int i = 2; i < maxn; i++) {
        if (!f[i]) {
            if (i > 1000) {
                string t;
                t += (char) ((i / 1000) + '0');
                t += (char) ((i / 100 % 10) + '0');
                t += (char) ((i / 10 % 10) + '0');
                t += (char) ((i % 10) + '0');
                sort(t.begin(), t.end());
                int s = (t[0]- '0') * 1000 + (t[1]- '0') * 100 + (t[2]- '0') * 10 + t[3] - '0';
                E[s].push_back(i);
            }
            for (int j = i + i; j < maxn; j += i) f[j] = 1;
        }
    }
}



int main(){
    init();
	for (int i = 1000; i < maxn ;i++) {
        if (E[i].size() > 3) {
            int l = E[i].size();
            for (int a = 0; a < l; a++) {
                for (int b = a + 1; b < l; b++) {
                    for(int c = b + 1; c < l; c++) {
                        if (E[i][c] - E[i][b] == E[i][b] - E[i][a]) {
                            printf("%d %d %d\n", E[i][a], E[i][b], E[i][c]);
                        }
                    }
                }
            }
        }
	}
    return 0;
}

阅读好习惯:点赞 + 收藏~

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值