Little_Sheep_Yawn's blog

By Little_Sheep_Yawn, history, 4 months ago, In English

It's been a rough year because of graduation things and stuff, but somehow I survived!

Some things I'm proud of:

  • Finally a full dark green problem solving profile.

  • Finally hold a Codeforces round of mine.

  • Keep writing solutions of 2 problems from Monday to Saturday and post them on the github.

  • Somehow I'm a friend of 2026 people, which seems something to mark.

Enough for this year. I'm not that ready but really curious about next year.

Full text and comments »

  • Vote: I like it
  • +89
  • Vote: I do not like it

By Little_Sheep_Yawn, 6 months ago, In English

2153A - Circle of Apple Trees

Hint 1
Hint 2
Solution
Code (C++, maomao90)
Code (Pypy3.10, Little_Sheep_Yawn)

2153B - Bitwise Reversion

Hint
Solution
Code (C++, maomao90)
Code (Pypy3.10, Little_Sheep_Yawn, using the bonus)
Bonus

2153C - Symmetrical Polygons

Hint 1
Hint 2
Solution
Code (C++, maomao90)
Code (Pypy3.10, Little_Sheep_Yawn)

2153D - Not Alone

Hint 1
Hint 2
Solution
Code (C++, maomao90)
Code (Pypy3.10, Little_Sheep_Yawn)

2153E - Zero Trailing Factorial

Hint 1
Hint 2
Hint 3
Solution
Code (C++, maomao90)
Code (Pypy3.10, Little_Sheep_Yawn)

2153F - Odd Queries on Odd Array

Hint 1
Hint 2
Solution
Code (Pypy 3.10, Little_Sheep_Yawn)
Code (C++, maomao90, slightly different)

Full text and comments »

  • Vote: I like it
  • +117
  • Vote: I do not like it

By Little_Sheep_Yawn, history, 7 months ago, In English

Hello Codeforces,

We are very glad to invite you to participate in Codeforces Round 1057 (Div. 2), which will start on Oct/10/2025 17:35 (Moscow time). You will be given 6 problems and 2 hours to solve them.

All the problems are written and prepared by me and maomao90.

We would like to give our sincere thanks to:

The score distribution is $$$500 - 750 - 1250 - 1750 - 2250 - 3250$$$.

Hope everyone will enjoy the round!

Editorial is out now!

Full text and comments »

  • Vote: I like it
  • +400
  • Vote: I do not like it

By Little_Sheep_Yawn, history, 13 months ago, In English

Sorry for such a title. But I believe this topic will interest a lot of people. (When writing this post, I am informed that this stage would be unrated. Nevertheless, I decide to post this. After all, I paid a lot of effort in it.)

This passage is about the Problem J — Hand Cricket on this stage. My team believe that EVERY "passed" submission in this problem is wrong and we can offer a hacking case for these submissions (and probably for the standard solution).

If you haven't seen the problem description, here it is:

Hacking Case

If you just wonder what is our hacking case, here it is:

3
100 100 1
1
1 3 0

If you try some passed submissions on this case, they all give $$$371894957$$$ as their answer, which is $$$\frac{100}{51}\bmod 998244353$$$ . The strategy chosen by these submissions are actually $$$(p_1,p_2,p_3)=(\frac{1}{102},\frac{1}{102},\frac{100}{102}), (q_1,q_2,q_3)=(\frac{1}{102},\frac{1}{102},\frac{100}{102})$$$ .

However, there's clearly a better solution: For Alice, she can choose $$$(p_1,p_2,p_3)=(\frac{1}{2},\frac{1}{2},0)$$$ . In that way, no matter what strategy Bob chooses, Alice is guaranteed to gain at least $$$50$$$ points, which is far larger than the answer mentioned above. Actually, the equilibrium here is $$$(p_1,p_2,p_3)=(\frac{1}{2},\frac{1}{2},0),(q_1,q_2,q_3)=(\frac{1}{2},\frac{1}{2},0)$$$ , rather than the answer provided by the "correct answer".

What happened?

We ignore the $$$K_i$$$ in the problem statement and let it be $$$0$$$, and we try to answer just a single query.

In that way, we just need to figure out what is the best strategies of Alice and Bob when the subarray is fixed.

Let's say the elements of this subarray are $$$[v_1,v_2,\dots,v_k]$$$ , and Alice chooses his strategy as $$$(p_1,p_2,\dots,p_k)$$$ , Bob chooses his strategy as $$$(q_1,q_2,\dots,q_k)$$$ .

Alice tries to solve this optimization problem: $$$\max_{(p_1,p_2,\dots,p_k)}\sum\limits_{i=1}^k v_ip_i(1-q_i)$$$

