P1217求助

P1217 [USACO1.5] 回文质数 Prime Palindromes

liaoyancheng @ 2024-03-06 21:43:54

66分,有三个测试点超时。求大佬帮助

#include<bits/stdc++.h>
using namespace std;
bool f=1;
void zs(long long n)
{
    if(n%3==0||n%5==0||n%7==0||n%11==0||n%13==0)
    {
        if(n>13)
        {
            f=0;
        }
        else
        {
            long long i;
            if(n%2==0)
            {
                f=0;
            }
            for(i=3;i<n;i+=2)
            {
                if(n%i==0)
                {
                    f=0;
                }   
                if(f==0)
                {
                    break;
                }
            }
        }
    }
    else
    {
        long long i;
        if(n%2==0)
        {
            f=0;
        }
        for(i=13;i<n;i+=2)
        {
            if(n%i==0)
            {
                f=0;
            }   
            if(f==0)
            {
                break;
            }
        }
    }
}
void hws(long long n)
{
    long long a=n,b=0,s=0;
    while(a>0)
    {
        b=b*10+a%10;
        a/=10;
        s++;
        if(b==0)
        {
            f=0;
            break;
        }
    }
    if(b!=n)
    {
        f=0;
    }
    if(s%2==0)
    {
        if(n!=11)
        {
            f=0;
        }
    }
}
int main () 
{
    long long c,a,b;
    cin>>a>>b;
    if(a%2==0)
        c=a+1;
    else
        c=a;
    for(long long i=c;i<=b;i+=2)
    {
        if(i>=9989899)
        {
            cout<<9989899<<endl;
            break;
        }
        else
        {
            hws(i);
            if(f==1)
            {
                zs(i);
                if(f==1)
                {
                    cout<<i<<endl;
                }
            }
            f=1;
        }
    }
    return 0;
}

by liaoyancheng @ 2024-03-06 21:47:19

本人小白一个,实在不知道怎么做了


by a_sir @ 2024-03-06 21:51:02

把cout改成printf试试,应该会快一点


by liaoyancheng @ 2024-03-06 22:05:11

@a_sir 我都换了,但为什么之前对的都错了,之前超时的还是超时

#include<bits/stdc++.h>
using namespace std;
bool f=1;
void zs(long long n)
{
    if(n%3==0||n%5==0||n%7==0||n%11==0||n%13==0)
    {
        if(n>13)
        {
            f=0;
        }
        else
        {
            long long i;
            if(n%2==0)
            {
                f=0;
            }
            for(i=3;i<n;i+=2)
            {
                if(n%i==0)
                {
                    f=0;
                }   
                if(f==0)
                {
                    break;
                }
            }
        }
    }
    else
    {
        long long i;
        if(n%2==0)
        {
            f=0;
        }
        for(i=13;i<n;i+=2)
        {
            if(n%i==0)
            {
                f=0;
            }   
            if(f==0)
            {
                break;
            }
        }
    }
}
void hws(long long n)
{
    long long a=n,b=0,s=0;
    while(a>0)
    {
        b=b*10+a%10;
        a/=10;
        s++;
        if(b==0)
        {
            f=0;
            break;
        }
    }
    if(b!=n)
    {
        f=0;
    }
    if(s%2==0)
    {
        if(n!=11)
        {
            f=0;
        }
    }
}
int main () 
{
    long long c,a,b;
    scanf("%I64d %I64d",&a,&b);
    if(a%2==0)
        c=a+1;
    else
        c=a;
    for(long long i=c;i<=b;i+=2)
    {
        if(i>=9989899)
        {
            printf("9989899");
            break;
        }
        else
        {
            hws(i);
            if(f==1)
            {
                zs(i);
                if(f==1)
                {
                    printf("%I64d \n",i);
                }
            }
            f=1;
        }
    }
    return 0;
}

能帮我看一下怎么回事吗


by ZHE333 @ 2024-03-18 13:43:18

呵呵,我也不会,我不是大佬


by V_HAPPY666 @ 2024-04-20 07:24:59

@liaoyancheng 此题无需开long long

int即可


by liaoyancheng @ 2024-04-20 11:52:43

@V_HAPPY666 我主要问题就是要超时


by V_HAPPY666 @ 2024-04-20 18:48:10

@liaoyancheng 循环变量太大了,我建议你定义全局变量


|