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;
}
阅读好习惯:点赞 + 收藏~