Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

14-mong3125 #49

Merged
merged 1 commit into from
Jun 27, 2024
Merged

14-mong3125 #49

merged 1 commit into from
Jun 27, 2024

Conversation

mong3125
Copy link
Collaborator

πŸ”— 문제 링크

거의 μ†Œμˆ˜

βœ”οΈ μ†Œμš”λœ μ‹œκ°„

2μ‹œκ°„

✨ μ½”λ“œ

import java.util.Arrays;
import java.util.Scanner;

public class Main {
    static long A, B;
    static boolean[] isPrime;
    public static void main(String[] args) {
        // μž…λ ₯
        Scanner sc = new Scanner(System.in);
        A = sc.nextLong();
        B = sc.nextLong();
        isPrime = new boolean[10000001]
        
        // μ „λΆ€ μ†Œμˆ˜ 후보
        Arrays.fill(isPrime, true);
        
        // 0κ³Ό 1은 μ†Œμˆ˜ μ•„λ‹˜
        isPrime[0] = false;
        isPrime[1] = false;

        // μ—λΌν† μŠ€ν…Œλ„€μŠ€μ˜ 체
        for (int i = 2; i < 10000001; i++) {
            if (!isPrime[i]) continue;

            for (int j = i+i; j < 10000001; j += i) {
                isPrime[j] = false;
            }
        }
        
        // 거의 μ†Œμˆ˜ 개수 κ΅¬ν•˜κΈ°
        int count = 0;
        int max = (int) Math.min(10000001, B);
        for (int i = 2; i < max; i++) {
            // λ§Œμ•½ μ†Œμˆ˜λΌλ©΄
            if (isPrime[i]) {
                long now = i;
                // x <= μ΅œλŒ€ / x^(n-1)
                while ((double) i <= (double) B / (double) now) {
                    // x >= μ΅œμ†Œ / x^(n-1)
                    // x^n >μ†Œ
                    if ((double) i >= (double) A / (double) now) count += 1;
                    now *= i;
                }
            }
        }

        System.out.println(count);
    }
}
μš°μ„  λ²”μœ„ λ‚΄μ˜ μ†Œμˆ˜λ₯Ό μ—λΌν† μŠ€ν…Œλ„€μŠ€μ˜ 체λ₯Ό μ΄μš©ν•˜μ—¬ κ΅¬ν•΄μ€λ‹ˆλ‹€.
제곱 수의 λ²”μœ„κ°€ 10^14 μ΄λ―€λ‘œ ꡬ해야할 μ†Œμˆ˜μ˜ λ²”μœ„λŠ” 10^7 μ΄ν•˜μž…λ‹ˆλ‹€.

κ·Έ λ‹€μŒ 거의 μ†Œμˆ˜μ˜ 개수λ₯Ό μ„ΈλŠ” 뢀뢄이 골 λ•Œλ¦½λ‹ˆλ‹€.
μ™œλƒλ©΄ λ²”μœ„κ°€ μ»€μ„œ λ²”μœ„λ₯Ό λ²—μ–΄λ‚˜ μ˜€λ²„ν”Œλ‘œμš°κ°€ λ°œμƒν•  수 있기 λ•Œλ¬Έμ΄μ£ . 
μ˜€λ²„ν”Œλ‘œμš°κ°€ λ°œμƒν•˜λ©΄ μˆ«μžκ°’μ΄ μ΄μƒν•˜κ²Œ νŠ€λ‹ˆκΉŒ μš°λ¦¬λŠ” 항상 λ²”μœ„λ₯Ό μƒκ°ν•΄μ•Όν•©λ‹ˆλ‹€.
10^7을 μ„Έμ œκ³±ν•˜λŠ” μˆœκ°„ long을 λ²—μ–΄λ‚˜λ²„λ¦¬κΈ° λ•Œλ¬Έμ—
이항정리λ₯Ό 톡해 `x^n <= Max` 둜 κ΅¬ν•˜λŠ”κ²Œ μ•„λ‹ˆλΌ `x <= Max / x^(n-1)`을 λ§Œμ‘±ν•˜λŠ” 개수λ₯Ό μ„ΈλŠ”κ²Œ μ’‹μŠ΅λ‹ˆλ‹€.

