9 条题解

  • 1
    @ 2026-11-1 19:57:24
    #include<bits/stdc++.h>
    using namespace std;
    const int N=1e5+10;
    int len=0,a[110000];
    bool prime(int x){
    	if(x<2) return false;
    	for(int i=2;i*i<=x;i++){
    		if(x%i==0)
    			return false;
    	}
    	return true;
    }
    void huiwen(int x,int n){
    	int b[100],tot=0;
    	int y=x;
    	while(y){
    		b[tot++]=y%10;
    		y/=10;
    	}
    	if(tot*2==n||tot*2-1==n){
    		if(tot*2==n){
    			x=x*10+b[0];
    		}
    		for(int i=1;i<tot;i++)
    			x=x*10+b[i];
    		if(prime(x)){
    			a[len++]=x;
    		}
    	}
    }
    int main(){
    	int n;
    	cin>>n;
    	for(int i=1;i<=100000;i++){
    		huiwen(i,n);
    	}
    	cout<<len<<endl;
    	for(int i=0;i<len;i++)
    		cout<<a[i]<<" ";
    	return 0;
    }
    
    • 1
      @ 2026-10-17 11:37:30

      #include<bits/stdc++.h> using namespace std; const int N=1e5; int len=0,a[N];

      bool prime(int x){ if(x<2) return false; for(int i=2;ii<=x;i++){ if(x%i==0) return false; } return true; } void huiwen(int x,int n){ int b[100],tot=0; int y=x; while(y){ b[tot++]=y%10; y/=10; } if(tot2n||tot*2-1n){ if(tot2==n){ x=x10+b[0]; } for(int i=1;i<tot;i++){ x=x*10+b[i]; } if(prime(x)){ a[len++]=x; } } } int main(){ int n; cin >>n; for(int i=1;i<100000;i++){ huiwen(i,n); } cout <<len<<endl; for(int i=0;i<len;i++){ cout <<a[i]<<' '; } return 0; }

      • 1
        @ 2026-10-17 11:21:03

        #include<bits/stdc++.h> using namespace std; int len=0,a[N]; bool prime(int x){ if(x<2) return false; for(int i=2;ii<=x) if(x%i==0) return false; return true; } void huiwen(int x,int n){ int b[100],tot=0; int y=x; while(y){ b[tot++]=y%10; y/=10; } if(tot2n||tot*2-1n){ if(tot2==n){ x=x10+b[0]; } for(int i=1;i<tot;i++){ x=x*10+b[i]; if(prime(x)){ a[len++]=x; } } } } int main(){ /code/ int n; cin>>n; for(int i=1;i<1000000;i++){ huiwen(i,n); cout<<len<<endl; for(int i=0;i<len;i++){ cout<<a[i]<<" "; } } return 0; }

        • 0
          @ 2026-11-1 19:57:46

          //肯定对,点个赞吧,求求了 #include #include<string.h> #include<stdio.h> #include #include const int N=1e5+10; const int INF=0x3f3f3f3f; using namespace std;

          int len=0,a[110000]; bool prime(int x){ if(x<2) return false; for(int i=2;ii<=x;i++){ if(x%i==0) return false; } return true; } void huiwen(int x,int n){ int b[100],tot=0; int y=x; while(y){ b[tot++]=y%10; y/=10; } if(tot2n||tot*2-1n){ if(tot2==n){ x=x10+b[0]; } for(int i=1;i<tot;i++) x=x*10+b[i]; if(prime(x)){ a[len++]=x; } } }

          int main(){ int n; cin>>n; for(int i=1;i<=100000;i++) huiwen(i,n); cout<<len<<endl; for(int i=0;i<len;i++) cout<<a[i]<<" "; return 0; }

          • 0
            @ 2026-11-1 18:45:17
            #include<bits/stdc++.h>
            using namespace std;
            long long len=0,a[10000000];
            bool p(int x) {
            	if(x < 2) return false;
            	for(int i = 2 ; i * i <= x ; i++) if(x % i == 0) return false;
            	return true;
            }
            void h(int x,int n) {
            	int b[100],t = 0;
            	int y = x;
            	while(y) {
            		b[t++] = y%10;
            		y /= 10;
            	}
            	if(t * 2 == n || t * 2 - 1 == n) {
            		if(t * 2 == n) x = x * 10 + b[0];
            		for(int i = 1; i < t ; i++) x = x * 10 + b[i];
            		if(p(x)) a[len++] = x;
            	}
            }
            int main() {
            	int n;
            	cin >> n;
            	for (int i = 1 ; i <= 100000 ; i++ ) h(i,n);
            	cout<< len << endl ;
            	for(int i = 0; i < len ; i++) cout<< a[i] <<' ';
            	return 0;
            }
            
            • 0
              @ 2026-5-1 17:08:17
              #include<iostream>
              using namespace std;
              int nn[6000];
              int main() {
              	int n,s=1,x,y,num=0;
              	bool f;
              	cin>>n;
              	if(n==2)
              		cout<<"1\n11";
              	else if(n%2==0)
              		cout<<0;
              	else if(n==1)
              		cout<<"4\n2 3 5 7";
              	else {
              		for(int i=1; i<(n+1)/2; i++)s*=10;
              		for(int i=s; i<s*10; i++) {
              			f=1;
              			for(y=i/10,x=i; y>0; y/=10)x=x*10+y%10;
              			for(int j=2; j*j<=x; j++)
              				if(x%j==0) {
              					f=0;
              					break;
              				}
              			if(f)nn[num++]=x;
              		}
              		cout<<num<<endl;
              		for(int i=0; i<num; i++)
              			cout<<nn[i]<<" ";
              	}
              	return 0;
              }
              • 0
                @ 2023-12-24 11:28:43

                拆成一半!!!哈哈哈

                #include <iostream>
                #include <cmath>
                #pragma GCC optimize(3)
                using namespace std;
                const int N=1e8+10;
                int n,ans;
                long long sum[N],len;
                bool c1(int x)
                {
                	if(x==1)
                	{
                		return 0;
                	}
                	for(int i=2;i<=sqrt(x);++i)
                	{
                		if(x%i==0)
                		{
                			return 0;
                		}
                	}
                	return 1;
                }
                int main()
                {
                	ios::sync_with_stdio(0);
                	cin.tie(NULL);
                	cout.tie(NULL);
                	cin>>n;
                	if(n==1)
                	{
                		cout<<4<<endl<<"2 3 5 7";
                		return 0;
                	}
                	long long c;
                	if(n&1)
                	{
                		c=pow(10,n/2-1);
                		for(int i=c;i<=c*10-1;++i)
                		{
                			for(int k=0;k<=9;k++)
                			{
                				long long p=i,l=0;
                				while(p!=0)
                				{
                					l*=10;
                					l+=p%10;
                					p/=10;
                				}
                				long long j=i;
                				j*=10;
                				j+=k;
                				j*=c*10;
                				j+=l;
                				if(c1(j))
                				{
                					ans++;
                					sum[++len]=j;
                				}
                			} 
                			
                		}
                	}
                	else 
                	{
                	    c=pow(10,n/2-1);
                	    for(int i=c;i<=c*10-1;++i)
                		{
                			long long p=i,l=0;
                			while(p!=0)
                			{
                				l*=10;
                				l+=p%10;
                				p/=10;
                			}
                			long long j=i;
                			j*=c*10;
                			j+=l;
                			if(c1(j))
                			{
                				ans++;
                				sum[++len]=j;
                			}
                		}
                	} 
                	cout<<ans<<endl;
                	for(int i=1;i<=len;++i)
                	{
                		cout<<sum[i]<<" "; 
                	}
                	return 0;
                }
                
                • -1
                  @ 2023-5-28 20:03:34

                  如果你也跟我我一样TLE,那么这件事情真的是泰库……啊呸,回归正题

                  看到这题,我就想到先暴力每一个数,先判断素数,再判断回文数,这样太慢啦!喜得吉利分数66,但是不要怕,我会比你先WA的!

                  经过了114514个夜晚的苦思冥想,我终于想出来啦:先枚举范围内的回文数,在逐一判断素数,果仁AC了

                  核心代码:

                  for(int i=10;i<=99999;i++){//上限是9位,就枚举到9/2+1位
                  	int tmp=i/10,k=i;//细节除以10,由于除了11,所有的偶数位数都不是素数 
                  	while(tmp)k=k*10+tmp%10,tmp/=10;//创造奇数位回文数 
                  	if(x<=k&&k<=y&&is_prime(k))//代码区: 将符合的数加入数组;//判断是否在范围内 
                  }
                  

                  (记得特判1位数和两位数,我的代码还是可以优化的)

                  • -2
                    @ 2023-4-21 21:11:51
                    #include<iostream>
                    using namespace std;
                    
                    int nn[6000];
                    
                    int main()
                    
                    {
                    
                    int n,s=1,x,y,num=0;
                    
                    bool f;
                    
                    cin>>n;
                    
                    if(n==2)
                    
                    cout<<"1\n11";
                    
                    else if(n%2==0)
                    
                    cout<<0;
                    
                    else if(n==1)
                    
                    cout<<"4\n2 3 5 7";
                    
                    else
                    
                    {
                    
                    for(int i=1;i<(n+1)/2;i++)s*=10;//以1*10^((n+1)/2-1)为基数
                    
                    for(int i=s;i<s*10;i++)
                    
                    {//到1*10^((n+1)/2)结束
                    
                    f=1;
                    
                    for(y=i/10,x=i;y>0;y/=10)x=x*10+y%10;//构造回文数
                    
                    for(int j=2;j*j<=x;j++)//判断构造数是否为素数
                    
                    if(x%j==0)
                    
                    {
                    
                    f=0;
                    
                    break;
                    
                    }
                    
                    if(f)nn[num++]=x;
                    
                    }
                    
                    cout<<num<<endl;
                    
                    for(int i=0;i<num;i++)
                    
                    cout<<nn[i]<<" ";
                    
                    }
                    
                    return 0;
                    
                    }
                    
                    • 1

                    信息

                    ID
                    1001
                    时间
                    1000ms
                    内存
                    128MiB
                    难度
                    7
                    标签
                    递交数
                    457
                    已通过
                    111
                    上传者