Skip to content

Commit a210e7d

Browse files
authored
Merge branch 'opencodeiiita:main' into main
2 parents f6f0ae4 + a758280 commit a210e7d

14 files changed

Lines changed: 717 additions & 12 deletions

File tree

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+
}
Lines changed: 117 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,117 @@
1+
/*PROBLEM STATEMENT :
2+
3+
There are n people standing in a circle. They are numbered from 1 to n in a clockwise order. The circle is even (i.e. n is even).
4+
5+
Each person is looking at the person standing exactly opposite him in the circle.
6+
7+
You are given three distinct integers a, b, and c. It is known that person a is looking at person b.
8+
9+
Determine the number of the person that person c is looking at.
10+
If there are multiple answers, print any of them.
11+
If there is no answer, print -1.
12+
13+
Approach : Total(i.e. n) = 2*(difference of a and b), so if any of the a,b,c is greater than total, it will return -1. Else we should return c + |a-b| or c-|a-b| (only one of them will be feasible)
14+
15+
Time Complexity: O(1) because we have created a formula directly.
16+
Space Complexity : O(1) because no. of inputs is fixed
17+
18+
Input
19+
7
20+
6 2 4
21+
2 3 1
22+
2 4 10
23+
5 3 4
24+
1 3 2
25+
2 5 4
26+
4 3 2
27+
Output
28+
8
29+
-1
30+
-1
31+
-1
32+
4
33+
1
34+
-1
35+
36+
Submission link : https://codeforces.com/contest/1560/submission/355191015
37+
*/
38+
39+
#include<bits/stdc++.h>
40+
#include<array>
41+
#include<unordered_set>
42+
using namespace std;
43+
typedef long long ll;
44+
#define int long long int
45+
#define F first
46+
#define S second
47+
#define pb push_back
48+
#define si set <int>
49+
#define vi vector <int>
50+
#define pii pair <int, int>
51+
#define vpi vector <pii>
52+
#define vpp vector <pair<int, pii>>
53+
#define mii map <int, int>
54+
#define mpi map <pii, int>
55+
#define spi set <pii>
56+
#define endl "\n"
57+
#define sz(x) ((int) x.size())
58+
#define all(p) p.begin(), p.end()
59+
#define double long double
60+
#define que_max priority_queue <int>
61+
#define que_min priority_queue <int, vi, greater<int>>
62+
#define bug(...) __f (#__VA_ARGS__, __VA_ARGS__)
63+
#define vin(a) for(int i = 0; i< a.size();i++) cin>>a[i]
64+
#define vout(a) {for(auto x : a) cout << x << " "; cout << endl;}
65+
#define pp(a) for(auto x : a) cout << x.F << " " << x.S << endl
66+
#define print(a,x,y) {for(int i = x; i < y; i++) cout<< a[i]<< " "; cout << endl}
67+
68+
inline int power(int a, int b){int x = 1;while (b){if (b & 1) x *= a;a *= a;b >>= 1;}return x;}
69+
template <typename Arg1>
70+
void __f (const char* name, Arg1&& arg1) { cout << name << " : " << arg1 << endl; }
71+
template <typename Arg1, typename... Args>
72+
void __f (const char* names, Arg1&& arg1, Args&&... args)
73+
{
74+
const char* comma = strchr (names + 1, ',');
75+
cout.write (names, comma - names) << " : " << arg1 << " | "; __f (comma + 1, args...);
76+
}
77+
78+
bool isPrime(int n){ if(n <= 1) return false; for(int i=2;i*i<=n;i++) if(n % i == 0) return false; return true; }
79+
ll gcd(ll a, ll b){ return b ? gcd(b, a % b) : a; }
80+
ll lcm(ll a, ll b){ return a / gcd(a,b) * b; }
81+
ll modpow(ll a, ll b, ll m){ ll r=1; while(b){ if(b&1) r=r*a%m; a=a*a%m; b>>=1;} return r; }
82+
83+
bool getBit(ll x, int k){ return (x >> k) & 1; }
84+
ll setBit(ll x, int k){ return x | (1LL << k); }
85+
ll clearBit(ll x, int k){ return x & ~(1LL << k); }
86+
ll toggleBit(ll x, int k){ return x ^ (1LL << k); }
87+
int countBits(ll x){ return __builtin_popcountll(x); }
88+
bool isPowerOfTwo(ll x){ return x && !(x & (x - 1)); }
89+
90+
vector<int> sieve(int n){ vector<int> p(n+1,1), primes; p[0]=p[1]=0; for(int i=2;i*i<=n;i++) if(p[i]) for(int j=i*i;j<=n;j+=i) p[j]=0; for(int i=2;i<=n;i++) if(p[i]) primes.pb(i); return primes; }
91+
92+
const int N = 200005;
93+
94+
void solve() {
95+
int a,b,c;
96+
cin >> a >> b >> c;
97+
98+
int total = 2*abs(a-b);
99+
if (max(max(a,b),c)>total)
100+
{
101+
cout << -1 << endl;return;
102+
}
103+
else
104+
{
105+
int ans = ((c-abs(a-b))>0 )? c-abs(a-b) : c+ abs(a-b) ;
106+
cout << ans << endl;return;
107+
}
108+
}
109+
110+
int32_t main()
111+
{
112+
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
113+
int t = 1;
114+
cin >> t;
115+
while (t--) solve();
116+
return 0;
117+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"name":"Local: Solution1","url":"c:\\Project\\OpenCode25\\CP-Chronicles\\Problems\\Mathematics\\Day-01\\sol\\Satwik_Santosh\\Solution1.cpp","tests":[{"id":1766771594036,"input":"","output":""}],"interactive":false,"memoryLimit":1024,"timeLimit":3000,"srcPath":"c:\\Project\\OpenCode25\\CP-Chronicles\\Problems\\Mathematics\\Day-01\\sol\\Satwik_Santosh\\Solution1.cpp","group":"local","local":true}

0 commit comments

Comments
 (0)