Skip to content

Commit fa79784

Browse files
authored
Merge branch 'main' into ahwan
2 parents f22415f + 3ae84f6 commit fa79784

22 files changed

Lines changed: 628 additions & 0 deletions

File tree

.DS_Store

0 Bytes
Binary file not shown.

Problems/.DS_Store

0 Bytes
Binary file not shown.

Problems/Mathematics/.DS_Store

2 KB
Binary file not shown.
0 Bytes
Binary file not shown.
8 KB
Binary file not shown.
6 KB
Binary file not shown.
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*We have:
2+
3+
An even number of people n standing in a circle, numbered 1 to n clockwise.
4+
5+
Each person looks directly opposite in the circle.
6+
7+
You are given three numbers: a, b, c.
8+
9+
a is looking at b.
10+
11+
We want to know who c is looking at.
12+
13+
If no circle is possible → return -1.
14+
15+
*/
16+
17+
typedef long long ll;
18+
#include <bits/stdc++.h>
19+
using namespace std;
20+
21+
int main() {
22+
int t;
23+
cin>>t;
24+
while(t--){
25+
ll a, b, c;
26+
cin >> a >> b >> c;
27+
28+
ll diff = abs(a - b);
29+
ll n = 2 * diff;
30+
31+
if (diff==0 || max({a,b,c})> n){
32+
cout << -1 << endl;
33+
} else {
34+
ll d = c + diff;
35+
if (d > n) d -= n;
36+
cout << d << endl;
37+
}
38+
}
39+
return 0;
40+
}
41+
42+
//O(1) per test case
43+
//My SUBMISSION : https://codeforces.com/contest/1560/submission/355307599
1.57 KB
Binary file not shown.
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// Link to Submission: https://codeforces.com/contest/1225/submission/355410192
2+
3+
/*
4+
Problem Statement:
5+
You are given n positive integers a1, …, an, and an integer k ≥ 2.
6+
Count the number of pairs i, j such that 1 ≤ i < j ≤ n, and there exists an integer x such that ai ⋅ aj = x^k.
7+
*/
8+
9+
/*
10+
Brief Explanation:
11+
To avoid TLE we avoid comparing each combination of elements, rather we calculate the reduced form of each element in the array.
12+
The reduced form of an element can be calculated as -> A = q^x * p^y... ~ q^(x % k) * p^(y %k)...
13+
We then get the complement i.e. Ac ~ q^((k - (x % k)) % k) * p^((k - (y % k)) % k)
14+
While iterating through the array, we maintain a frequency map of reduced forms.
15+
For each element, we count how many previously seen reduced forms equal its complement and add that to the pair count.
16+
*/
17+
18+
import java.util.*;
19+
20+
public class Solution2 {
21+
static long[] reducedAndComplement(long x, int k) {
22+
long rf = 1;
23+
long comp = 1;
24+
25+
for (long p = 2; p * p <= x; p++) {
26+
int count = 0;
27+
28+
while (x % p == 0) {
29+
x /= p;
30+
count++;
31+
}
32+
33+
count %= k;
34+
35+
for (int i = 0; i < count; i++)
36+
rf *= p;
37+
38+
int req = (k - count) % k;
39+
for (int i = 0; i < req; i++)
40+
comp *= p;
41+
}
42+
43+
if (x > 1) {
44+
rf *= x;
45+
int req = (k - 1) % k;
46+
for (int i = 0; i < req; i++)
47+
comp *= x;
48+
}
49+
50+
return new long[]{rf, comp};
51+
}
52+
53+
public static void main(String[] args) {
54+
Scanner sc = new Scanner(System.in);
55+
HashMap<Long, Integer> freq = new HashMap<>();
56+
int n = sc.nextInt();
57+
int k = sc.nextInt();
58+
59+
long fc = 0;
60+
61+
for (int i = 0; i < n; i++) {
62+
long a = sc.nextLong();
63+
long[] rc = reducedAndComplement(a, k);
64+
long r = rc[0];
65+
long c = rc[1];
66+
67+
fc += freq.getOrDefault(c, 0);
68+
69+
freq.put(r, freq.getOrDefault(r, 0) + 1);
70+
}
71+
72+
System.out.println(fc);
73+
74+
sc.close();
75+
}
76+
}
77+
78+
// Time Complexity: O(n * sqr.root(A))
79+
// Space Complexity: O(n)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
Problem Statement:
3+
Given three distinct integers a, b, and c placed on a circle of even size,
4+
numbered clockwise from 1. Person a is opposite to person b.
5+
Find the number opposite to c. If no such circle exists, return -1.
6+
7+
Prefix Sums:
8+
Not applicable since the problem is purely mathematical and does not involve
9+
range queries or cumulative sums.
10+
11+
Time Complexity: O(1)
12+
Space Complexity: O(1)
13+
14+
Submission Link:
15+
https://codeforces.com/contest/1560/submission/355443029
16+
*/
17+
#include <bits/stdc++.h>
18+
using namespace std;
19+
20+
int main(){
21+
22+
int t;
23+
cin>>t;
24+
while(t--){
25+
int a,b,c,n;
26+
cin>>a>>b>>c;
27+
if(a>b) swap(a,b); //naming the bigger number b and smaller one a
28+
29+
int SizeOfCircle= 2*(b-a); // thinking: b and a form the diameter of circle
30+
31+
if(SizeOfCircle==2) cout<<-1<<endl; //edge case when a and b are adjacent
32+
else{
33+
if(c>SizeOfCircle|| b>SizeOfCircle) cout<<-1<<endl; // edge case when either c or the bigger number do not reside in the size
34+
else{
35+
int ans=0;
36+
if(c>b) ans=(c-(b-a)); // because a and b form diameter then c lies in upper or lower half and opposite of c also lies based on that
37+
else if(c<=b) ans=(c+(b-a));
38+
39+
if(ans<=0) ans+=SizeOfCircle;
40+
else if(ans>SizeOfCircle) ans-=SizeOfCircle; // fixing ans if it resides outside elements of circle
41+
42+
cout<< ans<<endl;
43+
}
44+
45+
46+
}
47+
}
48+
return 0;
49+
}

0 commit comments

Comments
 (0)