Skip to content

Commit 66b1e37

Browse files
authored
Merge branch 'opencodeiiita:main' into main
2 parents cb97c8d + 96a1225 commit 66b1e37

20 files changed

Lines changed: 1451 additions & 0 deletions

File tree

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
#include <bits/stdc++.h>
2+
using namespace std;
3+
/*
4+
Logic:
5+
The main key in solving the problem is that a,b will be enought to know that the size of the area.
6+
Another observation would be that 1 is always facing the element more than the element which faces
7+
at the last element or the size of the circle.
8+
Once, you get the size of the array, we just have to check if the any of the elements are larger than
9+
the size of the array.
10+
On deeper checks like- a=3, b=2,c=1;
11+
we can observe how this isn't true but how does the conditon fulfill the case?
12+
--> the max check covers all small checks, like the circle don being possible
13+
in cases like a=1,b=6, c=100
14+
--> the circle condition is also checked by the max case.
15+
16+
So, after the checking, we need to see the answer, that can be easily given as the
17+
difference is same for all pairs of elements modular nature of the circle( cool words that make sense)
18+
19+
Time complexity-->
20+
21+
The time complexity is O(1)
22+
The space complexity is O(1)
23+
24+
Submission link- https://codeforces.com/contest/1560/submission/355188467
25+
*/
26+
//Soln->
27+
int main() {
28+
ios::sync_with_stdio(false);
29+
cin.tie(nullptr);
30+
31+
int t;
32+
cin >> t;
33+
34+
while (t--) {
35+
int a,b,c;
36+
cin>>a;
37+
cin>>b;
38+
cin>>c;
39+
int x=abs(b-a);
40+
if(max({a,b,c})>(2*x))
41+
cout<<-1<<endl;
42+
else
43+
{
44+
if(c>x)
45+
cout<<c-x<<endl;
46+
else
47+
cout<<c+x<<endl;
48+
}
49+
50+
}
51+
return 0;
52+
}
53+
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: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
//https://codeforces.com/contest/1560/submission/355205808
2+
//time complexity:O(t)
3+
//space complexity:O(1)
4+
#include <iostream>
5+
using namespace std;
6+
7+
int main()
8+
{
9+
int t;
10+
cin>>t;
11+
while(t--){
12+
int a,b,c;
13+
cin>>a>>b>>c;
14+
int len;
15+
if(a>b){
16+
len=a-b;
17+
len=2*len;
18+
} // staring at opposite end means both of them divide the circle
19+
// in exact half so we substract them and multiply by 2 to get exact length
20+
else {
21+
22+
len=b-a;
23+
len=2*len;
24+
25+
}
26+
if(a<=len&&b<=len&&c<=len){//a,b,c ofcourse cannot be greater than length
27+
if(c+(len/2)>len){
28+
cout<<c-(len/2)<<endl;//c will either stare at +n/2 or -n/2
29+
}
30+
else{
31+
cout<<c+(len/2)<<endl;
32+
}
33+
}
34+
else{cout<<-1<<endl;}
35+
}
36+
37+
return 0;
38+
}
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)