1594A - Consecutive Sum Riddle

**Hint**

See how $$$n = 2$$$ case works.

**Solution**

Tutorial is loading...

**Code (C++)**

```
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
int main(void){
ios_base::sync_with_stdio(0);
cin.tie(0);
int t;
cin>>t;
while(t--){
ll n;
cin>>n;
cout<<-n+1<<" "<<n<<endl;
}
}
```

**Hint**

Think of the numbers in base $$$n$$$.

**Solution**

Tutorial is loading...

**Code (C++)**

```
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1e9+7;
const ll MOD = 998244353;
typedef pair<ll,ll> ii;
#define iii pair<ll,ii>
#define f(i,a,b) for(ll i = a;i < b;i++)
#define pb push_back
#define vll vector<ll>
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
int main(void){
ios_base::sync_with_stdio(0);
cin.tie(0);
int t;
cin>>t;
while(t--){
ll n,k;
cin>>n>>k;
ll p = 1;
ll ans = 0;
f(j,0,31){
if(k & (1<<j)){
ans = (ans + p) % INF;
}
p *= n;
p %= INF;
}
cout<<ans<<"\n";
}
}
```

**Hint**

You need atmost 2 operations.

**Solution**

Tutorial is loading...

**Code (C++)**

```
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1e9+7;
const ll MOD = 998244353;
typedef pair<ll,ll> ii;
#define iii pair<ll,ii>
#define f(i,a,b) for(int i = a;i < b;i++)
#define pb push_back
#define vll vector<ll>
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
int main(void){
ios_base::sync_with_stdio(0);
cin.tie(0);
int t;
cin>>t;
while(t--){
ll n;
cin>>n;
char c;
cin>>c;
string s;
cin>>s;
vector<int>ans;
bool ok = true;
for(auto x:s){
if(x != c){
ok = false;
}
}
if(!ok){
f(i,1,n+1){
ok = true;
f(j,i,n+1){
ok &= (s[j-1] == c);
j += i-1;
}
if(ok){
ans.pb(i);
break;
}
}
if(!ok){
ans.pb(n);
ans.pb(n-1);
}
}
cout<<ans.size()<<"\n";
for(auto x:ans){
cout<<x<<" ";
}
cout<<"\n";
}
}
```

1594D - The Number of Imposters

**Hint**

Construct a graph from the comments.

**Solution**

Tutorial is loading...

**Code 1(C++)**

```
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1e9+7;
const ll MOD = 998244353;
typedef pair<ll,ll> ii;
#define iii pair<ll,ii>
#define f(i,a,b) for(int i = a;i < b;i++)
#define pb push_back
#define vll vector<ll>
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
vector<vector<ii> >adj;
int c[2];
int colour[700005];
bool ok;
int n;
void dfs(int idx){
c[colour[idx]]+= (idx <= n);
for(auto x:adj[idx]){
if(colour[x.F] == -1){
colour[x.F] = colour[idx] ^ x.S;
dfs(x.F);
}
else if(colour[x.F] != (colour[idx] ^ x.S)){
///impossible
ok = false;
}
}
}
int main(void){
ios_base::sync_with_stdio(0);
cin.tie(0);
int t;
cin>>t;
while(t--){
int m;
cin>>n>>m;
///n original + m atmost fake nodes
adj.assign(n+m+5,vector<ii>());
f(i,0,n+m+5){
colour[i] = -1;
}
int fake = n+1;
f(i,0,m){
int a,b;
string c;
cin>>a>>b>>c;
if(c == "crewmate"){
///same team
adj[a].pb(ii(fake,1));
adj[fake].pb(ii(a,1));
adj[fake].pb(ii(b,1));
adj[b].pb(ii(fake,1));
fake++;
}
else{
///different team
adj[a].pb(ii(b,1));
adj[b].pb(ii(a,1));
}
}
int ans = 0;
ok = true;
f(i,1,n+1){
if(colour[i] == -1){
colour[i] = 0;
c[0] = c[1] = 0;
dfs(i);
ans += max(c[0],c[1]);
}
}
if(!ok){
ans = -1;
}
cout<<ans<<"\n";
}
}
```

**Code 2(C++)**

```
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1e9+7;
const ll MOD = 998244353;
typedef pair<ll,ll> ii;
#define iii pair<ll,ii>
#define f(i,a,b) for(int i = a;i < b;i++)
#define pb push_back
#define vll vector<ll>
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
vector<vector<ii> >adj;
int c[2];
int colour[200005];
bool ok;
void dfs(int idx){
c[colour[idx]]++;
for(auto x:adj[idx]){
if(colour[x.F] == -1){
colour[x.F] = colour[idx] ^ x.S;
dfs(x.F);
}
else if(colour[x.F] != (colour[idx] ^ x.S)){
///impossible
ok = false;
}
}
}
int main(void){
ios_base::sync_with_stdio(0);
cin.tie(0);
int t;
cin>>t;
while(t--){
int n,m;
cin>>n>>m;
adj.assign(n+5,vector<ii>());
f(i,0,n+5){
colour[i] = -1;
}
f(i,0,m){
int a,b;
string c;
cin>>a>>b>>c;
if(c == "crewmate"){
///same team
adj[a].pb(ii(b,0));
adj[b].pb(ii(a,0));
}
else{
///different team
adj[a].pb(ii(b,1));
adj[b].pb(ii(a,1));
}
}
int ans = 0;
ok = true;
f(i,1,n+1){
if(colour[i] == -1){
colour[i] = 0;
c[0] = c[1] = 0;
dfs(i);
ans += max(c[0],c[1]);
}
}
if(!ok){
ans = -1;
}
cout<<ans<<"\n";
}
}
```