Bob tries to solve this optimization problem: $$$\min_{(q_1,q_2,\dots,q_k)}\sum\limits_{i=1}^k v_ip_i(1-q_i)$$$

Both equations are linear, so it seems quite easy.

For Alice, if she knew about $$$(q_1,q_2,\dots,q_k)$$$, she could find the maximum value of $$$v_i(1-q_i)$$$ and distribute her probability on those $$$i'$$$-s such that $$$v_{i'}(1-q_{i'})$$$ reaches this maximum. (Statement 1)

Same for Bob. If he knew about $$$(p_1,p_2,\dots,p_k)$$$, she could find the minimum value of $$$v_ip_i$$$ and distribute his probability on those $$$i'$$$-s such that $$$v_{i'}p_{i'}$$$ reaches this minimum. (Statement 2)

So far, so good, right? But we don't actually know which $$$i'$$$-s to consider . (And the hypothesis made by a lot of teams is that probability should be distributed in each $$$i=1,2,\dots,k$$$, which is just not true.)

Let's say in the final equilibrium, we distribute the probability over a subset $$$i_1,i_2,\dots,i_t\ (t\geq 1)$$$ . Then we can say: $$$p_{i_1}\gt 0, p_{i_2}\gt 0, \dots, p_{i_t}\gt 0$$$ .

Then, from what we have discussed (Statement 1), $$$v_{i_1}(1-q_{i_1}),v_{i_2}(1-q_{i_2}),\dots, v_{i_2}(1-q_{i_2})$$$ all reach maximum, and are therefore the same. So $$$v_{i_1}(1-q_{i_1})=v_{i_2}(1-q_{i_2})=\dots=v_{i_t}(1-q_{i_t})$$$ .

And, for other $$$i$$$ not in $$${i_1,i_2,\dots,i_t}$$$ , Bob has no reason to give probability on any of them, because Alice will never choose them, so $$$q_{i_1}+q_{i_2}+\dots+q_{i_t}=1$$$ , that is, $$$(1-q_{i_1})+(1-q_{i_2})+\dots+(1-q_{i_t})=t-1$$$ .

From these equations, we can get that $$$1-q_{i_k}=(t-1)\frac{\frac{1}{v_k}}{\sum\limits_{j=1}^t\frac{1}{v_j}}\gt 0\ (1\leq k\leq t)$$$ .

Therefore, from what we have discussed (Statement 2), $$$v_{i_1}p_{i_1},v_{i_2}p_{i_2},\dots,v_{i_t}p_{i_t}$$$ reach maximum. So we have $$$v_{i_1}p_{i_1}=v_{i_2}p_{i_2}=\dots=v_{i_t}p_{i_t}$$$ . And we have $$$p_{i_1}+p_{i_2}+\dots+p_{i_t}=1$$$ 。

From these equations, we can get that $$$p_{i_k}=\frac{\frac{1}{v_k}}{\sum\limits_{j=1}^t\frac{1}{v_j}}\ (1\leq k\leq t)$$$ .

And we use these results to get the expectation of the final gain for Alice:

$$$\sum\limits_{k=1}^t \left(v_{i_k}\times\frac{\frac{1}{v_k}}{\sum\limits_{j=1}^t\frac{1}{v_j}}\times(t-1)\frac{\frac{1}{v_k}}{\sum\limits_{j=1}^t\frac{1}{v_j}}\right)=\frac{t-1}{\sum\limits_{j=1}^t\frac{1}{v_j}}$$$ .

So, we should choose the largest $$$t$$$ $$$v_j$$$-s to maximize this value. However, $$$t$$$ is yet to be determined, and it doesn't seem to have an easy way to find the best $$$t$$$ . And comparisons between results of different $$$t$$$-s without precision loss are seemingly quite difficult for me.

Some comments

There is one way to make this problem solvable: Reduce the problem to one query and return a real value for each query, not something about modulo. Then, we can iterate over the number of numbers to choose and we add those small numbers.

Our team spent quite long hours trying to get a glympse of this problem's solution, only to find the answers of others are just wrong. It is quite upset for us.

But there are more things about this problem. Here, instinct tells us that we don't have much reason to ignore some options. But logic tells something different. So careful analysis can be the key.

So, maybe, Anti-Instinct is yet another AI we should never ignore.

Full text and comments »

  • Vote: I like it
  • +122
  • Vote: I do not like it

By Little_Sheep_Yawn, history, 20 months ago, In English

Finally made it throughout a year! Congratulations to me!

It seems that the page can be different in different time zones, so it is quite normal if what you see isn't this graph. (FYI, I'm currently in China.)

Wish things will be better!

Full text and comments »

  • Vote: I like it
  • +317
  • Vote: I do not like it

By Little_Sheep_Yawn, history, 2 years ago, In English

I was practicing when I came across this problem. And I submitted a few codes which are almost the same, but their running time is quite different. Here's the thing:

The fastest one is here, which only runs 1590ms:

n, mod = map(int, input().split())

dp = [0] * (n * (n - 1) + 1)
diff = [0] * (n * (n - 1) + 1)
msk = n * (n - 1) // 2
v = 1

wing = 0
for i in range(n):
    
    for j in range(msk - wing, msk + wing + 1):
        if dp[j]:
            diff[j] += dp[j]
            diff[j + (n - i - 1) + 1] -= dp[j]
    
    cur = 0
    for j in range(msk - wing, msk + wing + n - i):
        cur += diff[j]
        cur %= mod
        dp[j] = cur
        diff[j] = 0
    
    for j in range(msk - wing, msk + wing + n - i):
        if dp[j]:
            diff[j - (n - i - 1)] += dp[j]
            diff[j + 1] -= dp[j]
    
    cur = 0
    for j in range(msk - wing - (n - i - 1), msk + wing + n - i):
        cur += diff[j]
        cur %= mod
        dp[j] = cur
        diff[j] = 0
    
    for j in range(-(n - i - 1), 0):
        dp[j + msk] += v * (n - i + j) % mod
        dp[j + msk] %= mod
    
    v = v * (n - i) % mod
    wing += n - i - 1

print(sum(dp[msk+1:]) % mod)

Yet, another code which is basically the same as the former one needs 3743ms:

def main():
    n, mod = map(int, input().split())

    dp = [0] * (n * (n - 1) + 1)
    diff = [0] * (n * (n - 1) + 1)
    msk = n * (n - 1) // 2
    v = 1
    
    wing = 0
    for i in range(n):
        
        for j in range(msk - wing, msk + wing + 1):
            if dp[j]:
                diff[j] += dp[j]
                diff[j + (n - i - 1) + 1] -= dp[j]
        
        cur = 0
        for j in range(msk - wing, msk + wing + n - i):
            cur += diff[j]
            cur %= mod
            dp[j] = cur
            diff[j] = 0
        
        for j in range(msk - wing, msk + wing + n - i):
            if dp[j]:
                diff[j - (n - i - 1)] += dp[j]
                diff[j + 1] -= dp[j]
        
        cur = 0
        for j in range(msk - wing - (n - i - 1), msk + wing + n - i):
            cur += diff[j]
            cur %= mod
            dp[j] = cur
            diff[j] = 0
        
        for j in range(-(n - i - 1), 0):
            dp[j + msk] += v * (n - i + j) % mod
            dp[j + msk] %= mod
        
        v = v * (n - i) % mod
        wing += n - i - 1
    
    print(sum(dp[msk+1:]) % mod)
    return

t = 1
for _ in range(t):
    main()

I'm quite puzzled as these two codes seems nothing too different. I really wonder how this could happen as it could cause some unexpected TLEs in other questions. It would really help a lot if you could offer some insights on it.

Thanks in advance!

Full text and comments »

  • Vote: I like it
  • +11
  • Vote: I do not like it

By Little_Sheep_Yawn, history, 2 years ago, In English

In recent competitions, I came across this type of RUNTIME ERROR. Here's the thing:

I wrote a program like this:

def main():
    k = 15
    m = 3
    
    for a in range(k + 1):
        for b in range(k + 1):
            for c in range(min(m, k) + 1):
                for d in range(min(m, k) + 1):
                    continue
    return

t = 1
for _ in range(t):
    main() 

But in Atcoder, it showed something like this: (Sorry that the picture isn't clear enough)

It really bothered me and I wonder if anyone could be so kind as to help me with this issue.

(p.s. The RUNTIME ERROR doesn't show in Codeforces, so I'm really confused)

Here are some codes that don't show RUNTIME ERROR:

Code 1:

k = 15
m = 3

for a in range(k + 1):
    for b in range(k + 1):
        for c in range(min(m, k) + 1):
            for d in range(min(m, k) + 1):
                pass

Code 2:

def main():
    k = 15
    m = 3
    
    for a in range(k + 1):
        for b in range(k + 1):
            for c in range(min(m, k) + 1):
                for d in range(min(m, k) + 1):
                    print(d)
    return

t = 1
for _ in range(t):
    main()

Full text and comments »

  • Vote: I like it
  • +45
  • Vote: I do not like it