Skip to content

Commit cfb71f1

Browse files
authored
Merge pull request #525 from Krishna200608/feature/issue-523-removal-sequence
Solved Issue #523: Removal of a Sequence (Binary Search)
2 parents a3816d7 + 2dd41db commit cfb71f1

1 file changed

Lines changed: 87 additions & 0 deletions

File tree

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/*
2+
Submission Link:
3+
https://codeforces.com/contest/2169/submission/356454662
4+
*/
5+
6+
/*
7+
Problem: Removal of a Sequence (Easy Version)
8+
Link: https://codeforces.com/contest/2169/problem/D1
9+
Author: Krishna200608
10+
11+
Short Problem Statement:
12+
Find min integer n such that after x operations (v -= v/y), the result is >= k.
13+
14+
Approach:
15+
Binary Search on Answer. The function is monotonic.
16+
Simulate the process for a given start value to check validity.
17+
Search range [1, 10^12].
18+
19+
Time Complexity: O(T *x *log(N))
20+
Space : O(1)
21+
*/
22+
23+
#include<bits/stdc++.h>
24+
using namespace std;
25+
#define MOD 1000000007
26+
const int M = 1e9+7;
27+
const double PI = acos(-1.0);
28+
#define INF 1e18
29+
#define pb push_back
30+
#define ppb pop_back
31+
#define ll long long
32+
#define no cout << "NO" << endl;
33+
#define yes cout << "YES" << endl;
34+
#define ff first
35+
#define ss second
36+
#define inn(x) int x; std::cin >> x;
37+
#define ill(x) ll x; std::cin >> x;
38+
#define all(x) x.begin(),x.end()
39+
#define in(a) for(int i = 0; i<a.size(); i++) cin>>a[i];
40+
#define out(a) for(int i = 0; i<a.size(); i++) cout<<a[i]<< " " ;
41+
typedef vector<int> vi;
42+
typedef vector<ll> vll;
43+
44+
ll survivors_after_x(ll v, int x, ll y){
45+
ll s = v;
46+
for(int i=0;i<x && s>0;i++){
47+
s -= s / y;
48+
}
49+
return s;
50+
}
51+
52+
void solve(){
53+
int T;
54+
if(!(cin>>T)) return;
55+
const ll NMAX = 1000000000000LL;
56+
while(T--){
57+
int x;
58+
ll y, k;
59+
cin >> x >> y >> k;
60+
61+
// Optimization: check upper bound first
62+
if(survivors_after_x(NMAX, x, y) < k){
63+
cout << -1 << '\n';
64+
continue;
65+
}
66+
67+
ll lo = 1, hi = NMAX;
68+
while(lo < hi){
69+
ll mid = lo + (hi - lo) / 2;
70+
if(survivors_after_x(mid, x, y) >= k) hi = mid;
71+
else lo = mid + 1;
72+
}
73+
cout << lo << '\n';
74+
}
75+
}
76+
77+
signed main(){
78+
ios_base::sync_with_stdio(false);cin.tie(nullptr);cout.tie(nullptr);
79+
auto begin = std::chrono::high_resolution_clock::now();
80+
81+
solve(); // solve handles the test cases loop internally as per your code
82+
83+
auto end = std::chrono::high_resolution_clock::now();
84+
auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
85+
cerr << "Time measured: " << elapsed.count() * 1e-6 << "ms";
86+
return 0;
87+
}

0 commit comments

Comments
 (0)