1594E1 - Rubik's Cube Coloring (easy version)

**Hint**

Think of how many colorings there are if you color only the root with one color.

**Solution**

Tutorial is loading...

**Code (C++)**

```
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1e9+7;
const ll MOD = 998244353;
typedef pair<ll,ll> ii;
#define iii pair<ii,ll>
#define f(i,a,b) for(ll i = a;i < b;i++)
#define pb push_back
#define vll vector<ll>
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
ll power(ll a,ll b,ll mod){
if(b == 0){
return 1;
}
ll ans = power(a,b/2,mod);
ans *= ans;
ans %= mod;
if(b % 2){
ans *= a;
}
return ans % mod;
}
int main(void){
ios_base::sync_with_stdio(0);
cin.tie(0);
ll k;
cin>>k;
ll othernodes = (1LL<<k) - 2;
ll ans = power(4,othernodes,INF);
ans *= 6;
ans %= INF;
cout<<ans<<"\n";
}
```

1594E2 - Rubik's Cube Coloring (hard version)

**Hint**

If you have a tree with a root with a fixed color and the others are all not predefined the number of colorings is $$$4^{nodes-1}$$$.

**Solution**

Tutorial is loading...

**Code (C++)**

```
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll INF = 1e9+7;
const ll MOD = 998244353;
typedef pair<ll,ll> ii;
#define iii pair<ii,ll>
#define f(i,a,b) for(ll i = a;i < b;i++)
#define pb push_back
#define vll vector<ll>
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
ll dp[(60 * 10000) + 5][6];
vll color[6];
vector<vector<int> >adj;
map<ll,int>label;
ll c[(60 * 10000) + 5];
ll solve(int i,int j){
if(c[i] != -1 && c[i] != j){
return 0;
}
if(dp[i][j] != -1){
return dp[i][j];
}
ll ans = 0;
ll sum[2] = {0};
for(auto x:color[j]){
f(j,0,adj[i].size()){
sum[j] += solve(adj[i][j],x);
sum[j] %= INF;
}
}
if(adj[i].empty()){
sum[0] = sum[1] = 1;
}
if((ll)adj[i].size() == 1){
sum[1] = 1;
}
ans = (sum[0] * sum[1]) % INF;
return dp[i][j] = ans;
}
ll power(ll a,ll b,ll mod){
if(b == 0){
return 1;
}
ll ans = power(a,b/2,mod);
ans *= ans;
ans %= mod;
if(b % 2){
ans *= a;
}
return ans % mod;
}
int main(void){
ios_base::sync_with_stdio(0);
cin.tie(0);
color[0] = {1,2,4,5};
color[1] = {0,2,3,5};
color[2] = {0,1,3,4};
color[3] = {1,2,4,5};
color[4] = {0,2,3,5};
color[5] = {0,1,3,4};
map<string,ll>mp;
mp["white"] = 0;
mp["blue"] = 1;
mp["red"] = 2;
mp["yellow"] = 3;
mp["green"] = 4;
mp["orange"] = 5;
memset(dp,-1,sizeof dp);
memset(c,-1,sizeof c);
ll k;
cin>>k;
ll n;
cin>>n;
ll posoi = (1LL<<k) - 1;
int lab = 0;
map<ll,int>ar;
f(i,0,n){
ll x;
cin>>x;
string s;
cin>>s;
ar[x] = mp[s];
ll cur = x;
while(cur >= 1 && !label.count(cur)){
label[cur] = lab;
lab++;
posoi--;
cur /= 2;
}
}
adj.assign(lab + 5,vector<int>());
for(auto x:label){
if(ar.count(x.F)){
c[x.S] = ar[x.F];
}
if(label.count(x.F * 2)){
adj[x.S].pb(label[x.F * 2]);
}
if(label.count(x.F * 2 + 1)){
adj[x.S].pb(label[x.F * 2 + 1]);
}
}
ll ans = power(4,posoi,INF);
ll sum = 0;
f(j,0,6){
sum += solve(label[1],j);
sum %= INF;
}
ans *= sum;
ans %= INF;
cout<<ans<<"\n";
}
```

**Solution**

Tutorial is loading...

**Code (C++)**

```
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll INF = 1e9+7;
ll MOD = 998244353;
typedef pair<ll,ll> ii;
#define iii pair<ii,ll>
#define f(i,a,b) for(int i = a;i < b;i++)
#define pb push_back
#define vll vector<ll>
#define F first
#define S second
#define all(x) (x).begin(), (x).end()
int main(void){
ios_base::sync_with_stdio(0);
cin.tie(0);
int t;
cin>>t;
while(t--){
ll n,k,s;
cin>>s>>n>>k;
if(s == k){
cout<<"YES\n";
}
else if(k > s){
cout<<"NO\n";
}
else{
ll posa = s+k;
ll l = s-k+1,r = s;
ll siz = r - l + 1;
ll a = 0,b = 0;
ll num = (s / k) * k;
b = r - num + 1;
a = siz - b;
if((s / k) % 2 == 1){
posa -= b;
}
else{
posa -= a;
}
if((2 * n + 1) > posa){
cout<<"YES\n";
}
else{
cout<<"NO\n";
}
}
}
}
```

Wow the editorial even before the system tests are finished!

Thx for the fast editorial! :)