κ·Έλž˜μ„œ 2λΆ€ν„° 10^7 μ‚¬μ΄μ˜ μ†Œμˆ˜λ₯Ό μ°Ύμ•„μ„œ μœ„μ™€ 같은 λ°©λ²•μœΌλ‘œ
λ²”μœ„λ‚΄μ— 제곱 μˆ˜κ°€ μžˆλŠ”μ§€ ν™•μΈν•˜κ³  λ§Œμ•½ λ²”μœ„λ‚΄λΌλ©΄ λ”ν•΄μ€λ‹ˆλ‹€.

μ΅œμ’…μ μœΌλ‘œ μš°λ¦¬κ°€ μ›ν•˜λŠ” 거의 μ†Œμˆ˜λ₯Ό ꡬ할 수 μžˆμŠ΅λ‹ˆλ‹€.

πŸ“š μƒˆλ‘­κ²Œ μ•Œκ²Œλœ λ‚΄μš©

  • 숫자 λ²”μœ„κ°€ long도 λ„˜μ–΄κ°ˆ 수 μžˆλ‹€. 이 λ•Œ, 이항정리 등을 ν†΅ν•΄μ„œ if λ¬Έ 계산을 μ²˜λ¦¬ν•΄μ•Όν•œλ‹€.
  • μ†Œμˆ˜κ΅¬ν•˜κΈ°λ₯Ό ν•  λ•Œ, 10^7 κΉŒμ§€μ˜ μ†Œμˆ˜λ₯Ό κ΅¬ν•˜λŠ” 과정은 μ‹œκ°„λ³΅μž‘λ„λ₯Ό 크게 κ³ λ €ν•˜μ§€ μ•Šμ•„λ„ λœλ‹€. β†’ μ‹œκ°„ μ œν•œμ€ 2μ΄ˆμ˜€λ‹€. (1초라면 μ•ˆλ˜λ‚˜..?)

Copy link
Collaborator

@rivkms rivkms left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

μ—λΌν† μŠ€ν…Œλ„€μŠ€μ˜ 체λ₯Ό κ΅¬ν˜„ν•˜λŠ” 방법을 μΈμƒμ μœΌλ‘œ λ³Ό 수 μžˆμ—ˆμŠ΅λ‹ˆλ‹€.
μ†Œμˆ˜λ₯Ό κ΅¬ν•˜λŠ” λ°©λ²•μœΌλ‘œ κ°€μž₯ 많이 μ“°μ΄λŠ” 방법이 μ—λΌν† μŠ€ν…Œλ„€μŠ€μ˜ μ²΄μ΄μ§€λ§Œ, μ €λŠ” 아직 ν•΄λ‹Ή 방법을 κ΅¬ν˜„ν•˜λŠ” 것이 μ–΄λ €μ› λŠ”λ° 이번 μ½”λ“œλ₯Ό 톡해 많이 배울 수 μžˆμ—ˆμŠ΅λ‹ˆλ‹€.
또 값이 μ €μž₯λ²”μœ„λ₯Ό λ„˜μ–΄κ°€λŠ” λ¬Έμ œμ— λŒ€ν•˜μ—¬ 잘 λŒ€μ²˜ν•˜μ‹  것 κ°™μ•˜μŠ΅λ‹ˆλ‹€.

μˆ˜κ³ ν•˜μ…¨μŠ΅λ‹ˆλ‹€. 😁

Copy link
Collaborator

@YIM2UL2ET YIM2UL2ET left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

x^n <= Bμ—μ„œ 이항정리λ₯Ό ν†΅ν•˜μ—¬ x <= B / x^(n-1)둜 푸신것은 정말 λ†€λžλ„€μš”.. μš”μ¦˜ μˆ˜ν•™κ΄€λ ¨ 문제λ₯Ό μ•ˆ ν’€μ—ˆλ”λ‹ˆ 이런 풀이 방법이 또 μƒˆλ‘­κ²Œ λŠκ»΄μ§‘λ‹ˆλ‹€. μˆ˜κ³ ν•˜μ…¨μŠ΅λ‹ˆλ‹€.

@tgyuuAn tgyuuAn removed the request for review from kjs254 June 27, 2024 12:12
@tgyuuAn tgyuuAn merged commit da62af4 into main Jun 27, 2024
1 check passed
@tgyuuAn tgyuuAn deleted the 14-mong3125 branch June 27, 2024 12:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants