AC了但不理解
查看原帖
AC了但不理解
1555091
Starry_Knight楼主2025/8/30 13:04

这是欧拉筛(我对这个没有疑问):

#include<bits/stdc++.h>
#define endl '\n'
#define no ios::sync_with_stdio(0);
#define mle cin.tie(0),cout.tie(0);
using namespace std;
const int N=1e8+1;
const int N2=1e7+7;
int n,ans,m,r,a[N2];
bool p[N];
bool re(int x){
    int ans=0;
    int t=x;
    while(t>0){
        ans*=10;
        ans+=t%10;
        t/=10;
    }
    return ans==x;
}
void solve(){
	cin>>n>>r;
    p[0]=p[1]=1;
    for(int i=2;i<=r;i++){
        if(p[i]==0){
            a[++m]=i;
        }
        for(int j=1;j<=m;j++){
            if(i*a[j]>r) break;
            p[i*a[j]]=1;
            if(i%a[j]==0) break;
        }
    }
    for(int i=1;i<=m;i++){
        if(re(a[i])&&a[i]>=n) cout<<a[i]<<endl;
    }
}
int main(){
	no mle
	int T=1;
	while(T--){
		solve();
	} 
	return 0;
}

欧拉筛我没有疑问;我有疑问的是以下代码(之前写的):

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e8 + 1;
bool huiwen(int x) {
	int ans = 0;
	for(int i = x; i > 0; i /= 10) {
		ans = ans * 10 + i % 10;
	}
	return ans == x;
}
bool xuebao(int x) {
	if(x==0||x==1) {
		return 0;
	}
	for(int j=2; j<=sqrt(x); j++) {
		if(x%j==0) {
			return 0;
		}
	}
	return 1;
}
int l, r;
bool a[N];
signed main() {
	cin >> l >> r;
	a[1] = 1;
	for(int i = l; i <= r; i ++) {
		if(huiwen(i) && xuebao(i)) cout << i << "\n";
	}
	return 0;
}

个人直觉觉得这个会TLE,求不会TLE的证明(非喜勿喷)

2025/8/30 13:04
加载中...