Thanks for fast editorial

Thank you for the contest

Great problem set! Thank you

What is the intuition behind B?

for all $$$n \geq 2$$$ and $$$k \geq 1$$$, $$$n^k > \Sigma_{i=0}^{k-1}n^i$$$

Special number is n base number with only ones. So now we have to figure out how to calculate the k-th one And because it is just a matter of play between ones and zeroes, you are like in a binary number, so the k-th number will be the n-th base number with ones on the same spot as k

Got it thanks

can you see my solution for problem B...it is giving tle https://codeforces.com/contest/1594/my

It was the combination that we can create using powers of a number in increasing order

The numbers that we need to form should have distinct powers and their coefficient should be either 1 or 0. For example if we take n = 4 and k = 17 then 17 = (10) base 4. So this would give an intuition that we need to do something with binary strings or bianry representation of a number K as we need coefficient as 0 or 1 of the powers. Now Lets see what we have to do K = 1 in binary = 1 K = 2 in binary = 10 K = 3 in binary = 11 K = 4 in binary = 100 and so on Now lets see how our answer comes out, As we need to find the Kth special number for base n so suppose that above binary strings were formed with base n and then convert them into their decimal equivalent respectively. For example from test cases where n = 3 and k = 4 so now binary representaion of k is 100. Just take this in base n = 3 so it comes out to be 1*3^2 + 0*3^1 + 0*3^0 = 9 and our answer. Similarly take n = 2 and k = 12. So 12 = 1100 in binary. As our base n is also 2 only answer again converts back to 12. You can further take more examples.

crystal clear! Thanks

Thanks man, that's really helpful.

Hey if you won't mind, could you please provide some editorials or playlists for bit masking.

You can refer to this blog for learning new topics : https://codeforces.com/blog/YouKn0wWho

thanks

Wow nice explanation, I used math's for it and for finding power i used to do x=log2(k) and multiply n, x times and then k-=pow(a,2) until my k becomes 0.

Thankyou so much!

Oh , that was realy fast!

Testers for this round be like

This is really rude. Testers did their best and gave invaluable feedback. No-one can know every problem that has been created. We are sorry for the situation but testers are not to blame.

dumbasses always exist and they have a problem with the universe itself and nothing can be done about it.

i think this was clearly meant as a joke..?? Keep chill guys

that load of shit barely qualifies as a joke, but okay.

Nice contest. Thank you so much!!!!

That was really nice contest! Great statements, beautiful solution and very fast edutorial! (even faster than sys tests). I really enjoyed these 2h 15m in this contest, thank you!

E2 is pretty interesting I think.

Thanks for the fast editorial!

Nice round.

excellent contest with an interesting problemset , i got very close to solving 3 problems in a div2 for the first time ever , hopefully gonna do it next time :D

same for me too! i was close to solving C and A,B was very interesting :D

D was completely copied from INOI 2021... how can you blatantly copy a problem

https://www.codechef.com/INOIPRAC/problems/AMONGUS2

lmao

how did they do this? 1) You highly underestimate the popularity of among us 2) As pointed by someone in the contest announcement comments that it is a little bit trivial for experienced coders to come up with that idea for the problem... check that comment I am lazy to link it.

yeah but they can atleast put some effort..Like change some thing introduce some different parameter exactly copying a statement is too much

We did not copy anything and would never ever do that as it ruins the whole point of problemsetting for both authors and contestants. This was an unfortunate coincidence and we are very sorry for it, we will be taking every measure to avoid it in the future.

I really appreciate you guys effort...as i think it takes a lot of work to prepare a proper contest and i really respect you all for that.. and as for this it would have happened because of some mistake its just that a single problem, like this ruins your guys effort for all the other problems, and for the people participating it gives unnecessary advantage to some...

SUS

why y'all downnvoting its not like i am disrespecting ..I am just stating an obvious fact..Really cant get this community sometimes

Can someone please tell me why my solution for E gives wrong answer submission.

Your val variable which stores the total number of nodes has been stored modulo 1e9 + 7. And then you are using val as a power. If you are calculating a^b%MOD then b has to be stored not modulo MOD but rather a different number. In this case I think its 1e9+6 so you had to store val modulo 1e9 + 6.

can you see my solution for problem B...it is giving tle i don't know why https://codeforces.com/contest/1594/my

Your calculation of $$$val$$$ is wrong.

Fermat's little theorem says $$$a^{p-1} \equiv 1 \bmod p$$$, so you have to calculate the exponent modulo $$$p-1$$$ (in this case, $$$10^9 + 6$$$).

Can you please give an example where this calculation of val fails. I am unable to understand why this gives wrong answer?

$$$3^{10}=59049$$$ mod $$$5$$$ is $$$4$$$. But $$$3^0$$$ mod $$$5$$$ is $$$1$$$.

I hope now you can come up with a similar example for mod $$$10^9+7$$$.

Thanks.

he is saying that ((x^y)%mod)!=(((x^(y%mod))%mod))

Better approach for F, for case

`s > k`

:For each block of size k,

Put, (k-1) continuous ones, and then put (k+1) to avoid subarray with sum k.

`(1, 1.. k-1 times) (k+1)`

This is the basic case, when the farm isn't lucky, all other cases of the farm being unlucky will require higher value of s.

Sum left, after putting all ones =

`s - n`

Extra Sum Required =

`(n / k) * k`

(for basic case, described above)`if sum_left < extra_sum, then "YES"`

`else, "NO"`

