Idea: BledDest

**Tutorial**

Tutorial is loading...

**Solution (vovuh)**

```
for i in range(int(input())):
n, m = map(int, input().split())
print('YES' if n % m == 0 else 'NO')
```

Idea: Roms

**Tutorial**

Tutorial is loading...

**Solution (Roms)**

```
for t in range(int(input())):
n = input()
print(*sorted(map(int, input().split()))[::-1])
```

Idea: adedalic

**Tutorial**

Tutorial is loading...

**Solution 1 (adedalic)**

```
fun main() {
val T = readLine()!!.toInt()
testCases@for (tc in 1..T) {
val (_, k) = readLine()!!.split(' ').map { it.toLong() }
val a = readLine()!!.split(' ').map { it.toLong() }.toLongArray()
var maxPower = 1L
while (maxPower < 1e16.toLong())
maxPower *= k
while (maxPower > 0) {
val positions = a.withIndex().filter { it.value >= maxPower }.map { it.index }
if (positions.isNotEmpty()) {
if (positions.size > 1) {
println("NO")
continue@testCases
}
a[positions[0]] -= maxPower
}
maxPower /= k
}
if (a.max()!! > 0L) {
println("NO")
continue@testCases
}
println("YES")
}
}
```

**Solution 2 (adedalic)**

```
fun getMask(a: Long, k: Long): Long? {
var (tmp, res) = listOf(a, 0L)
var cnt = 0
while (tmp > 0) {
if (tmp % k > 1)
return null
res = res or ((tmp % k) shl cnt)
tmp /= k
cnt++
}
return res
}
fun main() {
val T = readLine()!!.toInt()
for (tc in 1..T) {
val (n, k) = readLine()!!.split(' ').map { it.toLong() }
val a = readLine()!!.split(' ').map { getMask(it.toLong(), k) }
val b = a.filterNotNull()
if (b.size < n) {
println("NO")
continue
} else {
val res = b.reduce { acc, l -> if (acc < 0 || (acc and l) > 0) -1 else acc or l }
println(if (res < 0) "NO" else "YES")
}
}
}
```

Idea: BledDest

**Tutorial**

Tutorial is loading...

**Solution (BledDest)**

```
#include <bits/stdc++.h>
using namespace std;
const int N = 200043;
const int MOD = 998244353;
int add(int x, int y)
{
x += y;
while(x >= MOD) x -= MOD;
while(x < 0) x += MOD;
return x;
}
int mul(int x, int y)
{
return (x * 1ll * y) % MOD;
}
int binpow(int x, int y)
{
int z = 1;
while(y)
{
if(y & 1) z = mul(z, x);
x = mul(x, x);
y >>= 1;
}
return z;
}
int inv(int x)
{
return binpow(x, MOD - 2);
}
int divide(int x, int y)
{
return mul(x, inv(y));
}
int fact[N];
void precalc()
{
fact[0] = 1;
for(int i = 1; i < N; i++)
fact[i] = mul(fact[i - 1], i);
}
int C(int n, int k)
{
return divide(fact[n], mul(fact[k], fact[n - k]));
}
int main()
{
precalc();
int n, m;
cin >> n >> m;
int ans = 0;
if(n > 2)
ans = mul(C(m, n - 1), mul(n - 2, binpow(2, n - 3)));
cout << ans << endl;
}
```

Idea: MikeMirzayanov

**Tutorial**

Tutorial is loading...

**Solution (adedalic)**

