File tree Expand file tree Collapse file tree
Problems/Mathematics/Day-01/sol/Rushalverma Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ /*
2+ https://codeforces.com/contest/1225/submission/355245357
3+
4+ time complexity: Factorization of each number using seive primes O(log maxA)
5+ total factorization cost - O(nlog maxA)
6+ Each number produces a vector of prime–exponent pairs mod k -> O(log maxA)
7+ total map work - O(nlong maxA)
8+ total complexity = O(nlog maxA)
9+
10+
11+
12+ */
13+
14+ #include < bits/stdc++.h>
15+ using namespace std ;
16+
17+ using ll = long long ;
18+
19+ int main (){
20+ ios::sync_with_stdio (false );
21+ cin.tie (nullptr );
22+
23+ int n;
24+ ll k;
25+ cin >> n >> k;
26+
27+ vector<ll> a (n);
28+ ll mx = 0 ;
29+ for (int i = 0 ; i < n; i++){
30+ cin >> a[i];
31+ mx = max (mx, a[i]);
32+ }
33+
34+
35+ ll LIM = sqrt (mx) + 2 ;
36+ vector<int > primes;
37+ vector<bool > is_prime (LIM+1 , true );
38+ is_prime[0 ] = is_prime[1 ] = false ;
39+ for (int i = 2 ; i <= LIM; i++){
40+ if (is_prime[i]){
41+ primes.push_back (i);
42+ for (ll j = 1LL * i * i; j <= LIM; j += i)
43+ is_prime[j] = false ;
44+ }
45+ }
46+
47+ unordered_map<string, long long > cnt;
48+ long long ans = 0 ;
49+
50+ for (int idx = 0 ; idx < n; idx++){
51+ ll x = a[idx];
52+
53+ vector<pair<ll,ll>> v;
54+
55+ for (int p: primes){
56+ if (1LL * p * p > x) break ;
57+ if (x % p == 0 ){
58+ ll c = 0 ;
59+ while (x % p == 0 ){
60+ x /= p;
61+ c++;
62+ }
63+ c %= k;
64+ if (c) v.push_back ({p, c});
65+ }
66+ }
67+
68+ if (x > 1 ) v.push_back ({x, 1 % k});
69+
70+ string key = " " ;
71+ for (auto &pr: v){
72+ key += to_string (pr.first ) + " #" + to_string (pr.second ) + " |" ;
73+ }
74+
75+ vector<pair<ll,ll>> u;
76+ for (auto &pr: v){
77+ ll need = (k - pr.second ) % k;
78+ if (need) u.push_back ({pr.first , need});
79+ }
80+
81+ string key2 = " " ;
82+ for (auto &pr: u){
83+ key2 += to_string (pr.first ) + " #" + to_string (pr.second ) + " |" ;
84+ }
85+
86+ ans += cnt[key2];
87+
88+
89+ cnt[key]++;
90+ }
91+
92+ cout << ans << " " ;
93+ return 0 ;
94+ }
You can’t perform that action at this time.
0 commit comments