I had the same idea,but I failed to perfectly prove "when the farm isn't lucky, all other cases of the farm being unlucky will require higher value of s."

Could you plz tell me how to prove it?thx

Firstly you can porve that if only use k blocks,the minimum n is $$$2\times k$$$.

Then you will find that the way to construct exactly fits the conclusion above，

`if only use k blocks,the minimum n is $$$2\times k$$$ first`

.The above conclusion can be proved by using Pigeonhole principle.

If there is a legal way that has n less than $$$2\times k$$$,it should satisfy that for each $$$pre_i\bmod k$$$ must be distinct,because if $$$pre_i\equiv pre_j\pmod k$$$,the subarray $$$[i+1,j]$$$ will be equal to k,($$$pre_i<2k$$$).

It is impossible because we have $$$n+1$$$ $$$b_i$$$,and $$$n$$$ $$$b_i\bmod i$$$.

Then we has already proved the conclusion.

Sorry,I'm not certain about whether my proof is correct.

Hope it can help you :)

Speedforces, and fast editorial.

I missed " if(s==k){ cout << "YES\n"; continue; } " and got F wrong. :(

Can you explain me problem please. I think , I am confuse about what is problem asking.

For problem $$$C$$$ it's just enough to check if any of the chars beetwen $$$[n/2+1, n]$$$ (1-indexing) is equal to $$$c$$$ , if it's then the answer is 1. $$$Proof$$$ : Any number between $$$[1, x - 1]$$$ and $$$[x+1, x*2-1]$$$ isn't divisible by x, so every index that isn't $$$x$$$ ,will be changeable. Time complexity — $$$O(N)$$$. My submission — 131189321. Btw, nice contest, thank u!

Thank you for this, was trying to understand this in tourist's code lol

You need to say why it's enough to check $$$[\frac{n}{2}+1, n]$$$, because if $$$x$$$ in $$$[1, \frac{n}{2}]$$$, $$$2x$$$ is in the second half, so in order to use $$$x$$$ it must be that the character at $$$2x$$$ is already correct. So we can use the second half without loss of generality.

Your are right, in order to use x in the $$$[1, n / 2]$$$ we need to check $$$2x,3x..$$$ as well, but instead of checking $$$x,2x,3x...$$$ we just need a maximum $$$yx$$$ such that $$$yx <= n$$$, and if we mark that $$$yx$$$ then every number $$$x,2x,3x,4x..<yx$$$ will be changeable because $$$u$$$ $$$mod$$$ $$$p$$$ $$$!=$$$ $$$0$$$ when $$$u < p$$$. That's why my solution uses number $$$[n/2+1, n]$$$ because there is $$$NOT$$$ any number $$$x$$$ in that range such that $$$2x <= n$$$.

Awesome perspective! Thanks.

thanks. I got stuck for this for a long time

Nice problems. Thank you!

Simpler code for C with O(n) complexity

Thank you sir, very cool.

Thanks for an amazing contest.I really like problem D and editorial is very good)

Video Editorial for Problem D explaining the 2 coloring strategy for bipartite graphs

I solved the original problem in contest so I explain my exact thought process which might help some of you.

Note that I didn't do this contest because I saw D before starting and didn't want to take an unfair advantage.

I know, You are a gem! Keep posting such valuable videos. You will surely have a great future ahead. I follow your videos and they are very helpful. Don't mind about the downvoters, such people always exists.

ak2006 Nice editorial man, keep up the good work. I've seen your content as well it's really educational.

Can someone please explain E1?

For the root node you have 6 colours to choose from, and for the rest you have 4. Then, the answer must be 6 * (4 ^ ((2 ^ k) — 2))

got it. Thank you

I did with dp i can have ans if n goes till 1e8 its sometimes very interesting to know that the solution was very diffrent[submission:131209883]

You can have DP solution also which is easy to understand . Each node have to be carefully coloured respecting the given condition in problem .

My implementation

Thanks a lot. I was looking for this kind of solution which actually does what the questions says to compute the answer.Thanks again!

can someone explain me problem B?

Basically, just think like this: For a given n, we need to find the kth integer in base n. Why so? If we need to get to that kth integer, we need to go through all the possible sum of powers of n, which happens to be the process of finding kth integer in base n.

To understand it clearly, take n = 2.

And for the solution, look for the set bits of k. And add all the powers of n to the set bit positions.

Please ask if you need more elaboration on this.

could you please elaborate it more?

to get the kth number in base of n , we need to add to sum of powers in the indexes of set bits , for example in case of n=2 , we are getting all natural numbers , but these numbers have a base of 10 , i am confused with this base thing

For n=2, we are looking at the kth number in base 2, which goes from 1, 10, 11, 100, 101 and so on.

For n=3, we are looking at it in base 3, so 1, 2, 10, 11, 12 and so on. But:

the question said that special numbers are sum of different powers, so the base can only contain 0s and 1s, so we will ignore 2, 12, 20 and so on.

You are a gem. Humanity still remains in this world.

just find binary representation of k and then add n^i where i is all set bits of k.

How can u think for B . is it number theory or something else ? And how can i be able to solve these type of questions? Pls somebody help me

First, let's see what the array is if n=2.

We already know that every integer can be represented via a sum of powers of two (and no two powers are equal). This happens because.. well.. every number has a distinct form in base-2, and the base-2 writing actually indicates you on what powers of two to add.

Now, let's see for n=5 for example, what the array will be

1 = 1(5) 5 = 10(5) 6 = 11(5) 25 = 100(5) 26 = 101(5)