```
#include<bits/stdc++.h>
using namespace std;
#define fore(i, l, r) for(int i = int(l); i < int(r); i++)
#define sz(a) int((a).size())
#define x first
#define y second
typedef long long li;
typedef long double ld;
typedef pair<int, int> pt;
template<class A, class B> ostream& operator <<(ostream& out, const pair<A, B> &p) {
return out << "(" << p.x << ", " << p.y << ")";
}
template<class A> ostream& operator <<(ostream& out, const vector<A> &v) {
out << "[";
fore(i, 0, sz(v)) {
if(i) out << ", ";
out << v[i];
}
return out << "]";
}
const int INF = int(1e9);
const li INF64 = li(1e18);
const ld EPS = 1e-9;
const int N = 555;
int n, a[N];
inline bool read() {
if(!(cin >> n))
return false;
fore(i, 0, n)
cin >> a[i];
return true;
}
int dp[N][N];
int calcDP(int l, int r) {
assert(l < r);
if(l + 1 == r)
return dp[l][r] = a[l];
if(dp[l][r] != 0)
return dp[l][r];
dp[l][r] = -1;
fore(mid, l + 1, r) {
int lf = calcDP(l, mid);
int rg = calcDP(mid, r);
if(lf > 0 && lf == rg)
return dp[l][r] = lf + 1;
}
return dp[l][r];
}
int dp2[N];
inline void solve() {
fore(i, 0, N)
dp2[i] = INF;
dp2[0] = 0;
fore(i, 0, n) {
fore(j, i + 1, n + 1) {
if(calcDP(i, j) > 0)
dp2[j] = min(dp2[j], dp2[i] + 1);
}
}
cout << dp2[n] << endl;
}
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
int tt = clock();
#endif
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cout << fixed << setprecision(15);
if(read()) {
solve();
#ifdef _DEBUG
cerr << "TIME = " << clock() - tt << endl;
tt = clock();
#endif
}
return 0;
}
```

Idea: BledDest

**Tutorial**

Tutorial is loading...

**Solution (BledDest)**

```
#include<bits/stdc++.h>
using namespace std;
const int N = 300043;
const int K = 5;
int x, y, z, n;
long long a[N];
typedef vector<vector<int> > state;
map<state, int> d;
int cnt;
int p;
vector<vector<int> > state_log;
int mex(const vector<int>& a)
{
for(int i = 0; i < a.size(); i++)
{
bool f = false;
for(auto x : a)
if(x == i)
f = true;
if(!f)
return i;
}
return a.size();
}
state go(state s)
{
int f1 = mex({s[0][K - x], s[1][K - y], s[2][K - z]});
int f2 = mex({s[0][K - x], s[2][K - z]});
int f3 = mex({s[0][K - x], s[1][K - y]});
state nw = s;
nw[0].push_back(f1);
nw[1].push_back(f2);
nw[2].push_back(f3);
for(int i = 0; i < 3; i++)
nw[i].erase(nw[i].begin());
return nw;
}
void precalc()
{
d.clear();
state cur(3, vector<int>(K, 0));
cnt = 0;
state_log.clear();
while(!d.count(cur))
{
d[cur] = cnt;
state_log.push_back({cur[0].back(), cur[1].back(), cur[2].back()});
cur = go(cur);
cnt++;
}
p = cnt - d[cur];
}
int get_grundy(long long x, int t)
{
if(x < cnt)
return state_log[x][t];
else
{
int pp = cnt - p;
x -= pp;
return state_log[pp + (x % p)][t];
}
}
void read()
{
scanf("%d %d %d %d", &n, &x, &y, &z);
for(int i = 0; i < n; i++)
scanf("%lld", &a[i]);
}
int check(int x, int y)
{
return x == y ? 1 : 0;
}
void solve()
{
precalc();
int ans = 0;
for(int i = 0; i < n; i++)
ans ^= get_grundy(a[i], 0);
int res = 0;
for(int i = 0; i < n; i++)
{
ans ^= get_grundy(a[i], 0);
res += check(ans, get_grundy(max(0ll, a[i] - x), 0));
res += check(ans, get_grundy(max(0ll, a[i] - y), 1));
res += check(ans, get_grundy(max(0ll, a[i] - z), 2));
ans ^= get_grundy(a[i], 0);
}
printf("%d\n", res);
}
int main()
{
int t;
scanf("%d", &t);
for(int i = 0; i < t; i++)
{
read();
solve();
}
}
```

**Tutorial**

Tutorial is loading...

**Solution (BledDest)**

```
#include<bits/stdc++.h>
using namespace std;
const int N = 1000043;
map<char, int> nxt[N];
bool term[N];
int n, k;
char buf[3];
int dp[N];
int dict[N];
int T[4 * N];
int f[4 * N];
void build(int v, int l, int r)
{
T[v] = int(1e9);
if(l != r - 1)
{
int m = (l + r) / 2;
build(v * 2 + 1, l, m);
build(v * 2 + 2, m, r);
}
}
int getVal(int v)
{
return T[v] + f[v];
}
void push(int v, int l, int r)
{
T[v] += f[v];
if(l != r - 1)
{
f[v * 2 + 1] += f[v];
f[v * 2 + 2] += f[v];
}
f[v] = 0;
}
void upd(int v, int l, int r)
{
if(l != r - 1)
{
T[v] = min(getVal(v * 2 + 1), getVal(v * 2 + 2));
}
}
int get(int v, int l, int r, int L, int R)
{
if(L >= R)
return int(1e9);
if(l == L && r == R)
return getVal(v);
push(v, l, r);
int m = (l + r) / 2;
int ans = min(get(v * 2 + 1, l, m, L, min(m, R)), get(v * 2 + 2, m, r, max(L, m), R));
upd(v, l, r);
return ans;
}
void add(int v, int l, int r, int L, int R, int val)
{
if(L >= R)
return;
if(l == L && r == R)
{
f[v] += val;
return;
}
push(v, l, r);
int m = (l + r) / 2;
add(v * 2 + 1, l, m, L, min(m, R), val);
add(v * 2 + 2, m, r, max(L, m), R, val);
upd(v, l, r);
}
void setVal(int v, int l, int r, int pos, int val)
{
if(l == r - 1)
{
f[v] = 0;
T[v] = val;
return;
}
push(v, l, r);
int m = (l + r) / 2;
if(pos < m)
setVal(v * 2 + 1, l, m, pos, val);
else
setVal(v * 2 + 2, m, r, pos, val);
upd(v, l, r);
}
void dfs(int v, int d, int last)
{
dp[v] = last + 1;
if(term[v])
dp[v] = min(dp[v], get(0, 0, n + 1, 0, d));
setVal(0, 0, n + 1, d, dp[v] + 1);
if(term[v])
add(0, 0, n + 1, 0, d + 1, 1);
for(auto x : nxt[v])
dfs(x.second, d + 1, dp[v]);
setVal(0, 0, n + 1, d, int(1e9));
}
int main()
{
scanf("%d", &n);
for(int i = 0; i < n; i++)
{
int p;
scanf("%d %s", &p, buf);
char c = buf[0];
nxt[p][c] = i + 1;
}
scanf("%d", &k);
for(int i = 0; i < k; i++)
{
scanf("%d", &dict[i]);
term[dict[i]] = true;
}
build(0, 0, n + 1);
dfs(0, 0, -1);
for(int i = 0; i < k; i++)
printf("%d ", dp[dict[i]]);
puts("");
}
```

fast editorial, thanks!

In G we can maintain value "the shortest distance to the smallest line in the lexicographic order from the current prefix" in dfs. To update it just check the distance from the current vertex + 1. And to pass it to the next vertex, we need to add to the value the number of vertices from the S that we went through in other sons. It is O(n). Code.

I solved G by the same way. And This is my code.

Could you plz explain your idea more clearly?

thx :P

Main challenge is speeding up computation of f(u) = min d(v) + rank of u in subtree rooted at v, over all ancestors v of node u. Suppose we have this computed for node u, and wish to update computation for children c of u. The difference is exactly the size of sibling subtrees that come before c. This difference affects subtrees of each ancestor equally, so after adding that to existing f(u), we only need to consider the additional case for a subtree beginning at c so you get something like f(c) = min(f(u)+siblings, d(c)).

Another Example (sorry for all the junk — just look for the dfs method which is 8 LOC)

thx :P

another way to find rank(u) in subtree rooted at v is tin(u)-tin(v)+(isgood(v)) (we increment time only when we encounter a good string)

can anyone explain in problem D why we are multiplying by 2^(n — 3) ?

Each element will appear either before the maximum number, or after it. Therefore each of the N-3 numbers (that aren't the maximum and the number we have to repeat) has two options, so we have to mulitiply by 2, N-3 times.

Since there are n elements we are to choose (n-1) elements and one element will be copy of one of n-1 elements choosen by us. Now we can not make copy of max element as it violates the condition so we are left with n-2 elements so we can choose one of them.Let x be the position of max element so the element which has duplicate must be persent on either side because if it will be persent on single side the sequence can not be strictly increasing or decreasing so we have max element at x and two same elements so now we are left with n-3 elements since there are two choices for every element to either go on right hand side of max element or LHS of that max element . so 2^(n-3) factor is involved. why is it so? Since we can arrange a array in strictly increasing order if we dont have duplicates. Hope it was clarified

great explanation :)

we assume one element as left or right side of max .Isn't it possible that element become max for another combination?So why not we use 3^n-3?As there can be three position?

Notice that we have chosen the current set of (n-1) elements already and are then placing the elements in the desired order.

Hence for the current set of elements, only one will be the peak element. For another set, it might be so that an element currently on the left/right becomes the peak, but for that the initial (n-1) elements chosen will be different. Hence the factor of 3 doesn't appear here.

great explanation .Thanks!

We have already selected elements now how can our combo differ?

you explained it in a really great manner!

int mul(int x, int y) { return (x * 1ll * y) % MOD; } For what we use this func, why we cant make x-long long, and y-long long, and multiply them?

Of course, we can, there is no difference: long long 1.08 s and int 1.08s for calculation of $$$2^{28}!$$$. Only one think that you should do is declare

`mod`

as constant. If no const — long long 3.33s. It is because division by a constant can be calculated without integer division.Hey Bro can you elaborate this further

It is because division by a constant can be calculated without integer divisionBook: Hacker's Delight, 2nd Edition, Chapter 10: Integer Division By Constants. Main idea in few words: change $$$\dfrac{x}{y}$$$ by multiplication $$$x \cdot y^{-1}$$$, where $$$y$$$ is constant.

Thanks for the info so is multiplication faster than division

Can anyone please explain E problem it would be great help. Thanks in advance :)

I'll try to go along the lines of the solution:

Let $$$dp[i][j]$$$ be the value of the single remaining element, when subarray $$$[i:j]$$$ is reduced using the given operation. If there is no way to reduce this subarray into a single element, $$$dp[i][j]$$$ will be $$$-1$$$.

How do you compute $$$dp[i][j]$$$? Consider index $$$k$$$, such that $$$i \le k \lt j$$$. We divide subarray $$$[i:j]$$$ into two halves: $$$[i:k]$$$ and $$$[k+1:j]$$$.

Base case: when subarray is of size 1 $$$(i=j)$$$, answer will obviously be the number in that subarray. Now for any $$$k$$$, if $$$dp[i][k] = dp[k+1][j]$$$, i.e. we can divide subarray into two halves such that both halves are reduced to the same number, we can combine the two numbers, hence $$$dp[i][j] = dp[i][k] + 1$$$.

This is one half of the solution. We now compute the minimum number of partitions that can be reduced to size 1.

Let $$$dp2[i]$$$ : minimum number of partitions required for array $$$[1:i]$$$. We compute this in this way: Take index $$$k$$$, $$$1 \le k \le i$$$. If $$$[k:i]$$$ can be reduced to a single element, i.e. $$$dp[k][i] \ne -1$$$, then we can find optimal partitioning of subarray $$$[1:k-1]$$$ and just add one to the answer. Formally, $$$dp2[i] = min(dp2[i], dp2[k] + 1)$$$, for all $$$k$$$ such that $$$dp[k][i] \ne -1$$$.

The required answer is $$$dp[n]$$$. Here's my solution for reference: 72913085. Hope this helped.

Can you explain why will dp[i][j] reduce to a single unique value?

I do feel it's intuitive but still why is it impossible for having two different ways of combining elements of a subarray and reaching two different final merged values?

consider sequence $$$ 2^{a_1}, 2^{a_2}, ..., 2^{a_n} $$$ combining two adjacent equal values $$$ a_i = a_{i+1} $$$ is equivalent to merging two values in to one value $$$ 2^{a'} = 2^{a_i} + 2^{a_{i+1}} = 2 ^ {a_i + 1} $$$. So if [l, r] can be reduced to a single value, it must be unique.

This proof is from tmwilliamlin168 explanation

How come converting into a sum helped to prove uniqueness?

I think he means that, the "merging" operation is akin to adding 2 similar powers of 2, sum of a few powers of 2,if reduced to a single power of 2, then that term should be unique

We can prove that a subarray $$$[i:j]$$$ can be divided in atmost one way, i.e. there can be only one such $$$k$$$, for which $$$dp[i][k] = dp[k+1][j]$$$.

For this, we define a

fully reducible subarrayas a subarray which can be reduced to a single element. We claim two things: removal of elements from a fully reducible subarray will only reduce the value of the remaining element, providedthe remaining subarray is a fully reducible subarray. Similarly, addition of elements will only increase the value of the remaining element (you can try this out).Hence, if $$$dp[i][k] = dp[k+1][j]$$$, there is no possible way to transfer elements from subarray $$$[i:k]$$$ to $$$[k+1:j]$$$, or vice versa, such that $$$dp[i][k'] = dp[k'+1][j]$$$, for another $$$k'$$$.

We can also prove by contradiction,

Lets say there are two values of K, i.e K1 and K2, such that dp(i, K1) = dp(K1 + 1,j) = X and dp(i, K2) = dp(K2 + 1, j) = Y.

Lets assume K1 < K2,

If that is the case then according to the dp definition, the subarray (i, K1) reduced to the number X, and the subarray (K1 + 1, j) also reduced to X whereas the subarrays (i, K2) and (K2 + 1, j) reduced to Y,

Since K1 < K2, the subarray (i, K1) is a prefix of (i, K2) thus X < Y. And (K2 + 1, j) is a suffix of (K1 + 1, j) which suggests that X > Y.

This leads to a contradiction.

Amazing, thanks a lot :)

Thanks a lot!

Anybody like me who is thinking why does the prefix

`[1...k1]`

of a larger subarray`[1...k2]`

where`k1 < k2`

proves that`X < Y`

. The solution is that :If it's possible to merge the things from

`[1...k2]`

then Y has to be at X + 1.Because :

We know that

`[1....k2]`

==`[1....k1]`

+`[k1 + 1...k2]`

. We know from`our assumption`

that`[1....k1] == X`

so now for`[1...k2]`

to hold then`[k1 + 1... k2] should be equal to X`

. Thus`[1...k2]`

= X + 1.Thanks a lot :)

Thank you very much for this explanation! I was able to implement the solution you outlined and it passed :)

This explanation was better than the one in editorial. Thanks a lot!

Thanks a lot to all in this thread This explanation is much better than that of editorial

in problem F, why are five lines enough to determine all the other values?

Let's consider that the number of remaining soldiers is i.Because x,y,z<=5.You only need the answer to[i-5,i-1] to update i.When you know the answer to i,you delete the answer to (i-5),and add the new answer to the end of the vector.In other words,the vector is scrolling. Because the period is at most 36,you can use brute force to find it. I hope this helps. ：）

Can anyone explain how we get string "ieh" in first example in two seconds? After getting string "i" "ieh" is lexicographically second after "i", so we need at least 3 seconds, isn't it?

How to solve problem

Cusingbitmasks. Kindly help me.You can make each number of the array v into a number based on k.If the number only consists of 0 and 1, it must be able to become 0.Then, mark the position of each 1.If the number consists of other numbers or the position of some 1 is already marked, the array won't be able to be filled with 0 and the answer is "NO". After you finish that operation for each number in the array v, the answer will be "YES".This is my solusion.

I am a Chinese and I am sorry that my English is poor. if there is something wrong in my words, please tell me and I will repair it as fast as posiible.

Can you please explain What is the use of

stepvariable in your code?How we will check if this i power is used or not?don't fret about grammatical errors.. you are able to communicate your idea.. that's the important part.. many people who speak the language can't do that.. so great work !!

Is it posssible to solve G on Java?

See my solution, I had to make my recursion iterative in order to bypass stack overflow

awoo Why the same solutions receive completely different times?:

72925974 4321 ms = 72925981 2885 ms

72927822 1668 ms = 72927856 920 ms

Can anyone explain B. Bogo Sort Problem??

In this problem it was quite an observation that if we sort the array in descending(or non increasing order to be precise) then A[i]-i can never be equal to A[j]-j for i<j. Because if i<j that implies A[i]>A[j] since we sorted the array in non increasing order.Now we are subtracting larger value by smaller index example array of 5 4 will become 5-1 != 4-2. hope that helps. Here's the link to my solution

can u give the code of bogo sort?

sorry i gave link to problem A please excuse me for that. Here my code for problem B. code B

If we sort the array in non-increasing order, then we have

$$$ \begin{equation} i \lt j \end{equation}\tag{1} $$$

$$$ \begin{equation} a_i \ge a_j \end{equation}\tag{2} $$$

Rewrite $$$(1)$$$ to

$$$ \begin{equation} -i \gt -j \end{equation}\tag{3} $$$

and add $$$a_i$$$ to both side

$$$ \begin{equation} a_i - i \gt a_i -j \end{equation}\tag{4} $$$

Add $$$-j$$$ to both side to $$$(2)$$$

$$$ \begin{equation} a_i - j \ge a_j - j \end{equation}\tag{5} $$$

From $$$(4)$$$ and $$$(5)$$$, we have

$$$ \begin{equation} a_i - i \gt a_i - j \ge a_j - j \end{equation}\tag{6} $$$

which means

$$$ \begin{equation} a_i - i \gt a_j - j \end{equation}\tag{7} $$$

$$$ \begin{equation} i - a_i \ne j - a_j \end{equation}\tag{8} $$$

$$$ \begin{equation} j - a_j \ne i - a_i \end{equation}\tag{9} $$$

Therefore, after sorting the array in non-increasing order, for each pair of indexes $$$ i < j $$$ , the condition $$$ j - a_j \ne i - a_i$$$ holds.

Can anyone explain me C please.

convert given numbers into k-base system. now, do addition of k-base numbers without k-base system. now, you can observe that, if there is any bit is >= 2 then it's not possible to make given numbers using power of k. because, you can use k^some power only one time.

great way!

can u please explain in more details?

if a number is in k-base system, then if number n = 1111 = k^0 + k^1 + k^2 + k^3 , and if n = 2111 = k^0 + k^1 + k^2 + 2*k^3, means we need to add k^3 two times. so we can't make this number.

really appreciate but can you explain by taking this example:- 3 9 /n 0 59049 810

Thanx...Very Nice Explanation...I Finally got the idea...

lets assume we have to check for no. X ,if X is summation of power of k or not then X=k^n+k^m+k^l....,where n>m>l..,so if we take k^n common and divide X by k^n then resulting no. should be divisible by k, X=k^n(1+k^(m-n)+k^(l-n)) X/(k^n)-1=k^(m-n)+k^(l-n)..

code for above explanation

Solution for Problem C is not quit clear can anyone help me out with that??? adedalic

see this comment : https://codeforces.com/blog/entry/74640?#comment-587595

Could you tell me what does this

return dp[l][r] = a[l]?I think it first assigns the value of a[I] to dp[l][r] and then returns the value of dp[l][r];:^)

Thanks for replying. It seems true.

Can Anyone explain Problem D...why we have to find inverse elements?

Because of n! / (k! * (n-k)!), and the division can be written as n! * inverse(k! * (n-k)!).

Using Fermat's Little Theorem, a^(p-1) (triple-bar) 1 mod(p). Multiplying both sides by a^-1, we have a^(p-2) (triple-bar) a^-1 mod(p), where p is 998244353.

Therefore, the inverse can be simplified to a^(p-2).

I have an $$$O(n \log n)$$$ solution to problem E: https://codeforces.com/blog/entry/74656

Can someone please explain how time complexity of problem E is n^3 ? Thanks.

Figured it out after rethinking the problem. Let's think of the main function for loop and calcDP separately. So calcDP is an N^3 function while our for loop runs in N^2. They are independent entities. So when you call calcDP from inside your N^2 for loop, their complexities don't affect each other.

There is another solution to problem D with complexity $$$O(n \log P)$$$.

Welcome to see in https://codeforces.com/blog/entry/74685. For the Chinese version https://andylizf.github.io/2020/03/10/CF1312D-Count-the-Arrays.

Can anyone prove it is equal to the normal solution?

Hey BledDest, adedalic, awoo.

please see why this code is getting accepted

I think there should be an extra condition for when n is odd, please check.

Also, this code is getting Accepted.

can anyone explain c better plz?

The question states that you have an array ( which is initially all zeros ), and you apply some number of operations, say $$$M$$$.

In $$$i$$$th operation ( $$$0 \le i \lt M$$$ ), you can either pass and go to step $$$i+1$$$, or add $$$k^i$$$ to one of the elements of the array.

Notice that, each power of $$$k$$$ is used

atmostonce, if at all. Thus, we simply convert each number to base $$$k$$$, and see if any power of $$$k$$$ is repeated.Another possible solution of 1312E — Array Shrinking. Complexity is O(V * N) where V is limit of

`a`

. The solution is here.To solve F, I used the heuristic that the period is equal to $$$smallest+largest$$$ out of $$$x,y,z$$$ It only has 2 exceptions (considering $$$y$$$ and $$$z$$$ equivalent): $$$1,3,4$$$ (period=7) and $$$4,1,2$$$ (period=3).

I have no clue on why this works, wondering if it just somehow worked because of small constraints on the values. Any insights?

Link to my code: 73467272

what is the time complexity of D,is it O(m log p) or (n log p)??

I think the time complexity is only O(log p).

For problem G,we don't need any data struct .Simple dfs is enough.

The code is very short:

CodeThough there are many details,it's still much simpler than solutions using data structures.Also,it's very fast.

In problemA, if we consider case n = 8 and m = 4 then how can a convex polygon with 4 sides can have a same center.

Thanks, in advance

Number the vertex from 0 to n-1 (7) take 0th and 2nd and 4th and 6th vertex

How do you prove that we can have the vertices of smaller polygon in common with larger polygon?

I don't have a math-heavy perfect proof but you can think in this way.

Lets call polygon of m sides be polygonIn and polygon of n sides be polygonOut. select one side of polygonIn and connect it to the center of polygon the angle will be (2*pi/m) also note that this angle is equal to x*(2*pi/n) (x is some integer) because the end points of a side of polygonIn must be end points of some x continous sides of polygonOut.

Example in case of hexagon and triangle x = 2 because end points of one side of triangle is also end points of 2 continous sides of hexagon.

so we get x*(2*pi/n) = 2*pi/m

x*m = n

n%m = 0

Can anyone please explain the solution to F — Attack on Red Kingdom?

How can we check the period is at most 36 by brute force ?

Is there a theoretical proof for the bound on period ?

I don't know about the 36 brute force thing but I think I know about why some people searched period on only 1000 values.

Here is my shallow understanding:

You have only 3 attack possible at each step. This means that the grundy values will always be between 0<=G<=3 (there will be at most 3 different children in the set with which we compute the mex).

Consider a series of Grundy values of length N = m(4^m + 1). You can make 4^m + 1 groups of size m. Each group of grundy values can take at most 4^m different values (Indeed, remember that a grundy value can only take value G=0, 1, 2 or 3). By the pigeon principle two of this groups must be the same.

For m = 4 N = 4 * 256 = 1025 This means if we look at the 1025 first values we are guaranted there is going to be 2 groups of 4 grundy values that are going to be identical.

Why is it a real period ? This should have to do with the fact if you have a pattern of length = Nb of different possible grundy values (here 4 ) then it's a valid pattern. ( I'm not sure about this )

I don't have a formal proof, but consider that we look again on the 1025 next character that would give us :

We are guaranted to find a 3rd time the pattern e f g h , and since e2 and e3 are the same we are guaranted that their respect children (one child for each type of attack), e_C21 e_C22 e_C23 and e_C31, e_C32, e_C33 will be such that

My guess is that you should be able to conclude the values are equal (although right know i'm not sure of how to prove it).

Actually if has someone has a nicer explanation on this, please do illuminate me ..

In problem E, would the single representative of a subarray always be unique (to cache beforehand), would appreciate if someone can give a proof.

I think This is really good .

Got it, thank you

I have an alternate characterization for [D] : Count the Arrays. (which would time-out when implemented naively). Can anyone help me prove the equivalence of the 2 results?

Let us fix the index of the maximum element as $$$pos$$$ and the value of the maximum element as $$$max$$$. Assume one based indexing.

First, we will fill all the elements in the left half. Clearly, there are $$$(pos-1)$$$ slots and $$$(max-1)$$$ elements to choose from. The total number of ways is $$$max-1 \choose pos-1$$$.

Now, let us pick the element which would be duplicated. We have $$$(pos-1)$$$ choices.

Finally, we need to fill the elements in the right half. We have $$$(n - pos - 1)$$$ empty slots (as one slot is reserved for the duplicated element). Also, we have $$$(max - 1 - (pos - 1))$$$ numbers to choose from, i.e. the total number of choices is $$$max - pos \choose n - pos -1$$$

So, for a fixed choice of $$$pos$$$ and $$$max$$$, the total number of choices is $$$(pos - 1) {max - 1 \choose pos - 1} {max - pos \choose n - pos - 1}$$$

As for the limits, we can easily see the $$$ 1<pos<n$$$, while $$$n-1 \leq max \leq m$$$.

So, the final result is $$$\sum_{pos = 2}^{n-1} \sum_{max = n-1}^{m} (pos - 1) {max - 1 \choose pos - 1} {max - pos \choose n - pos - 1}$$$.

However, with an alternate analysis, we know that the answer is $$${m \choose n-1} (n-2) 2^{n-3}$$$.

Can anyone guide me on how to prove the equivalence of the 2 results?

can anyone please explain the editorial of problem C and what is s in k^s? Thankyou.

it is a power of k from descending order

What we are required to do in the question is we need to find whether we can convert the array v which is initialised to 0 into the array a.

We will convert the values of array a into k-ary representation. So lets say I have an array= {0 , 59049 , 810} then representation of this array in base-9 is {0,100000,1100}. Every bit in this array is either 0 or 1 and also each 1 is at different position. So we can make the array v into a.

Lets say we have an array={21,27} and base=3 So representation of this array in base-3 is {210,1000} Here, we require the 3rd bit in 21 two times i.e. we require 3^2 two times. Hence we cannot make the array v into a.

Generally, in k-ary representation of array any bit is greater than 1 then answer is NO,otherwise YES.

Hope this helps.

Can any one help me with my solution (81649940) to Problem E.

my approach :

dp[i][j][0] — min length of which can be obtained from subarray [i,j]

dp[i][j][1]- after reduction of this([i,j]) subarray what is the left most value eg if array is 6 6 3 3 5 then dp[i][j][2] = 7;(from reduced array- 7 4 5)

dp[i][j][2]- after reduction of this subarray what is the right most value eg. if array is 6 6 3 3 5 then dp[i][j][2] = 5;(from reduced array- 7 4 5)

can someone explain me problem C. It would be great help. Thanks

resolved

can anyone explain Problem A in detail?

no

This tutorial is not linked in the problems, for example 1312A - Two Regular Polygons Maybe someone can fix this.

fixed