We'd like to thank you all for participating in the contest, and hope you enjoyed it. Hope to see you again soon!
Author: JainWinn
It is given that $$$a$$$ and $$$b$$$ are number of commuters at concourse and platform respectively and $$$c$$$ is maximum capacity of lift .
- Number of non-empty translations from concourse to platform is given by $$${up=(a+c-1)/c}$$$ .
- Number of non-empty translations from platform to concourse is given by $$${down=(b+c-1)/c}$$$ .
- If $$${up \gt down}$$$ ,then answer is $$${2*up-1}$$$ , else answer is $$${2*down}$$$.
#include<bits/stdc++.h>
using namespace std;
void solve(){
int a,b,c;
cin>>a>>b>>c;
int up=(a+(c-1))/c;
int down=(b+(c-1))/c;
if(up>down){
cout<<2*(up)-1<<endl;
}
else{
cout<<2*(down)<<endl;
}
}
int32_t main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t;
cin>>t;
while(t--){
solve();
}
return 0;
}
Author: prathamtyagii
We have to find the minimum number of matchdays for which we can guarantee a winner in an ideal scenario if $$$n$$$ teams are there in the league.Let us take this matchday to be $$$i$$$. To consider the ideal scenario, we need one team that will win all of its matches up to $$$i$$$ matchdays and all the remaining teams draw their matches while facing each other. Now we need to create a gap between the $$$1^{st}$$$ and $$$2^{nd}$$$ team which cannot be surpassed even if the $$$1^{st}$$$ team loses all of its remaining matches and the $$$2^{nd}$$$ team wins all of its remaining matches after $$$i$$$ matchdays. To do this we can directly formulate a condition, $$$3*i \gt i+3*(n-1-i)$$$ where:
$$$3*i$$$ is the final score of the $$$1^{st}$$$ team after winning first $$$i$$$ matchdays and losing the remaining ones
$$$i$$$ is the score of the $$$2^{nd}$$$ team after drawing the first $$$i$$$ matchdays and $$$3*(n-1-i)$$$ is the score after winning the remaining matchdays.
Simplifying the expression, the final condition is $$$i \gt (3*n-3)/5$$$.
import java.util.*;
import java.lang.*;
import java.io.*;
public class cfct
{
public static void main (String[] args) throws java.lang.Exception
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String line = br.readLine();
int t = Integer.parseInt(line.split(" ")[0]);
while(t>0){
t--;
String line1 = br.readLine();
int n = Integer.parseInt(line1.split(" ")[0]);
int ans=(3*n-3)/5;
System.out.println(ans+1);
}
}
}
Author: vrintle
This problem is very similar to the classical coin change problem (similar CSES Problem). Every "coin" in this problem is of the form x, xx, xxx, xxxx, and so on. We state that for this problem, it is always optimal to choose the maximum value coin which is just less than the required sum.
... Let's say the greedy approach uses the coin xxxx, and let's say we have a better answer than greedy, which doesn't use xxxx. Then it must form xxxx using smaller values. Let's choose the next smaller value: xxx. We need $$$10$$$ such coins (plus $$$1$$$ of the value x) to form xxxx. This means $$$\text{optimal answer} = \text{greedy answer} - 1 + 11$$$.
So, this contradicts the fact that we have any better answer than the greedy one.
Credits: kachhuaa for a very simple proof!
So it's always better to choose greedily.
#include <bits/stdc++.h>
using namespace std;
#define int int64_t
#define endl '\n'
#define sz(v) ((int) v.size())
#define all(v) v.begin(), v.end()
#define vi vector<int>
#define pii pair<int, int>
#define inf 2e18
int max(int x, int y) { return x > y ? x : y; }
int min(int x, int y) { return x < y ? x : y; }
void solve() {
int n, x;
cin >> n >> x;
int ans = 0, mx = 0;
while(mx < n) {
mx = mx * 10 + x;
}
while(mx > 0) {
ans += n / mx;
n %= mx;
mx /= 10;
}
cout << (n ? -1 : ans);
}
int32_t main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
int t = 1;
cin >> t;
while(t--) {
solve();
cout << endl;
}
return 0;
}
Author: C0de_Beast
We have to convert an hidden number $$$x$$$ into a given number $$$k$$$ with in $$$31$$$ queries, where $$$x$$$ and $$$k$$$ both lies in range $$$[1,10^9]$$$, which can be represented in $$$30$$$ bits.
Our last move will obviously be to convert the hidden number to $$$k$$$, thus we will have to figure out the hidden number in $$$30$$$ queries. We can do by doing the following steps:-
- First we query "? $$$0$$$" to find the number of set bits in the hidden number.
- For every bit from $$$1st$$$ bit to $$$29th$$$ bit from right we can query a number with $$$ith$$$ bit set to find whether it was set in the hidden number or not, while accounting for the fact that this operation would flip the $$$ith$$$ bit.
- Now we want to find the state $$$30th$$$ bit which can we can infer from the initial count of set bits and the count we found during $$$29$$$ queries in $$$2nd$$$ step.
After these $$$30$$$ queries we will be able to construct the current hidden number $$$x$$$ and just query "? $$$x⊕k$$$" to finally convert the hidden number to $$$k$$$.
#include<stdio.h>
int main()
{
int t;
scanf("%d", &t);
while(t--) {
int k;
scanf("%d", &k);
int set_bits;
printf("? %d\n", 0);
fflush(stdout);
scanf("%d", &set_bits);
int set_bits_t = set_bits;
int set_bits_c{};
int p{};
for(int x = 0; x < 29; x++) {
printf("? %d\n", 1<<x);
fflush(stdout);
int temp;
scanf("%d", &temp);
if(temp == -1) {
return 0;
}
if(temp > set_bits) {
p = p|(1<<x);
}else {
set_bits_c++;
}
set_bits = temp;
}
if(set_bits_c != set_bits_t) {
p = p|(1<<29);
}
p = p^k;
printf("? %d\n", p);
fflush(stdout);
int temp;
scanf("%d", &temp);
if(temp == -1) {
return 0;
}
printf("!\n");
fflush(stdout);
}
return 0;
}
Author: C0de_Beast
Breaking down the equation $$$a≡b$$$ $$$($$$mod $$$2^k)$$$, this means the number of same bits from right in binary representation of $$$a$$$ and $$$b$$$.
Now if we use a trie to store each number bitwise from right, we could do insertion operation in $$$O(log n)$$$ time complexity, while also being able to calculate the change in cohesiveness simultaneously if we additionally store the count at each node.
#include<stdio.h>
using namespace std;
struct Node {
int count;
Node* zero;
Node* one;
};
void del_trie(Node* head) {
if(head->one) {
del_trie(head->one);
}
if(head->zero) {
del_trie(head->zero);
}
delete head;
}
void insert(Node* head, int n, long long& sum) {
sum = (sum + head->count) % (1000000007);
head->count++;
if(n == 0) {
return;
}
if((n&1) == 1) {
if(head->one == nullptr) {
Node* temp = new Node();
head->one = temp;
}
insert(head->one, n>>1, sum);
}else {
if(head->zero == nullptr) {
Node* temp = new Node();
head->zero = temp;
}
insert(head->zero, n>>1, sum);
}
}
int main()
{
int t;
scanf("%d", &t);
while(t--) {
int n;
scanf("%d", &n);
long long sum{};
Node* head = new Node();
for(int x = 0; x < n; x++) {
int k;
scanf("%d", &k);
k = k|(1<<30);
insert(head, k, sum);
head->count = 0;
}
printf("%lld\n", sum);
del_trie(head);
}
return 0;
}




