Skip to content

Commit a41a086

Browse files
authored
Merge branch 'opencodeiiita:main' into main
2 parents 3fd29e4 + 05bb5f5 commit a41a086

19 files changed

Lines changed: 1189 additions & 3 deletions

File tree

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/* 1 to n people are standing in a circle. n is even.... a is looking at b.
2+
If given c is looking at some d person for given a,b then find that d. If such a combination
3+
of circle does not exist then print -1*/
4+
5+
// Time complexity: O(1)
6+
// Space complexity: O(1)
7+
//My submission: https://codeforces.com/contest/1560/submission/355204246
8+
9+
#include <bits/stdc++.h>
10+
using namespace std;
11+
12+
using ll = long long;
13+
#define all(x) (x).begin(), (x).end()
14+
15+
const int MOD = 1e9 + 7; // 998244353
16+
const ll INF = 1e18;
17+
18+
void solve() {
19+
int a,b,c;
20+
cin>>a>>b>>c;
21+
int n=2*abs(a-b);
22+
if(a>n || b>n ||c>n ) cout<<-1<<endl;
23+
else{
24+
int x=c-n/2,y=c+n/2;
25+
if(x>=1 && x<=n) cout<<x<<endl;
26+
else cout<<y<<endl;
27+
}
28+
}
29+
30+
int main() {
31+
// Fast I/O
32+
ios::sync_with_stdio(false);
33+
cin.tie(nullptr);
34+
35+
int t;
36+
cin >> t;
37+
while (t--) {
38+
solve();
39+
}
40+
41+
return 0;
42+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//https://codeforces.com/contest/1560/problem/C
2+
//https://codeforces.com/contest/1560/submission/355210608
3+
// TC : O(1) SC: O(1)
4+
#include <bits/stdc++.h>
5+
using namespace std;
6+
7+
int main() {
8+
int t;
9+
cin>>t;
10+
while(t--){
11+
int a,b,c;
12+
cin>>a>>b>>c;
13+
int k=abs(a-b);
14+
if(a>2*k || b>2*k || c>2*k){
15+
cout<<-1<<endl;
16+
}
17+
else{
18+
if((c-k)>0){
19+
cout<<c-k<<endl;
20+
}
21+
else{
22+
cout<<c+k<<endl;
23+
}
24+
}
25+
}
26+
27+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
int main() {
5+
int t;
6+
cin >> t;
7+
8+
while (t--) {
9+
int a, b, c;
10+
cin >> a >> b >> c;
11+
12+
int half = abs(a - b);
13+
int n = 2 * half;
14+
15+
if (c > n) {
16+
cout << -1 << "\n";
17+
} else {
18+
int opposite = (c + half <= n) ? (c + half) : (c - half);
19+
cout << opposite << "\n";
20+
}
21+
}
22+
return 0;
23+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
int main() {
5+
ios::sync_with_stdio(false);
6+
cin.tie(nullptr);
7+
8+
int n, k;
9+
cin >> n >> k;
10+
11+
vector<int> a(n);
12+
for (int &x : a) cin >> x;
13+
14+
map<vector<pair<int,int>>, long long> freq;
15+
long long ans = 0;
16+
17+
for (int x : a) {
18+
int temp = x;
19+
vector<pair<int,int>> cur, need;
20+
21+
for (int p = 2; p * p <= temp; p++) {
22+
if (temp % p == 0) {
23+
int cnt = 0;
24+
while (temp % p == 0) {
25+
temp /= p;
26+
cnt++;
27+
}
28+
cnt %= k;
29+
if (cnt) {
30+
cur.push_back({p, cnt});
31+
need.push_back({p, (k - cnt) % k});
32+
}
33+
}
34+
}
35+
36+
if (temp > 1) {
37+
cur.push_back({temp, 1 % k});
38+
need.push_back({temp, (k - 1) % k});
39+
}
40+
41+
ans += freq[need];
42+
freq[cur]++;
43+
}
44+
45+
cout << ans << "\n";
46+
return 0;
47+
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
Problem:
3+
You are given an array of n positive integers and an integer k (k >= 2).
4+
Count the number of unordered pairs (i, j) such that ai * aj is a perfect k-th power.
5+
6+
Approach:
7+
1. Precompute smallest prime factor (SPF) up to max(ai).
8+
2. For each number, compute its prime factorization.
9+
3. Reduce exponents modulo k to form a "signature".
10+
4. Compute the complementary signature needed to make exponents divisible by k.
11+
5. Use a map to count matching signatures seen so far.
12+
13+
Time Complexity: O(n log A)
14+
Space Complexity: O(n)
15+
16+
Problem Link:
17+
https://codeforces.com/problemset/problem/1225/D
18+
*/
19+
20+
#include <bits/stdc++.h>
21+
using namespace std;
22+
23+
static const int MAXA = 100000;
24+
25+
int main() {
26+
ios::sync_with_stdio(false);
27+
cin.tie(nullptr);
28+
29+
int n, k;
30+
cin >> n >> k;
31+
32+
vector<int> a(n);
33+
for (int i = 0; i < n; i++) {
34+
cin >> a[i];
35+
}
36+
37+
// Step 1: Compute Smallest Prime Factor (SPF)
38+
vector<int> spf(MAXA + 1);
39+
for (int i = 1; i <= MAXA; i++) spf[i] = i;
40+
41+
for (int i = 2; i * i <= MAXA; i++) {
42+
if (spf[i] == i) {
43+
for (int j = i * i; j <= MAXA; j += i) {
44+
if (spf[j] == j)
45+
spf[j] = i;
46+
}
47+
}
48+
}
49+
50+
// Map to store frequency of signatures
51+
map<vector<pair<int,int>>, long long> freq;
52+
long long answer = 0;
53+
54+
// Step 2–5: Process each number
55+
for (int x : a) {
56+
map<int,int> factorCount;
57+
58+
// Factorize using SPF
59+
while (x > 1) {
60+
int p = spf[x];
61+
factorCount[p]++;
62+
x /= p;
63+
}
64+
65+
vector<pair<int,int>> signature;
66+
vector<pair<int,int>> complement;
67+
68+
for (auto &it : factorCount) {
69+
int prime = it.first;
70+
int exp = it.second % k;
71+
72+
if (exp != 0) {
73+
signature.push_back({prime, exp});
74+
complement.push_back({prime, (k - exp) % k});
75+
}
76+
}
77+
78+
// Count valid pairs
79+
answer += freq[complement];
80+
81+
// Store current signature
82+
freq[signature]++;
83+
}
84+
85+
cout << answer << "\n";
86+
return 0;
87+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
int main() {
5+
int n, k;
6+
cin >> n >> k;
7+
8+
vector<int> a(n);
9+
for (int i = 0; i < n; i++)
10+
cin >> a[i];
11+
12+
map<vector<pair<int,int>>, long long> freq;
13+
long long ans = 0;
14+
15+
for (int i = 0; i < n; i++) {
16+
int x = a[i];
17+
vector<pair<int,int>> cur, need;
18+
19+
for (int p = 2; p * p <= x; p++) {
20+
if (x % p == 0) {
21+
int cnt = 0;
22+
while (x % p == 0) {
23+
x /= p;
24+
cnt++;
25+
}
26+
cnt %= k;
27+
if (cnt > 0) {
28+
cur.push_back({p, cnt});
29+
need.push_back({p, (k - cnt) % k});
30+
}
31+
}
32+
}
33+
34+
if (x > 1) {
35+
cur.push_back({x, 1});
36+
need.push_back({x, (k - 1) % k});
37+
}
38+
sort(cur.begin(), cur.end());
39+
sort(need.begin(), need.end());
40+
41+
ans += freq[need];
42+
freq[cur]++;
43+
}
44+
cout << ans << "\n";
45+
return 0;
46+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// submission link
2+
// https://codeforces.com/contest/1560/submission/355180160
3+
4+
// Space Complexity = O(1)
5+
// Time Complexity = O(1) for main solution code O(t) for overall code
6+
7+
8+
9+
// The if condition checks if the circle is valid and if the inputs exist within that circle.
10+
// abs(b-a)<=1: Ensures a and b are not adjacent or the same.
11+
// 2*(abs(b-a)-1)+2 < c || 2*(abs(b-a)-1)+2 < a || 2*(abs(b-a)-1)+2 < b. checks that the input numbers a, b, and c are not larger than the total size of the circle.
12+
// If any of these are true, it prints -1 .
13+
14+
// The line int k = 2*(abs(b-a)-1) + 2 calculates the total number of items in the circle.
15+
16+
// If the inputs are valid, it calculates the number opposite to c.
17+
// The halfway distance is k / 2.
18+
// If c is in the first half (c<= k/2), the opposite is c + k/2.
19+
// If c is in the second half (c > k/2), the opposite is c - k/2.
20+
21+
22+
23+
24+
#include<bits/stdc++.h>
25+
using namespace std;
26+
int main(){
27+
int t;
28+
cin>>t;
29+
while(t--){
30+
int a,b,c;
31+
cin>>a>>b>>c;
32+
if(abs(b-a)<=1 || 2*(abs(b-a)-1)+2 < c || 2*(abs(b-a)-1)+2 < a || 2*(abs(b-a)-1)+2 < b ){
33+
cout<<-1<<"\n";
34+
}
35+
else {
36+
int k = 2*(abs(b-a)-1) + 2;
37+
if(c<=k/2) cout<<c+k/2 <<"\n";
38+
else cout<<c-k/2<<"\n";
39+
}
40+
41+
}
42+
return 0;
43+
}

0 commit comments

Comments
 (0)