Skip to content

Commit 9abe56a

Browse files
authored
Merge branch 'main' into main
2 parents 5493d69 + e8f9e60 commit 9abe56a

29 files changed

Lines changed: 1380 additions & 51 deletions

File tree

.DS_Store

0 Bytes
Binary file not shown.

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
.vscode/

Problems/.DS_Store

0 Bytes
Binary file not shown.

Problems/Mathematics/.DS_Store

0 Bytes
Binary file not shown.
0 Bytes
Binary file not shown.
4 KB
Binary file not shown.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//#include <bits/stdc++.h>
2+
#include <iostream>
3+
#include <vector>
4+
#include <string>
5+
#include <algorithm>
6+
#include <map>
7+
#include <set>
8+
#include <unordered_map>
9+
#include <queue>
10+
#include <stack>
11+
#include <cmath>
12+
#include <climits>
13+
#define int long long
14+
#define fo(n) for (int i = 0; i < n; i++)
15+
#define fo1(ii, n) for(int i=ii; i<n; i++)
16+
#define all(x) x.begin(), x.end()
17+
#define rall(x) x.rbegin(), x.rend()
18+
#define pb push_back
19+
#define fi first
20+
#define se second
21+
#define vec(a) vector<int> a
22+
#define vecn(a,n) vector<int> a(n)
23+
#define py cout<<"YES"<<endl
24+
#define pn cout<<"NO"<<endl
25+
const int MOD = 1e9 + 7;
26+
const int INF = 1e18;
27+
using namespace std;
28+
29+
void solve(){
30+
int a,b,c;
31+
cin>>a>>b>>c;
32+
//a->b
33+
//c->?
34+
int total=abs(a-b)*2;
35+
int half=total/2;
36+
if(max({a,b,c})>total){
37+
cout<<-1<<endl;
38+
return;
39+
}
40+
else if(c<=total){
41+
if(c>half){
42+
cout<<c-half<<endl;
43+
}
44+
else cout<<c+half<<endl;
45+
}
46+
}
47+
48+
signed main() {
49+
ios_base::sync_with_stdio(false);
50+
cin.tie(0);
51+
int t;
52+
cin >> t;
53+
while(t--)
54+
solve();
55+
return 0;
56+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
4+
static const int maxi = 100000;
5+
int spf[maxi + 1];
6+
int k;
7+
8+
/*sieve*/
9+
void buildSPF() {
10+
for (int i = 1; i <= maxi; i++)
11+
spf[i] = i;
12+
13+
for (int i = 2; i * i <= maxi; i++) {
14+
if (spf[i] == i) {
15+
for (int j = i * i; j <= maxi; j += i) {
16+
if (spf[j] == j)
17+
spf[j] = i;
18+
}
19+
}
20+
}
21+
}
22+
23+
/*factorise*/
24+
vector<pair<int,int>> getSignature(int x) {
25+
map<int,int> factors;
26+
27+
while (x > 1) {
28+
int p = spf[x];
29+
factors[p]++;
30+
x /= p;
31+
}
32+
33+
vector<pair<int,int>> sig;
34+
for (auto &it : factors) {
35+
int exp = it.second % k;
36+
if (exp != 0)
37+
sig.push_back({it.first, exp});
38+
}
39+
return sig;
40+
}
41+
42+
/*how many needed*/
43+
vector<pair<int,int>> getComplement(const vector<pair<int,int>> &sig) {
44+
vector<pair<int,int>> comp;
45+
for (auto &p : sig) {
46+
comp.push_back({p.first, (k - p.second) % k});
47+
}
48+
return comp;
49+
}
50+
51+
int main() {
52+
ios::sync_with_stdio(false);
53+
cin.tie(nullptr);
54+
55+
int n;
56+
cin >> n >> k;
57+
58+
vector<int> a(n);
59+
for (int i = 0; i < n; i++)
60+
cin >> a[i];
61+
62+
buildSPF();
63+
64+
map<vector<pair<int,int>>, long long> freq;
65+
long long ans = 0;
66+
67+
for (int i = 0; i < n; i++) {
68+
vector<pair<int,int>> sig = getSignature(a[i]);
69+
vector<pair<int,int>> need = getComplement(sig);
70+
ans += freq[need];
71+
freq[sig]++;
72+
}
73+
cout << ans << "\n";
74+
return 0;
75+
}
76+
//https://codeforces.com/problemset/submission/1225/355302622
77+
//time complexity O(nlognlog(10^5))
6 KB
Binary file not shown.
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/* Submission Link - https://codeforces.com/problemset/submission/1560/355259349
2+
Problem: Codeforces 1560B - Who's Opposite?
3+
4+
Explanation:
5+
6+
People are standing on a circle, numbered from 1 to n (n is even).
7+
Each person looks at the person exactly opposite them on the circle.
8+
9+
Key observation:
10+
If a person at position x looks at position y, then the distance between
11+
them along the circle must be exactly n/2.
12+
13+
Given:
14+
a looks at b
15+
=> |a - b| = n/2
16+
17+
So the total number of people in the circle must be:
18+
n = 2 * |a - b|
19+
20+
Validity check:
21+
All positions a, b, and c must lie within the range [1, n].
22+
If max(a, b, c) > n, then such a circle is impossible → output -1.
23+
24+
Finding who c looks at:
25+
The opposite position of c is c + (n/2).
26+
If this exceeds n, we wrap around the circle by subtracting n.
27+
28+
Edge case:
29+
If a == b, then |a - b| = 0 → invalid (no circle possible).
30+
31+
Algorithm:
32+
1. Compute diff = |a - b|
33+
2. Set n = 2 * diff
34+
3. If diff == 0 or max(a, b, c) > n → print -1
35+
4. Else compute d = c + diff
36+
If d > n, d -= n
37+
5. Print d
38+
39+
Time Complexity: O(1) per test case
40+
Space Complexity : O(1) as number of test cases are fixed */
41+
42+
#include <bits/stdc++.h>
43+
using namespace std;
44+
45+
int main(){
46+
ios::sync_with_stdio(false);
47+
cin.tie(nullptr);
48+
49+
int t;
50+
cin >> t;
51+
while(t--){
52+
long long a, b, c;
53+
cin >> a >> b >> c;
54+
55+
long long diff = llabs(a - b);
56+
long long n = 2 * diff;
57+
58+
if(diff == 0 || max({a, b, c}) > n){
59+
cout << -1 << "\n";
60+
} else {
61+
long long d = c + diff;
62+
if(d > n) d -= n;
63+
cout << d << "\n";
64+
}
65+
}
66+
return 0;
67+
}

0 commit comments

Comments
 (0)