And so on.

Observe that the base-5 writing of these numbers looks very similar to the base-2 writing of 1,2,3,4,5... So, we can state the following:

If we know the base-2 writing of K, then we can just change the base to n (we keep the number as it was). And from here, I think it's pretty straightforward.

can u please explain me , like for natural nos like 1 2 3 ... , they have a base of 10 , so y are we refering them to as having base of 2 here

would you suggest me what should i learn according to me rating ?

It's very basic properties of positional notation (positional numeral system). Read about it.

Nice problems. Thank you!

Yes,B was very nice problem

Hey, can you help me here? I am getting the wrong answer for test case 105, 564.

Apparently, I am doing something wrong with modulo operator>

FastReader s=new FastReader(); int t = s.nextInt(); // Number of test cases

I think you should not use Math.pow(). It uses float or double type inside and causes precision loss. Not good at Java, just as my personal opinion.

Noted! I'll attempt it again.

Excellent contest, System testing was also awesome :DMaybe 1594C has an $$$O(|s|)$$$ method?

Here is my solution.

First, if we have a minium i such that $$$s_i,s_{i+1},...,s_n \neq c$$$ and the answer is $$$1$$$ operation with a certain $$$x$$$, it is obvious that $$$s_x = c$$$ and $$$\forall k \in N_+ \wedge kx \leq n, kx < i$$$.

If $$$i = 1$$$, we can't find that $$$x$$$.

If $$$i \neq 1$$$, let $$$a = i - 1$$$.

We can prove that if $$$a$$$ doesn't match the conditions above, we can't find that $$$x$$$. If $$$a$$$ doesn't match the conditions above and there exists $$$x$$$ such that $$$x < a$$$, $$$i \leq 2a \leq n$$$ and $$$\exists k \in N_+, a < i \leq kx \leq a + x < 2a \leq n$$$, so $$$x$$$ doesn't match the conditions above.

And if and only if $$$2a > n$$$, $$$a$$$ can be the $$$x$$$, because $$$2a > i$$$, $$$\forall i \in N_+ \wedge i < a, i$$$ is not divisible by $$$a$$$ and only at this time $$$\forall k \in N_+ \wedge k > 2, ka > n$$$.

Then we can use $$$O(|s|)$$$ to find this $$$a$$$ and output it, solving the situation when the answer is $$$1$$$ operation with a certain $$$x$$$.

I didn't attend the contest and tried to solve this problem today. It took me some time to come up with the answer that only 3 solutions exists. 0,1 and 2. 0 when all the characters in the string match with the given letter. 1 when any of the characters from 1 <= i <= n/2 and also when any one of the character from n/2 <= i <= n doesn't match the given letter. Else the answer is 2.(when all the characters from n/2 <= i <= n matches the letter). (n being the length of the string. Excuse me if I use s and n for the length interchangibly.) I'm glad that I came up with a better solution than the tutorial. For a newbie its a great win xd

Great Editorial!!

One can check this youtube channel for video editorials: Link

B harder than E1 !

upvoted

regarding C — "Make Equal" :

Can someone please tell me where my code gives the wrong output? The wrong answer is for test case 74 of preset 2 but I'm not able to see what it is. I've been trying to find my mistake for more than an hour.

idea — I tried to check if there is any index > n/2(n/2 + 1 for even numbers ) that has the correct character. If yes, then the required number of x is one. else 2.

Link to the submission

CodeI think your for loop should go from i = 2 to i <= n. You have written i < n. Plz check.

My apologies, I mistakenly put the wrong code earlier. I have put my original code now. can you please take a look again /\

can you please see my problem submission for problem B it is giving tle...

you can link a submission or put the code in a spoiler

okay, sure

check this test case: 1 13 b aaaaaaabaaaaa

output :7 answer : 8

Contest was actually good, I coded F in O(min(s — n, n) / k), it passed pretests, then I changed in to O(1) but after the round it still works. Strange tests to be honest. Here is my solution https://codeforces.com/contest/1594/submission/131243246

Actually yes, your code gives TLE on this test

Test1

1000000000000000000 500000000000000000 2

I have a solution in problem C that runs in O(|s|) time complexity Firstly, I'll check if all characters in the string are equal to C and if so I will print 0(no need to do any operations).

Next, I check if I can use 1 operation only and construct a good string at the end by finding an index such that (s[i] == c) and (i * 2 > n) and doing 1 operation using this index i. Let's divide proving the second step into two parts: Proving that this operation is enough & Proving that if such an index doesn't exist then simply we can't change the whole string to be equal to c in 1 operation. 0) Choosing index i means that all indices <i are changed to C(since none of them are divisible by i) and since 2 * i>n then all indices between i+1 and n are changed to C too.

1) The chosen index must have s[this_Index]=c because we are always not changing any of its multiples including itself and building on this fact we need to choose an index i such that all characters on indices that are multiples of i(if any exists) are equal to c. The thing is that the (largest multiple of i that is <=n) *2 must always be >n hence that an index such that s[this_index] is equal to c and this_index * 2 >n must exist or there will be no way to do it in 1 operation.

Finally, if I didn't find such i that can fix the string in 1 operation then I will fix the string in 2 operations by making x = n and this will make all of s[i] = c for 1<=i<n and then use x = n-1 to make s[n] = c

My solution: https://codeforces.com/contest/1594/submission/131192419

Easier solution for 1594F - Ideal Farm.

lol I sent the same, now I dont care about the right solution, I am just interested in tests' weakness

