Skip to content

Commit 05bb5f5

Browse files
authored
Merge pull request #152 from ishanrajsingh/solution1-day01-mathematics
2 parents 8e69446 + d7a7a1b commit 05bb5f5

1 file changed

Lines changed: 365 additions & 0 deletions

File tree

Lines changed: 365 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,365 @@
1+
//Submission Link - https://codeforces.com/problemset/submission/1560/355220034
2+
#include <bits/stdc++.h>
3+
using namespace std;
4+
5+
#pragma GCC optimize("O3,unroll-loops")
6+
#pragma GCC target("avx2")
7+
8+
#define ll long long
9+
#define ull unsigned long long
10+
#define ld long double
11+
#define pb push_back
12+
#define mp make_pair
13+
#define f first
14+
#define s second
15+
#define all(x) x.begin(), x.end()
16+
#define rep(i,a,b) for (int i = a; i < b; ++i)
17+
#define rrep(i,a,b) for (int i = a; i >= b; --i)
18+
#define in(a,n) rep(i,0,n) cin>>a[i]
19+
#define yes cout<<"YES\n"
20+
#define no cout<<"NO\n"
21+
#define int long long
22+
23+
typedef vector<int> vi;
24+
typedef vector<ll> vl;
25+
typedef pair<int,int> pi;
26+
typedef pair<ll,ll> pl;
27+
typedef vector<pi> vpi;
28+
typedef vector<pl> vpl;
29+
30+
const ll MOD = 1e9 + 7;
31+
const ll INF = 1e18;
32+
const int N = 2e5 + 5;
33+
34+
ll gcd(ll a, ll b) {
35+
while (b) a %= b, swap(a, b);
36+
return a;
37+
}
38+
39+
ll lcm(ll a, ll b) {
40+
return a / gcd(a, b) * b;
41+
}
42+
43+
ll power(ll a, ll b, ll mod = MOD) {
44+
ll res = 1; a %= mod;
45+
while (b > 0) {
46+
if (b & 1) res = res * a % mod;
47+
a = a * a % mod;
48+
b >>= 1;
49+
}
50+
return res;
51+
}
52+
53+
ll invmod(ll a, ll mod = MOD) {
54+
return power(a, mod - 2, mod);
55+
}
56+
57+
ll fact[N], invfact[N];
58+
void precompute() {
59+
fact[0] = invfact[0] = 1;
60+
rep(i,1,N) fact[i] = fact[i-1]*i % MOD;
61+
invfact[N-1] = invmod(fact[N-1]);
62+
rrep(i,N-2,1) invfact[i] = invfact[i+1]*(i+1) % MOD;
63+
}
64+
65+
ll nCr(int n, int r) {
66+
if (r > n || r < 0) return 0;
67+
return fact[n] * invfact[r] % MOD * invfact[n - r] % MOD;
68+
}
69+
70+
vector<bool> is_prime;
71+
vi primes;
72+
void sieve(int maxn = N) {
73+
is_prime.assign(maxn + 1, true);
74+
is_prime[0] = is_prime[1] = false;
75+
for (int i = 2; i * i <= maxn; ++i) {
76+
if (is_prime[i]) {
77+
for (int j = i * i; j <= maxn; j += i)
78+
is_prime[j] = false;
79+
}
80+
}
81+
rep(i, 2, maxn + 1) if (is_prime[i]) primes.pb(i);
82+
}
83+
tuple<ll, int, int> kadane_with_indices(const vl &a) {
84+
ll max_sum = a[0], cur = a[0];
85+
int start = 0, end = 0, temp_start = 0;
86+
87+
rep(i, 1, a.size()) {
88+
if (a[i] > cur + a[i]) {
89+
cur = a[i];
90+
temp_start = i;
91+
} else {
92+
cur += a[i];
93+
}
94+
95+
if (cur > max_sum) {
96+
max_sum = cur;
97+
start = temp_start;
98+
end = i;
99+
}
100+
if (cur == 0) {
101+
temp_start = i + 1;
102+
}
103+
}
104+
105+
return {max_sum, start, end};
106+
}
107+
108+
// Disjoint Set Union (DSU)
109+
struct DSU {
110+
vi parent, size;
111+
DSU(int n) {
112+
parent.resize(n);
113+
size.assign(n, 1);
114+
iota(all(parent), 0);
115+
}
116+
117+
int find(int x) {
118+
return x == parent[x] ? x : parent[x] = find(parent[x]);
119+
}
120+
121+
bool unite(int x, int y) {
122+
x = find(x); y = find(y);
123+
if (x == y) return false;
124+
if (size[x] < size[y]) swap(x, y);
125+
parent[y] = x;
126+
size[x] += size[y];
127+
return true;
128+
}
129+
};
130+
131+
// Grid Traversal (4 & 8 directions)
132+
int dx[4] = {0, 0, 1, -1};
133+
int dy[4] = {1, -1, 0, 0};
134+
int ddx[8] = {0,0,1,-1,1,-1,1,-1};
135+
int ddy[8] = {1,-1,0,0,1,-1,-1,1};
136+
137+
bool DEBUG = true;
138+
#define debug(x) if(DEBUG) cout << "[DEBUG] " << #x << " = " << (x) << endl
139+
140+
141+
#undef int
142+
#include <ext/pb_ds/assoc_container.hpp>
143+
#include <ext/pb_ds/tree_policy.hpp>
144+
#define int long long
145+
146+
using namespace __gnu_pbds;
147+
template <typename T>
148+
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
149+
150+
// Functions:
151+
// s.find_by_order(k) ? k-th element
152+
// s.order_of_key(x) ? number of elements < x
153+
154+
155+
template <typename T>
156+
ostream& operator<<(ostream& os, const vector<T>& v) {
157+
for (int i = 0; i < (int)v.size(); i++) {
158+
if (i) os << " ";
159+
os << v[i];
160+
}
161+
return os;
162+
}
163+
template <typename T>
164+
istream& operator>>(istream &is, vector<T> &v) {
165+
for (auto &x : v) is >> x;
166+
return is;
167+
}
168+
template <typename K, typename V>
169+
ostream& operator<<(ostream &os, const map<K,V> &mp) {
170+
os << "{ ";
171+
for (auto &p : mp) {
172+
os << p.first << ":" << p.second << " ";
173+
}
174+
os << "}";
175+
return os;
176+
}
177+
#define debug(x) if(DEBUG){ cout << "[DEBUG] " << #x << " = "; cout << (x) << endl; }
178+
179+
180+
int mex(vector<int>& v) { // minimum excluded value
181+
unordered_set<int> s(v.begin(), v.end());
182+
int m = 0;
183+
while (s.count(m)) m++;
184+
return m;
185+
}
186+
187+
int setbit(int n, int pos) { return n | (1LL << pos); }
188+
int unsetbit(int n, int pos) { return n & ~(1LL << pos); }
189+
int togglebit(int n, int pos) { return n ^ (1LL << pos); }
190+
bool checkbit(int n, int pos) { return n & (1LL << pos); }
191+
192+
// Count set bits
193+
int count_bits(int n) { return __builtin_popcountll(n); }
194+
195+
// Index of LSB/MSB
196+
int lsb(int n) { return __builtin_ctzll(n); } // least significant set bit index
197+
int msb(int n) { return 63 - __builtin_clzll(n); } // most significant set bit index
198+
199+
200+
long long sumOfDigits(long long n) {
201+
if (n < 10) return n * (n + 1) / 2; // direct sum
202+
203+
long long d = log10(n); // number of digits - 1
204+
long long p = pow(10, d); // largest power of 10 <= n
205+
long long msd = n / p; // most significant digit
206+
207+
// formula:
208+
// sum of digits from 1..n =
209+
// (msd * sumOfDigits(p-1)) [complete blocks before]
210+
//+ (msd * (msd - 1) / 2) * p [contribution of MSD itself]
211+
//+ (msd * (1 + n % p)) [MSD contribution in current block]
212+
//+ sumOfDigits(n % p) [recurse on remaining suffix]
213+
214+
return (msd * sumOfDigits(p - 1)) +
215+
(msd * (msd - 1) / 2) * p +
216+
(msd * (1 + n % p)) +
217+
sumOfDigits(n % p);
218+
}
219+
220+
vector<long long> digitCount(long long n) {
221+
vector<long long> cnt(10, 0);
222+
for (long long pos = 1; pos <= n; pos *= 10) {
223+
long long higher = n / (pos * 10);
224+
long long cur = (n / pos) % 10;
225+
long long lower = n % pos;
226+
227+
// count for each digit
228+
for (int d = 0; d <= 9; d++) {
229+
cnt[d] += higher * pos; // full cycles
230+
231+
if (d < cur) cnt[d] += pos;
232+
else if (d == cur) cnt[d] += lower + 1;
233+
}
234+
235+
// fix leading zero problem
236+
cnt[0] -= pos;
237+
}
238+
return cnt;
239+
}
240+
241+
vector<int> prime_factors(int n) {
242+
vector<int> factors;
243+
for (int i = 2; i * i <= n; i++) {
244+
while (n % i == 0) {
245+
factors.pb(i);
246+
n /= i;
247+
}
248+
}
249+
if (n > 1) factors.pb(n);
250+
return factors;
251+
}
252+
vector<int> divisors(int n) {
253+
vector<int> divs;
254+
for (int i = 1; i * i <= n; i++) {
255+
if (n % i == 0) {
256+
divs.pb(i);
257+
if (i != n / i)
258+
divs.pb(n / i);
259+
}
260+
}
261+
sort(all(divs));
262+
return divs;
263+
}
264+
// Graph Utilities
265+
// --------------------------
266+
vector<vector<int>> adj;
267+
vector<int> vis;
268+
269+
void dfs(int u) {
270+
vis[u] = 1;
271+
for (auto v : adj[u]) {
272+
if (!vis[v]) dfs(v);
273+
}
274+
}
275+
276+
vector<int> bfs(int start, int n) {
277+
vector<int> dist(n+1, -1);
278+
queue<int> q;
279+
q.push(start);
280+
dist[start] = 0;
281+
while (!q.empty()) {
282+
int u = q.front(); q.pop();
283+
for (auto v : adj[u]) {
284+
if (dist[v] == -1) {
285+
dist[v] = dist[u] + 1;
286+
q.push(v);
287+
}
288+
}
289+
}
290+
return dist;
291+
}
292+
293+
294+
long long nextNonDividingPrime(long long n) {
295+
// Small primes list (enough for all practical n ? 1e18)
296+
static const int primes[] = {
297+
2, 3, 5, 7, 11, 13, 17, 19, 23, 29,
298+
31, 37, 41, 43, 47, 53, 59, 61, 67,
299+
71, 73, 79, 83, 89, 97
300+
};
301+
302+
for (int p : primes) {
303+
if (n % p != 0)
304+
return p; // found the smallest prime not dividing n
305+
}
306+
307+
// In the (practically impossible) case n is divisible by all above,
308+
// find the next prime manually
309+
auto isPrime = [](long long x) {
310+
if (x < 2) return false;
311+
for (long long i = 2; i * i <= x; i++)
312+
if (x % i == 0) return false;
313+
return true;
314+
};
315+
316+
long long candidate = 101; // next after our list
317+
while (true) {
318+
if (isPrime(candidate) && n % candidate != 0)
319+
return candidate;
320+
candidate++;
321+
}
322+
}
323+
324+
325+
//*********************************************************************************//
326+
327+
void solve() {
328+
int a,b,c;
329+
cin>>a>>b>>c;
330+
int n = (2*(abs(a-b)));
331+
if(n % 2){
332+
cout<<"-1\n";
333+
return;
334+
}
335+
int ans1 = c + n/2;
336+
int ans2 = c - n/2;
337+
if( a <= n && b <= n && c <= n){
338+
if(ans1 <= n && ans1 > 0){
339+
cout<<ans1<<"\n";
340+
return;
341+
}
342+
else if(ans2 <= n && ans2 > 0){
343+
cout<<ans2<<"\n";
344+
return;
345+
}
346+
}
347+
cout<<"-1\n";
348+
return;
349+
}
350+
351+
352+
signed main() {
353+
ios::sync_with_stdio(false);
354+
cin.tie(nullptr); cout.tie(nullptr);
355+
356+
// precompute();
357+
// sieve();
358+
DEBUG = false;
359+
ll t;
360+
cin >> t;
361+
while (t--) {
362+
solve();
363+
}
364+
return 0;
365+
}

0 commit comments

Comments
 (0)