社区讨论

22分求助

P1217[USACO1.5] 回文质数 Prime Palindromes参与者 4已保存回复 3

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
3 条
当前快照
1 份
快照标识符
@m4poldss
此快照首次捕获于
2024/12/15 22:08
去年
此快照最后确认于
2025/11/04 12:46
4 个月前
查看原帖

帮帮我

#3-#9都TLE了
CPP
#include <iostream>
#include <vector>
#include <cstring>
#include <cmath>
using namespace std;
int first,last;
vector<int> list;
template<typename T>
inline void read(T &x){
    bool f=1;x=0;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-') f=!f;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        x=(x<<1)+(x<<3)+(ch^48);
        ch=getchar();
    }
    if (f)x = x;
    else x = -x;
    return;
}
template<typename T>
inline void write(T x){
    if(x<0) putchar('-'),x=-x;
    if(x>9) write(x/10);
    putchar(x%10+'0');
    return;
}
void make_a_list(int end){
    list.push_back(2);
    list.push_back(3);
    for (int i = 5; i <= end; ++i) {
        for (int j : list) {
            if(i%j==0)goto flag;
        }
        list.push_back(i);
        flag:;
    }
}
int re(int x){
    int num = log10(x),sum=0;
    while(x){
        sum+=x%10*pow(10,num);
        x/=10;
        num--;
    }
    return sum;
}
bool newif(int x){
    for (int i: list)if (x == i)return true;
    return false;
}
void run(string type){
    if(type=="P1217"){
        read(first);
        read(last);
        make_a_list(last);
        for (int i = first; i <= last; ++i) {
            if(re(i) == i && newif(i)){
                write(i);
                putchar('\n');
            }
        }
        return;
    }
    return;
}
int main(){
    run("P1217");
    return 0;
}

回复

3 条回复,欢迎继续交流。

正在加载回复...