Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- 1) N == target 이면 Yes, N % target != 0 이면 No
- -> 자명
- 2) target이 소수면 Yes
- -> 자명
- 3) 어떤 소수 p에 대해 target == p^2 && N == p^k 를 만족하면 Yes
- -> 적절한 귀납법으로 답이 Yes라는 것을 증명할 수 있다.
- 4) 그 이외엔 No
- 4-1) target이 두 개 이상의 소인수를 갖고 있는 경우
- 4-2) 어떤 소수 p에 대해 target == p^k (k>=3) 인 경우
- 4-3) 어떤 소수 p에 대해 target == p^2 && N != p^k 인 경우
- -> 모두 적절한 귀납법으로 답이 No라는 것을 증명할 수 있다.
- */
- #include <cmath>
- #include <string>
- using namespace std;
- bool notPrime[100003];
- class CompositeSmash {
- public:
- void sieve() {
- for (int i = 2; i <= 100000; ++i) {
- if (notPrime[i]) continue;
- for (int j = i + i; j <= 100000; j += i) notPrime[j] = true;
- }
- }
- string thePossible(int N, int target) {
- if (N % target != 0) return "No";
- if (N == target) return "Yes";
- sieve();
- if (!notPrime[target]) return "Yes";
- int pp = (int)(sqrt(target) + 1e-8);
- for (; N % pp == 0; N /= pp);
- if (!notPrime[pp] && pp * pp == target && N == 1) return "Yes";
- return "No";
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement