# [Codeforces] the abundance of Codeforces [partial explanation]

Posted by tready29483 on Tue, 08 Feb 2022 07:54:43 +0100

# Abundance of Codeforces

## Codeforces Round #721 (Div. 2)

Codeforces Round #721 (Div. 2)

### A. And Then There Were K

Problem - A - Codeforces

• The meaning of the question is to seek satisfaction[ n & ( n − 1 ) & ( n − 2 ) & ( n − 3 ) & . . . ( k ) = 0 n\&(n−1)\&(n−2)\&(n−3)\&...(k)=0 n&(n−1)&(n−2)&(n−3)&... (k) = 0] the maximum value of K required
• Bit operation → binary down processing
• If all the following bits are 0, k is 0 at the highest bit of n, and in this process, it must pass through a number with only this bit of 1( 2 x , x = f l o o r ( l o g 2 n ) 2^x,x=floor(log_2n) 2x,x=floor(log2 n)), so just output[ 2 x − 1 2^x-1 2x − 1] will do
```#include<bits/stdc++.h>

using namespace std;

int main()
{
int T,n,nn,i;
bitset<10>num;
cin>>T;
while(~scanf("%d",&n))
{
nn=1;i=n;
while(i>>=1)nn<<=1;
printf("%d\n",nn-1);
}
return 0;
}
```

### B1. Palindrome Game (easy version)

Problem - B1 - Codeforces

• The meaning of the question is to give a 01 string. Two operations can be performed at a time

• Spend 1dollar: convert a 0 to 1
• No cost: reverse the string, provided that the previous step is not reverse and the current string does not contain palindromes

A wise man is the first to win, and a wise man is the last to win

The string given by easy version is originally a palindrome

• Obviously, game theory, first look at the previous experience:

• Game theory solution:
1. Determine the winning situation of one party
3. Repeat 2 operations to get the game result of the given shape
4. Realize the judgment of game results
• There are two cases for 0:

• cas1:... 0... |... 0... (symmetrical position is also 0)
• cas2:... 0... |... 1... (the symmetrical position is not 0) will not appear in the original string here
• No 0: there must be a draw

• For 0 with only cas1, there are two cases:

• To deal with the general... 0... |... 0..., the current player must pay 1dollar

• When the other party reverse s, the other party will pay another dollar and return the same situation to the other party. Although the other party may have to pay at least another dollar in the next step (if there is, that is, when there is still 0), the other party earns (2dollar) for the last hand, so the other party will guide the last step to take place in the step of the other party
• Or, the other party pays 1dollar, leaving the same situation to the second party. In this way, the payment is equal, except for the case of cas1 without changing the operation

Thus, when the scene has only one or more pairs of 0's with asymmetric center, the first hand will lose 2dollar and the second hand will win

• For the case where the center of symmetry is 0

• If Alice chooses to spend 1dollar to convert it into 0 in the first step, she will become the backhand of the previous case. In the previous case, A earns 2-1=1dollar, and A will win. In the absence of the previous case, A loses 1dollar, and B will win
• If A does not convert, because the original string is A palindrome string, it must convert A 0 to 1. Other options will only be worse
• Because it is easy version, only the existence of cas1 needs to be considered, and the existence of cas2 needs to be considered in hard version

```#include<iostream>

using namespace std;

int main()
{
int T,n,cas1,cas2,anss=0;//anss means is good to Alice
char str[1007];
cin>>T;
while(T--)
{
scanf("%d",&n);
getchar();
for(int i=0;i<n;i++)
str[i]=getchar();
str[n]='\0';
// cout<<str<<endl;
cas1=cas2=0;
for(int i=0,m=(n+1)/2-1;i<=m;i++)
{
if(str[i]==str[n-i-1])
{
if(str[i]=='0')
{
cas1+=2;//..0..|..0.. *2
}
}
else//B1 will no come to this
{
cas2++;//..1..|..0.. or ..0..|..1..
}
}
cas1-=((n&1)&&(str[(n+1)/2-1]=='0'));
//		 printf("cas1 = %2d ; cas2 = %2d ;\n",cas1,cas2);
anss=(cas1&1?(cas1==1?-1:1):(cas1==0?0:-2));
if(cas2>0)
{
if(anss>=0)
anss+=cas2;
else
anss=cas2-2+abs(anss);
}
if(anss)
{
if(anss>0)
puts("ALICE");
else
puts("BOB");
}
else
puts("DRAW");
}
return 0;
}
```

### B2. Palindrome Game (hard version)

Problem - B2 - Codeforces

• The meaning of the question is to give a 01 string. Two operations can be performed at a time

• Spend 1dollar: convert a 0 to 1
• No cost: reverse the string, provided that the previous step is not reverse and the current string does not contain palindromes

Alice takes the lead and Bob takes the lead. The string ends with 1. They are both wise men. Judge who is sure to win for the given situation

The string given by hard version is not necessarily palindrome

• As above, there are two cases for 0:

• cas1:... 0... |... 0... (symmetrical position is also 0)
• cas2:... 0... |... 1... (the symmetrical position is not 0) will not appear in the original string here
• No 0: there must be a draw

• For 0 with only cas1, there are two cases:

• To deal with the general... 0... |... 0..., the current player must pay 1dollar

• When the other party reverse s, the other party will pay another dollar and return the same situation to the other party. Although the other party may have to pay at least another dollar in the next step (if there is, that is, when there is still 0), the other party earns (2dollar) for the last hand, so the other party will guide the last step to take place in the step of the other party
• Or, the other party pays 1dollar, leaving the same situation to the second party. In this way, the payment is equal, except for the case of cas1 without changing the operation

Thus, when the scene has only one or more pairs of 0's with asymmetric center, the first hand will lose 2dollar and the second hand will win

• For the case where the center of symmetry is 0

• If Alice chooses to spend 1dollar to convert it into 0 in the first step, she will become the backhand of the previous case. In the previous case, A earns 2-1=1dollar, and A will win. In the absence of the previous case, A loses 1dollar, and B will win
• If A does not convert, because the original string is A palindrome string, it must convert A 0 to 1. Other options will only be worse
• In case of cas2:

• Alice was so happy to see that the current situation was not a palindrome string that she immediately chose to reverse cas2 times in a row. Bob had to spend 1dollar twice to eliminate this situation, so that she could reason about the continuous loss state as soon as possible

• To the last time

• If Alice gives up flipping the string and uses 1dollar to convert the last asymmetric 0 to 1, Bob will face the situation that there is only cas1 above
• If A continues to reverse, he will face only cas1

We can see that when there is only cas1, the first player will lose 2dollar, lose 1dollar and earn 1dollar. In order to make her own profit, Alice will give up the last flip when there is only cas1 left, otherwise she will not

• According to the above analysis, we can calculate the final loss and profit of A (anss in the program below), and output it according to the positive and negative judgment results

```#include<iostream>

using namespace std;

int main()
{
int T,n,cas1,cas2,anss=0;//anss means is good to Alice
char str[1007];
cin>>T;
while(T--)
{
scanf("%d",&n);
getchar();
for(int i=0;i<n;i++)
str[i]=getchar();
str[n]='\0';
// cout<<str<<endl;
cas1=cas2=0;
for(int i=0,m=(n+1)/2-1;i<=m;i++)
{
if(str[i]==str[n-i-1])
{
if(str[i]=='0')
{
cas1+=2;//..0..|..0.. *2
}
}
else
{
cas2++;//..1..|..0.. or ..0..|..1..
}
}
cas1-=((n&1)&&(str[(n+1)/2-1]=='0'));
//		 printf("cas1 = %2d ; cas2 = %2d ;\n",cas1,cas2);
anss=(cas1&1?(cas1==1?-1:1):(cas1==0?0:-2));
if(cas2>0)
{
if(anss>=0)
anss+=cas2;
else
anss=cas2-2+abs(anss);
}
if(anss)
{
if(anss>0)
puts("ALICE");
else
puts("BOB");
}
else
puts("DRAW");
}
return 0;
}
```

## Codeforces Round #723 (Div. 2)

Codeforces Round #723 (Div. 2)

### A. Mean Inequality

Problem - A - Codeforces

• The meaning of the title is to 2 n 2n 2n numbers are arranged in a ring, and one arrangement is required to make the adjacent three numbers unequal (no number is the average of the numbers on both sides)
• After sorting, the array is divided into two parts with a length of n (greater than the median and less than the median), which can be placed Cross (the large middle clip is small, and the small middle clip is large). Because the large average must be greater than the median, and the small average must be less than the median, this is certainly feasible
• Note that the allocated space of the initial array (before splitting) should have 2n elements
```#include<bits/stdc++.h>

using namespace std;

int main()
{
int T,n,nn,a[55],b[55];//Space, 2n ah ah ah ah ah ah
cin>>T;
while(T--)
{
scanf("%d",&n);
nn=n*2;
for(int i=0;i<nn;i++)
scanf("%d",a+i);
sort(a,a+nn);
for(int i=0;i<n;i++)
b[i]=a[i+n];

printf("%d %d",a[0],b[n-1]);
for(int i=1;i<n;i++)
printf(" %d %d",a[i],b[n-i-1]);
putchar('\n');
}
return 0;
}
```

### B. I Hate 1111

Problem - B - Codeforces

• The meaning of the question is to give T numbers and judge whether each number can be divided into several { 11 , 111 , 1111 , 11111 , . . . } \{11,111,1111,11111,...\} {11,111,1111,11111,...} And
• because { 1111 , 11111 , . . . } \{1111,11111,...\} {1111,11111,...} Can be { 11 , 111 } \{11,111\} {11111} composition, so just look at whether it is divided by 11 after subtracting several 111
```#include<bits/stdc++.h>

using namespace std;

//1111,11111,111111... Can be composed of 111 and 11, so just split into several 111 and 11

int main()
{
int T,n;
cin>>T;
while(T--)
{
scanf("%d",&n);
while(n>=0)
{
if(n%11==0)
{
puts("YES");
n=0;
break;
}
n-=111;
}
if(n)
puts("NO");
}
return 0;
}
```

Topics: Algorithm acm