Can you explain it a little? I saw the exact same solution somewhere else but couldn't understand. I have read the editorial though and I understand editorialist's solution. edit — : I think I got it now, this person explained it nicely. comment link

I liked problem B. Very nice contest.

I think it's just simply one of the most interesting contest i've written here.

Thanks!

Can you please explain the end of the tutorial of F? I. e.:

"Let m be the maximum number of elements that we can take.

We go through the last k elements ([s−k+1,s]) and we count the number of elements that have the same modulo k.

For each element in this range, if there are odd elements that have the same modulo, we can't take all of them because for every element x that we add in pre that we also add x+k to b. Thus one element would have a x+k out of range.

Therefore we count all the elements that have odd elements with the same modulo k and subtract them from s+k to find m."

I can't understand literally anything from here.

$$$m = $$$ the maximum number of elements that we can take among the $$$[1,s + k]$$$. (We want to make $$$pre$$$ of size $$$n$$$ and $$$b$$$ of size $$$n+1$$$).

We only need to build $$$pre$$$ but there should not same elements in $$$pre$$$ and $$$b$$$.

We go through the last $$$k$$$ elements ($$$[s−k+1,s]$$$) and we count the number of elements that have the same modulo $$$k$$$.

For each element in this range, if there are odd elements that have the same modulo, we can't take all of them (and add them to $$$pre$$$) because for every element $$$x$$$ that we add in $$$pre$$$ that we also add $$$x+k$$$ to $$$b$$$. Thus one element would have a $$$x+k$$$ out of range ($$$x + k > s + k => x > s$$$ which is obviously wrong) .

$$$pre: $$$

($$$[s−k+1,s]$$$)

($$$[s−3k+1,s-2k]$$$)

$$$...$$$

$$$b: $$$

($$$[s+1,s+k]$$$)

($$$[s−2k+1,s-k]$$$)

$$$...$$$

Therefore we count all the elements that have odd elements with the same modulo $$$k$$$ and subtract them from $$$s+k$$$ to find $$$m$$$.

I hope that now it's clearer.

The solution means that we need to allocate the elements of $$$[1, s + k]$$$ into $$$2n + 1$$$ positions (some elements may not be allocated).

Firstly we can observe that for every element $$$x$$$ assigned to

prethere's a corresponding $$$x + k$$$ assigned tob, this means that the numbers with the same modulo $$$k$$$ must be allocated even number times. So we can divide all the numbers with the same modulo $$$k$$$ into the same group.Noticed that if there is an odd number of elements in a group, then the group cannot be fully allocated, so the total number of elements that can be allocated must be subtracted by one. It is easy to figure out how many groups of odd size we have, we can subtract the number of groups with odd size from $$$s + k$$$ finally we will get the number of elements that we can allocate.

But note that $$$k$$$ must initially be assigned to

b(because $$$b[0]=pre[0]+k=0+k=k$$$ ), so the number of groups in which module $$$k$$$ is equal to $$$0$$$ must be subtracted by $$$1$$$.The last paragraph is wrong. When the modulo k is 0 then it's like we take the element 0 in pre and k at pre. Therefore, you again have to subtract 1 when there are odd numbers that have 0 modlulo k.

I just consider the elements in $$$[1,s+k]$$$, so the element $$$0$$$ doesn't count.

in problem D i submit thiscode but it recieved wrong answer it use directed edges but when i convert edges to bidirectionl it get accepts codewhy this happens?

Bruh, how can you still be expert with that amount of solved problems lmffaaaoooo

I have similar query as well. Did you get to know the reason why this happened?

Code with Directed Edges: Directed

Code with Undirected Edges: Undirected

because if 2 says 3 is a liar and 1 says 3 is not a liar then see from first two possibilities are there for 2 and 3 they r 2 is liar while is not or 2 not liar and 3 is. Based on this we can get information about 1 since 1 says 3 is not a liar then if 3 is a liar then 1 is also (see the reverse edge) and if 3 is not a liar then 1 is also not. Thus if we have 2->3 and 1->3 this can be rewritten as 2->3 and 3->1 hence bidirectional

yeah, I got your point. Thanks for the explanation. Can you please further tell, why solving with a directed graph gives a wrong answer. I mean, if you have some counter case.

1 4 3 1 2 imposter 3 2 crewmate 4 2 imposter

your code is giving 3 while the answer should be 2

Thanks a lot.

My feedback -

I just read F and despite multiple readings couldn't figure out what exactly it means. I tried guessing but couldnt come up with something. I wasnt participating but just decided to look at F in middle of the contest, but would have submitted something on F if I had a soln.

- Animal pen doesn't make any sense. Something which animals eat could have been better.

- There is no clarification if there are total n pens or if there are n types of pens with infinite quantities of each type.

- What exactly do you mean by "empty pen"? You haven't defined what exactly is "empty pen"/"non-empty pen" is.

- "all animals in all pens". I tried reading it as "you have to distribute n pens among k animals such that ....." Couldn't come up with something to fill those .... here.

Although authors tried to give a formal definition? But a more formal definition would have been better. Formal definitions should be absolutely formal just like the definition after "The problem is the same as" in the editorial. Someone who just reads it should be able to grasp what is being asked and it shouldn't involve references to the story.

