LHX_18460366315 @ 2024-02-12 14:10:26
改了代码之后,3个TLE
#include<bits/stdc++.h>
using namespace std;
bool zhishu(long long x){
if (x <= 1){
return false;
}
for (long long i = 2;i <= sqrt(x);i++){
if (x % i == 0){
return false;
}
}
return true;
}
bool hw(int n) {//判定回文,不懂请参考数字反转
int sum = 0;
int k = n;
while (n != 0) {
sum =sum * 10 + n % 10;
n /= 10;
}
if (sum == k){
return true;
}else{
return false;
}
}
bool weishu(int x){
if((1000 <= x && x <= 9999) || (100000 <= x && x <= 999999)){
return false;
}
return true;
}
int main(){
long long n,m;
cin >> n >> m;
if (n % 2 == 0){
for (long long i = n + 1;i <= m;i += 2){
if (zhishu(i) && hw(i) && weishu(i)){
printf("%ld\n",i);
}
}
}else{
for (long long i = n;i <= m;i += 2){
if (zhishu(i) && hw(i) && weishu(i)){
printf("%ld\n",i);
}
}
}
return 0;
}
by Weekoder @ 2024-02-12 14:14:06
@ZZYX_18670145320 这题好像需要质数筛吧,你可以参考一下我的(埃氏筛)
#include <bits/stdc++.h>
using namespace std;
const int N = 1e7 + 5;
int a, b, prime[N + 5];
void getPrime() {
for (int i = 2; i <= N; i++) {
if (!prime[i]) prime[++prime[0]] = i;
for (int j = 1; j <= N && i <= N / prime[j]; j++) {
prime[prime[j] * i] = 1;
if (i % prime[j] == 0) break;
}
}
}
bool ispalindrome(int n) {
int sum = 0, nn = n;
while (n) {
sum *= 10;
sum += n % 10;
n /= 10;
}
if (nn == sum) return 1;
return 0;
}
int main() {
ios::sync_with_stdio(0);
cin >> a >> b;
getPrime();
for (int i = 1; i <= N; i++) {
if (prime[i] == 1 || prime[i] < a) continue;
if (prime[i] > b) break;
if (ispalindrome(prime[i]))
cout << prime[i] << "\n";
}
return 0;
}
by Weekoder @ 2024-02-12 14:15:08
@ZZYX_18670145320 手滑打错了,是线性筛(欧拉筛)。
by LHX_18460366315 @ 2024-02-12 14:21:34
@Weekoder 谢谢