結果
問題 |
No.1653 Squarefree
|
ユーザー |
![]() |
提出日時 | 2023-06-28 08:55:43 |
言語 | Java (openjdk 23) |
結果 |
TLE
|
実行時間 | - |
コード長 | 2,876 bytes |
コンパイル時間 | 2,654 ms |
コンパイル使用メモリ | 96,764 KB |
実行使用メモリ | 55,872 KB |
最終ジャッジ日時 | 2024-07-05 01:11:44 |
合計ジャッジ時間 | 9,396 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | -- * 3 |
other | TLE * 1 -- * 37 |
ソースコード
import java.io.*; import java.util.*; import java.util.stream.*; public class Main { static ArrayList<Integer> primes = new ArrayList<>(); public static void main(String[] args) throws Exception { Scanner sc = new Scanner(); long left = sc.nextLong(); long right = sc.nextLong(); boolean[] isNotPrimes = new boolean[1000001]; for (int i = 2; i < isNotPrimes.length; i++) { if (!isNotPrimes[i]) { primes.add(i); for (int j = 2; j * i < isNotPrimes.length; j++) { isNotPrimes[j * i] = true; } } } int ans = 0; for (long i = left; i <= right; i++) { if (isFree(i)) { ans++; } } System.out.println(ans); } static boolean isFree(long x) { long y = x; for (int z : primes) { if (z > Math.sqrt(y)) { break; } int count = 0; while (y % z == 0) { count++; y /= z; } if (count >= 2) { return false; } } if (x != y) { return true; } long left = 1; long right = Long.MAX_VALUE / 2; while (right - left > 1) { long m = (left + right) / 2; if (m * m <= x) { left = m; } else { right = m; } } if (left > 1 && left * left == x) { return false; } else { return true; } } } class Utilities { static String arrayToLineString(Object[] arr) { return Arrays.stream(arr).map(x -> x.toString()).collect(Collectors.joining("\n")); } static String arrayToLineString(int[] arr) { return String.join("\n", Arrays.stream(arr).mapToObj(String::valueOf).toArray(String[]::new)); } } class Scanner { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st = new StringTokenizer(""); StringBuilder sb = new StringBuilder(); public Scanner() throws Exception { } public int nextInt() throws Exception { return Integer.parseInt(next()); } public long nextLong() throws Exception { return Long.parseLong(next()); } public double nextDouble() throws Exception { return Double.parseDouble(next()); } public int[] nextIntArray() throws Exception { return Stream.of(br.readLine().split(" ")).mapToInt(Integer::parseInt).toArray(); } public String next() throws Exception { while (!st.hasMoreTokens()) { st = new StringTokenizer(br.readLine()); } return st.nextToken(); } }