Skip to content

Commit bc4e849

Browse files
Merge branch 'main' into patch-1
2 parents f087a6d + bd58766 commit bc4e849

9 files changed

Lines changed: 699 additions & 0 deletions

File tree

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
n = number of elements (≤ 100,000)
3+
4+
A = maximum value of ai (≤ 100,000)
5+
6+
Space Complexity - 𝑂(𝐴+𝑛)
7+
8+
Time Complexity - O(nlogA)​
9+
10+
11+
*/
12+
13+
14+
#include <bits/stdc++.h>
15+
using namespace std;
16+
17+
int main() {
18+
ios::sync_with_stdio(false);
19+
cin.tie(nullptr);
20+
21+
int n, k;
22+
cin >> n >> k;
23+
24+
vector<int> a(n);
25+
for (int i = 0; i < n; i++) cin >> a[i];
26+
27+
// Smallest Prime Factor sieve
28+
const int MAXA = 100000;
29+
vector<int> spf(MAXA + 1);
30+
for (int i = 1; i <= MAXA; i++) spf[i] = i;
31+
for (int i = 2; i * i <= MAXA; i++) {
32+
if (spf[i] == i) {
33+
for (int j = i * i; j <= MAXA; j += i)
34+
if (spf[j] == j)
35+
spf[j] = i;
36+
}
37+
}
38+
39+
map<vector<pair<int,int>>, long long> freq;
40+
long long ans = 0;
41+
42+
for (int x : a) {
43+
map<int,int> cnt;
44+
45+
// factorization
46+
while (x > 1) {
47+
int p = spf[x];
48+
int c = 0;
49+
while (x % p == 0) {
50+
x /= p;
51+
c++;
52+
}
53+
cnt[p] += c;
54+
}
55+
56+
vector<pair<int,int>> sig, need;
57+
58+
for (auto &it : cnt) {
59+
int p = it.first;
60+
int e = it.second % k;
61+
if (e != 0) {
62+
sig.push_back({p, e});
63+
need.push_back({p, (k - e) % k});
64+
}
65+
}
66+
67+
sort(sig.begin(), sig.end());
68+
sort(need.begin(), need.end());
69+
70+
ans += freq[need];
71+
freq[sig]++;
72+
}
73+
74+
cout << ans << "\n";
75+
return 0;
76+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// https://codeforces.com/problemset/submission/1560/355220642
2+
3+
#include <iostream>
4+
#include <cmath>
5+
using namespace std;
6+
int main()
7+
{
8+
int t;
9+
cin >> t;
10+
while (t--)
11+
{
12+
long long a, b, c;
13+
cin >> a >> b >> c;
14+
long long order = fabs(a - b);
15+
if ((2 * order < a) || (2 * order < b) || (2 * order < c))
16+
cout << -1 << endl;
17+
else if (order < c)
18+
cout << c - order << endl;
19+
else
20+
cout << c + order << endl;
21+
}
22+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//https://codeforces.com/contest/1560/submission/355194892
2+
3+
#include <bits/stdc++.h>
4+
using namespace std;
5+
6+
void solve(){
7+
int a,b,c;
8+
cin>>a>>b>>c;
9+
int n=2*abs(a-b);
10+
11+
int opp=c+n/2;
12+
if(c>n||b>n||a>n){
13+
cout<<-1<<endl;
14+
return;
15+
}
16+
if(opp>n)opp=opp-n;
17+
18+
cout<<opp<<endl;
19+
}
20+
21+
int main(){
22+
int t;
23+
cin>>t;
24+
while(t--)solve();
25+
}

0 commit comments

Comments
 (0)