AL_lS @ 2024-10-08 16:33:21
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
bool a(int num) {
if (num <= 1) return false;
if (num == 2) return true;
if (num % 2 == 0) return false;
for (int i = 3; i <= sqrt(num); i += 2) {
if (num % i == 0) return false;
}
return true;
}
int main() {
int m, n;
vector<int> numbers;
cin >> m >> n;
int range = (n > 1000000) ? 2 : (n > 10000) ? 1 : 0;
if (n > 100) {
switch (range) {
case 2:
for (int i = 1; i <= 9; i++) {
for (int j = 0; j <= 9; j++) {
for (int q = 0; q <= 9; q++) {
for (int p = 0; p < 9; p++) {
int sum = 1000000 * i + 100000 * j + 10000 * q + 1000 * p + 100 * q + 10 * j + i;
if (sum >= n && sum <= m && a(sum)) {
numbers.push_back(sum);
}
}
}
}
}
break;
case 1:
for (int j = 1; j <= 9; j++) {
for (int q = 0; q <= 9; q++) {
for (int p = 0; p <= 9; p++) {
int sum = 10000 * j + 1000 * q + 100 * p + 10 * q + j;
if (sum >= n && sum <= m && a(sum)) {
numbers.push_back(sum);
}
}
}
}
break;
case 0:
for (int q = 1; q <= 9; q++) {
for (int p = 0; p <= 9; p++) {
int sum = 100 * q + 10 * p + q;
if (sum >= n && sum <= m && a(sum)) {
numbers.push_back(sum);
}
}
}
break;
}
}
else {
if (n <= 100 && n >= 11) {
numbers.push_back(5);
numbers.push_back(7);
numbers.push_back(11);
}
if (n < 11 && n >= 7) {
numbers.push_back(5);
numbers.push_back(7);
}
if (n < 7 && n >= 5) {
numbers.push_back(5);
}
}
sort(numbers.begin(), numbers.end());
for (int num : numbers) {
cout << num << endl;
}
return 0;
}
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
bool a(int num) {
if (num <= 1) return false;
if (num == 2) return true;
if (num % 2 == 0) return false;
for (int i = 3; i <= sqrt(num); i += 2) {
if (num % i == 0) return false;
}
return true;
}
int main() {
int m, n;
vector<int> numbers;
cin >> m >> n;
int range = (n > 1000000) ? 2 : (n > 10000) ? 1 : 0;
if (n > 100) {
switch (range) {
case 2:
for (int i = 1; i <= 9; i++) {
for (int j = 0; j <= 9; j++) {
for (int q = 0; q <= 9; q++) {
for (int p = 0; p <= 9; p++) {
int sum = 1000000 * i + 100000 * j + 10000 * q + 1000 * p + 100 * q + 10 * j + i;
if (sum >= n && sum <= m && a(sum)) {
numbers.push_back(sum);
}
}
}
}
}
break;
case 1:
for (int j = 1; j <= 9; j++) {
for (int q = 0; q <= 9; q++) {
for (int p = 0; p <= 9; p++) {
int sum = 10000 * j + 1000 * q + 100 * p + 10 * q + j;
if (sum >= n && sum <= m && a(sum)) {
numbers.push_back(sum);
}
}
}
}
break;
case 0:
for (int q = 1; q <= 9; q++) {
for (int p = 0; p <= 9; p++) {
int sum = 100 * q + 10 * p + q;
if (sum >= n && sum <= m && a(sum)) {
numbers.push_back(sum);
}
}
}
break;
}
}
else {
if (n <= 100 && n >= 11) {
numbers.push_back(5);
numbers.push_back(7);
numbers.push_back(11);
}
if (n < 11 && n >= 7) {
numbers.push_back(5);
numbers.push_back(7);
}
if (n < 7 && n >= 5) {
numbers.push_back(5);
}
}
sort(numbers.begin(), numbers.end());
for (int num : numbers) {
cout << num << endl;
}
return 0;
}
本地运行直接返回空,感觉思路没啥问题啊(大悲) 早知道不头铁用switch-case语句了
by craftmine @ 2024-10-08 17:04:55
我用的暴力枚举+一堆常数优化(另类卡时间解法),参考一下:
#include<bits/stdc++.h>
using namespace std;
bool v[100000001];
int prime[50000001],len;
bool palindrome(int x){
int c=0,s=x;
while(x>0){
c=c*10+x%10;
x/=10;
}
return c==s;
}
int qread(){
int ans=0,negative=1;
char c=getchar();
while(c<'0'||c>'9'){
if(c=='-'){
negative=-1;
}
c=getchar();
}
while('0'<=c&&c<='9'){
ans=ans*10+(c-'0');
c=getchar();
}
return ans*negative;
}
void qwrite(int num){
if(num<0){
putchar('-');
num=-num;
}
if(num>9){
qwrite(num/10);
}
putchar(num%10+'0');
}
void fill_c(int n){
for(int i=2;i<=n;i++){
if(v[i]==false){
prime[++len]=i;
}
for(int j=1;j<=len&&i*prime[j]<=n;j++){
v[i*prime[j]]=true;
if(i%prime[j]==0){
break;
}
}
}
}
int main(){
int a=qread(),b=qread();
fill_c(b);
for(int i=1;i<=len;i++){
if(prime[i]>b){
break;
}
if(palindrome(prime[i])&&prime[i]>=a){
qwrite(prime[i]);
putchar('\n');
}
}
return 0;
}
by craftmine @ 2024-10-08 17:26:33
@AL_lS 我下线了
by AL_lS @ 2024-10-09 07:28:44
@craftmine 感谢大佬,从未设想的优化方式!小登消化一下知识盲区的c风格书写方式和函数,结合自己的思路再试试,感谢!