본문 바로가기

백준/수학

[ICPC] 백준 28152 - Power of Divisors

728x90

https://www.acmicpc.net/problem/28152

힌트 1

더보기

문제에서 유효한 해가 존재할 때 $f(n)$의 값은 충분히 작다.

힌트 2

더보기

$n^{f(n)} = x$라는 조건에 의해 $x$는 어떤 수의 거듭제곱으로 이루어져야만 함을 확인할 수 있다.

 

힌트 1과 힌트 2에서 언급한 사실을 종합했을 때, 후보를 빠르게 추릴 수 있는 방법이 있는가?

풀이

더보기

$2^{64} > 10^{18}$이므로, $f(n)$이 가질 수 있는 최댓값을 64로 잡는다.

 

그러면 1부터 64까지 반복을 돌면서 $1 \leq i \leq 64$에 대해 어떤 정수 $a$가 $a^{i} = x$인지 $a$에 대해 이진탐색을 시행하여 확인할 수 있다.

 

$a^{i} = x$를 만족하는 a를 찾을 경우, $f(a) = i$인지 $O(\sqrt a)$에 확인할 수 있다.

 

이렇게 해서 적절한 $a$를 찾거나, 없으면 -1을 출력하면 된다.

 

정답 코드 - C++

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#pragma GCC target("fma,avx,avx2,bmi,bmi2,lzcnt,popcnt")
#pragma GCC optimize("O3,unroll-loops")
 
#include "bits/stdc++.h"
#include "ext/pb_ds/assoc_container.hpp"
 
using namespace std;
using namespace __gnu_pbds;
 
using ll = int_fast64_t;
using pii = pair<intint>;
// using oset = tree<int, null_type, less<>, rb_tree_tag, tree_order_statistics_node_update>;
 
ll x;
 
void solve()
{
  cin >> x;
 
  if (x == 1)
  {
    cout << 1;
    return;
  }
 
  for (int i = 1; i <= 64; i++)
  {
    __int128_t lo = 1, hi = 1e9;
 
    ll good = -1;
    ll cand = -1;
    while (lo <= hi)
    {
      auto mid = lo + (hi - lo) / 2;
 
      __int128_t val = 1;
      for (int j = 0; j < i; j++)
      {
        val *= mid;
        if (val > x) break;
      }
 
      if (val == x)
      {
        good = mid;
        cand = mid;
        break;
      }
 
      if (val > x)
      {
        hi = mid - 1;
      }
      else
      {
        lo = mid + 1;
      }
    }
 
    if (good == -1continue;
 
    map<intint> mp;
 
    for (int i = 2; (ll)i * i <= good; i++)
    {
      if (good % i) continue;
      while (good % i == 0)
      {
        ++mp[i];
        good /= i;
      }
    }
 
    if (good != 1)
    {
      ++mp[good];
    }
 
 
    int cnt = 1;
 
    for (auto [k, v] : mp)
    {
      cnt *= (v + 1);
    }
 
    if (cnt == i)
    {
      cout << cand;
      return;
    }
  }
 
  cout << -1;
}
 
int main()
{
#ifdef LOCAL
  freopen("input.txt""r", stdin);
//  freopen("output.txt", "w", stdout);
#endif
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
 
  solve();
}
cs

 

 

 

 

 

728x90

'백준 > 수학' 카테고리의 다른 글

백준 12994 - 이동 3-2  (0) 2025.01.22
백준 31002 - 그래프 변환  (0) 2025.01.11
백준 27437 - 분수찾기 2  (1) 2024.11.13
백준 18287 - 체스판 이동  (0) 2022.08.27
[ICPC] 백준 9341 - ZZ  (0) 2022.08.26