Did you perhaps misunderstand what pens are? [Pen](https://en.wikipedia.org/wiki/Pen_(enclosure))

Yeah. I did. Thanks.

I've been living in the UK for three years and I've never heard the word 'pen' in this context. Anyone who knows that pen is a thing to write with should be careful about using a word with double meaning...

Then what is the word for pen which the UK citizens use? Animal pen is quite common usage.

I think they use the word "pen", but to use it, you must talk about animal pen, which nobody does among city citizens.

You guys never came across this?

Me last night on problem A:

SpoilerI done the same!!! LOL

What does it function means?

You mean "ctz"? "Count trailing zeros (in binary)", i.e., $$$\operatorname{ctz}(n) =$$$ largest integer $$$s$$$ such that $$$2^s$$$ divides $$$n$$$. C++ has it in supported architectures as

`__builtin_ctz`

, or there is an old algorithm using bitshifts and compares which is essentially hand-coded binary search.Or you can always fall back on the naive $$$O(\log n)$$$ approach.

I thought problem E2 was really cool.

can u please explain me the approach i cannot solve it and also do not got it from editorial

The idea is that if we have a node $$$x$$$, whose color we arbitrarily fix, and nothing in the subtree of $$$x$$$ is fixed (in the sense that there are no restrictions on nodes below $$$x$$$), then we have $$$4^{sz - 1}$$$ ways to fill out the subtree. There are a lot of nodes of this type; in fact, 99.999% of the nodes are nodes of this type.

There are also other types of nodes: nodes in which some child or child of a child, etc is fixed. These cases are a little bit harder and to do so, we can just memoize on its two left and right children. Fix the color on the left child, fix the color on the right child, and then take the sum of the products of sub[left] and sub[right].

131252148

Thank u i got it

A nice round, thanks for the fast editoria!!!

Tou xiang fei chang nice,gei wo zheng xiao le.

In problem c,I set p is the c's latest position.if p>n/2,only operation ones,x=p,otherwise,operation twos,x=n-1 and x=n,but I wrong.I can't understand why wrong.131226305

There is no assignment of 0 when defining P in your code. I think it may be an error here.

brother 666

So fast!! Best ever!

Good thing problem B was from aime lol.i got positive delta.best round ever.easy google

Could u provide a link ?

in problem E1, "a white node can not be neighboring with white AND yellow nodes", I belive the preposition used should be "or" instead of "and". I only read the formal statement, got confused, and ended up solving another problem for half an hour before realizing how dumb I am :(

I think problem F's difficulty at most be *1800.

I still don't know solution. I didn't read editorial yet. Only for problems I solved.

I think in problem

CO(nlog(n)) can be reduced to O(n).Notation: n=|s|; c=required char to be setIf all s[i]==c then ans=0

Else if at any of these positions (n/2 + 1,....,n),

s[i]==c, then usingx=iwe can set all the rest positions in just1operationOtherwise,

allof these positions (n/2+1,....,n) will haves[i]!=c. So, they can't be changed in just one operation. So, answer will be 2 with x=n-1 , nGood job for you to write down this approach here. I have also implemented similar to it during the contest.

Link to my submission https://codeforces.com/contest/1594/submission/131210444

why rating point for this contest is too low? 5 problem only for 7pts :'(

I solved 4 and got a negative delta lmao

Thanks a lot!

can some one please tell me how to solve e2 i am unable to get it from editorial(my mistake not of editorial)

In short, whole idea is to forget about whole tree and find out how many ways to color nodes which are on paths from root to already-colored. Then, multiply by 4 to the corresponding power. And to find out first thing, make dp[v][c] which would tell how many correct ways to color vertex

vinto colorcand its subtree in any colors (without taking into account nodes we're not interested).Your text to link here... I tried to solve question C but I got the wrong answer. I read the editorial and my approach was similar to it. Can some pls suggest to me what I missed in my soln.

Try this testcase1

5 b

abcde

Correct sol requires two steps, ur sol prints one

thanks

I have finsolved B. But I don't understand why k-th element doesn't equal k? For example, for module 7 and n = 3 we have sequence [0, 1, 2, 3, 4, 5, 6]. What special in 1e9+7?

Great set of problems with good underlying logics.

Can someone explain to me Question C?

Video Solutions for all problems from A to E2 : solutions

Thanks

Can someone explain how to code problem E1 in java? I have understood the logic but I'm getting TLE error. Please help, thanks in advance.

You should use your own power function. You can see how I coded it in the editorial to implement it yourself.

More contest at 18 05

Great Contest. Nice set of problems!

i was stuck at B so i didn't looked at C oh man C was a easy one

There is

O(s)solution of problemC — Make Them EqualExplanation:

Iterate over the whole string and if the i-th is K then push the index to a vector named

good.As we iterating from 0 to n-1, the

goodis sorted.now if the greatest element aka index of

goodis greater or equal to the half of the size of the string, then we can prove that this is possible in 1 operationif ( good[good.size() — 1] >= s.size()/2 )

=>1 operationthe except cases are same as the Editorial.

Here is my submission:

131204546

in problem F: Why are we going through the last k elements?

I have no idea what editorial is telling about but this should help:

Our goal is clearly to construct (judge if this is possible) prefix-sum array such that it has length n + 1, first element is 0, last element is s, the array is strictly increasing and the array doesn't have x and x + k simultaneously for any x (then and only then we would have subarray with length exactly k).

Instead we will try to construct longest possible such array (and if it's length would be > n + 1, we can easily "shrink" (decrease it's length) it to obtain construction of array of length exactly n + 1).

Now for each rem = 0, 1 .. k-1 let's look how many elements at most of this (prefix) array can be equal to rem MOD k.

Clearly for particular "rem" one of optimal constructions is rem, 2k + rem, 4K + rem and so on. So you only need to count how many numbers in interval [0, s] are equal to rem MODULO k. Suppose their count is x. Then you can take at most (x + 1) / 2 of them to the prefix array. Do this for every rem = 0,1 .. k-1 and you will know what is the greatest possible length of this prefix array. The only corner case is when s == k, because you are always forced to contain 0 and s in the prefix sum array as first and last elements.

Could you please explain why could we easily "shrink" this array?

Ok, I figured it out. The elements need to have the same rem, and their difference is already 2K. It would be just greater if we shrink.

probably >=n+1. Thanks for the explanantion.

Because if we don't put the last k elements in pre, b will not have the elements from s to s+k.

Those two code are same.The only difference between them is variable type, int and long long int. But one got accepted, another got runtime error. For int type variable got AC, long long int got RE. why ?

AC code: 131311594 RE code: 131309828

Finally become expert QWQ. Continue learning!

Video Solutions

A B C

I have a doubt in D: My solution is giving Runtime error while using vector of map for storing graph and works fine when I use vector of vector. Can anyone help? Here is link to my solution.

can anybody see my solution for problem B, it is giving TLE...i don't know why https://codeforces.com/contest/1594/my

update ur solution link correctly

this is link https://codeforces.com/contest/1594/my and https://codeforces.com/contest/1594/submission/131244376

Can anyone plz explain B without binary way?

I think my solution to the problem C is easier :)

Can you explain the intuition behind your solution ?

How to solve D by DSU ?

let the value of k be greater than 64. Can we still use this-> (1LL<<k)??

bad editorial

Can you be more specific about what you didn't understand?

If you could write comments on your code explaining why you wrote this specific line then it might be easier to understand your code.

Thanks for the contest. Nice problems and fast editorial!

Question: is it possible to solve B without using bitmasks?

I don't think so.

Yes, check out my solution: https://codeforces.com/contest/1594/submission/131288334. I did it without bitmasks.

can anybody please explain why is my solution not working for problem d? solution link : https://codeforces.com/contest/1594/submission/131297133

I am also getting same verdict on testcase 2 line 40 :(

Update: Try this case 4 3 1 2 imposter 3 4 crewmate 2 3 crewmate

Your code will give -1. But actual ans is 3.

Fun fact this is the third problem called Make Them Equal

1154B - Make Them Equal

1417D - Make Them Equal

1594C - Make Them Equal

In problem D, What is the intuition behind connecting fake node with A and B nodes if person A said that B is a crewmate.

Also, how to do the problem with dp / dsu as it is there in one of the problem tags

because if person A said that B is a crewmate, then A and B must belong to the same type, so if there is fake node between them, when we use dfs to paint node, they must be painted the same color.

After hours of reading the Problem F editorial, I still can't understand the last part about finding M. If someone else has the same problem as me, this might help.

Suppose we have distributed the array A. Let's build a prefix sum

Pre[i] = Pre[i-1] + a[i]. Pre[i] is distinct and increasing. If there are i, j such that Pre[i] = Pre[j] + k then there must exist a segment on A which it's sum equal to k.Now we want to check if there is a way to build Pre[i] that

does notexist Pre[i] = Pre[j] + k. Pre[i] is distinct and Pre[i] is a value in range [1...S]. If we chosePre[i] = xthen we can not chosex+k, if we don't chosex+k, we can chosex+2*k.For every p = [0...k-1] (remainder when divided by k), we can take from S number

x = t*k+p(t <= S-p/k). The maximum way to choose number x(s) is t=(0, 2, 4, 6,...). Except p=0, the best way to choose is t=(1, 3, 5, 7,..) since Pre[i] > 0. For every p, the maximum way to choose x (or t) is Tp.Now, M = sum of all Tp (p=[0...k]). If M>=n then there is a way that

does notexistPre[i] = Pre[i]+k, the answer is NO, otherwise, the answer is YES.You don't have to count Tp separately, use some modulus and divine operation and you can get M easily.

Thank you for the contest

In problem E2, I use priority_queue to process node from bottom to the top, but it lead to TLE in case9, here is my code, anyone can tell me why it fail?

Problem C can be solved in O(|s|) :

https://codeforces.com/contest/1594/submission/132498264

Can anyone help me figure out why my solution is giving RTE in problem D? I am using DSU and small to large merging. 133162809

https://codeforces.com/contest/1594/submission/133176237 why I am getting Wrong answer. which case i haven't considered.

It looks like you only use operations for $$$x = 2$$$ or $$$x = 3$$$. In this case, for example, you cannot change the $$$6$$$-th position in the string, because $$$6$$$ is divisible by both $$$2$$$ and $$$3$$$.

DIV 2. B The test case explained for n=3 has 1 and 3 in the sequence but how can we write 1 and 3 as the sum of two different powers of n???

Hi, here I have an O(n) solution for 1594C - Make Them Equal

133993659

In problem D's Editorial, in Code 1, why is the adjacency list (graph) a vector<vector<pair<int,int>> >adj? I mean why not just vector<vector>adj, we are anyway not changing the color part of that pair i.e. the second member. Can anyone explain please?

Also this is my submission using vector<vector>adj, but it is failing for some reason. Not sure if this is due to the same reason or what? https://codeforces.com/contest/1594/submission/145685355

Thanks in advance for help!

Can someone tell what's wrong in my solution of problem B, https://codeforces.com/contest/1594/submission/165817175 Its giving TLE