蒟蒻求助!

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

swan999 @ 2024-03-12 15:13:43

最后一个样例老是tle 哭

import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.Scanner;

public class Main {
    static int a,b;
    static PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    static boolean is_Reverse(int num){
        String str = num + "";
        char[] c = str.toCharArray();
        for(int i = str.length() - 1;i >= 0;i --)
            if(str.charAt(i) != c[str.length() - 1 - i])
                return false;
        return true;
    }
    static boolean is_zs(int num){
        if(num == 2)
            return true;
        for(int i = 2;i <= Math.sqrt(num);i ++)
            if(num % i == 0)
                return false;
        return true;
    }
    public static void main(String[] args){
        Scanner sc = new Scanner(new BufferedInputStream(System.in));
        a = sc.nextInt();
        b = sc.nextInt();
        if(a == 1 || a == 2) {
            out.println(2);
            a = 3;
        }
        for(int i = a;i <= b;i ++){
            if(is_Reverse(i) && is_zs(i)){
                out.println(i);
            }
        }
        out.flush();
        out.close();
    }
}

by tzl_Dedicatus545 @ 2024-03-12 15:55:45

@little_whitezzz 你为啥要用java


by tzl_Dedicatus545 @ 2024-03-12 15:59:26

@little_whitezzz 你这个复杂度是 O(n\sqrt{n}) 的啊,n=10^8 肯定过不去


by Lian_zy @ 2024-03-12 16:12:20

@little_whitezzz 绫华同学建议你用欧拉筛筛出所有素数然后再循环一个一个判断回文数就好了


by tzl_Dedicatus545 @ 2024-03-12 16:29:21

@Lian_lele 我才是神里绫华小姐的狗


by tzl_Dedicatus545 @ 2024-03-12 16:29:35

@Lian_lele 我根本不认识他


by tzl_Dedicatus545 @ 2024-03-12 16:29:44

@Lian_lele 建议反思


by swan999 @ 2024-03-12 18:30:18

@Lian_lele 谢谢芙卡洛斯大佬


by swan999 @ 2024-03-12 18:30:52

@tzl_Dedicatus545 我才是~-v-~


by tzl_Dedicatus545 @ 2024-03-12 18:42:49

@little_whitezzz 菜就多练,这么菜还好意思说自己是神里绫华小姐的狗?


by Lian_zy @ 2024-03-12 20:44:58

@little_whitezzz (跳舞)(变路易十六)


| 下一页