求卡常
  • 板块灌水区
  • 楼主ceh666
  • 当前回复13
  • 已保存回复13
  • 发布时间2024/10/1 14:37
  • 上次更新2024/10/1 16:47:58
查看原帖
求卡常
1418335
ceh666楼主2024/10/1 14:37

rt
卡常前的

#include <bits/stdc++.h>
using namespace std;
bool ans[100000001];
void init () {
	for (int i = 2; i * i <= 100000000; i ++) {
		if (ans[i] == 0) {
			for (int j = i + i; j <= 100000000; j += i) {
				ans[j] = 1;
			}
		}
	}
}
bool hw (int n) {
    int temp = n, ans = 0;
    while (temp != 0) {
        ans = ans * 10 + temp % 10;
        temp /= 10;
    }
    if (ans == n) return 1;
    else return 0;
}
int main(){
//	freopen("pprime.in", "r", stdin);
//	freopen("pprime.out", "w", stdout);
	ans[0] = 0;
	ans[1] = 0;
	init();
	int a, b;
	cin >> a >> b;
	for (int i = a; i <= b; i ++) {
		if (ans[i] == 0 && hw(i)) {
			cout << i << endl;
		}
	}
	return 0;
}

卡常后的

#include <iostream>
#include <stdio.h>
using namespace std;
bool ans[100000001];
void init () {
	for (int i = 2; i * i <= 98000000; i ++) {
		if (ans[i] == 0) {
			for (int j = i + i; j <= 98000000; j += i) {
				ans[j] = 1;
			}
		}
	}
}
bool hw (int n) {
    int temp = n, ans = 0;
    while (temp != 0) {
        ans = ans * 10 + temp % 10;
        temp /= 10;
    }
    if (ans == n) return 1;
    else return 0;
}
int main(){
	init();
	int a, b;
	scanf("%d%d", &a, &b);
	for (int i = a; i <= b; i ++) {
		if (!(i % 2)) {
			continue;
		}
		if (ans[i] == 0){
			if (hw(i)) {
			 	printf("%d\n", i);
			}
		}
	}
}

求降时间复杂度

2024/10/1 14:37
加载中...