Блог пользователя Murasame-chan

Автор Murasame-chan, история, 3 месяца назад, По-английски

Hello Codeforces!

I'm glad to invite you to my first contest, Murasame's Contest 1 (Div. 2.5)! Whether you are looking to sharpen your skills or enjoy some interesting problems, everyone is welcome to join.

The contest will start at 01.16.26 17:50 (UTC+8, China Standard Time) and last for 8 days. There are 5 problems in total, with difficulty ranging from Div. 3 to Div. 2 (I guess). All the problems are authored by me.

Since this is a long contest, feel free to solve the problems at your own pace. I hope you find the problem set interesting and educational.

I would like to thank the following people for making the contest possible:

  • You for participating in the contest.

This is an ICPC-style contest, where every problem has the same weight, and every unaccepted non-CE submission will incur a penalty.

Good luck and have fun!

About how to register: Open https://mirror.codeforces.com/contestInvitation/bbbb4ddb6f3fc3f77ea467a10a06e35181e87b5f, click "Register" on the right, then click "Enter" on the left to enter the contest.

UPD: statement for E has been changed

The previous statement was incorrect due to AI's misinterpretation during the statement translation. I manually fixed it and your penalties on problem E (before the fix) have been removed. Apologies for the bad experience.

UPD2: Congratulations to the winners

Editorial will be available soon.

UPD3: Editorial is out!

Check it here (pdf): https://file.murasame.site/shared/cp/cf/contest-1-solution.pdf

  • Проголосовать: нравится
  • +51
  • Проголосовать: не нравится

»
3 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by Murasame-chan (previous revision, new revision, compare).

»
3 месяца назад, скрыть # |
 
Проголосовать: нравится +5 Проголосовать: не нравится

Overnight, a large number of user-created contests sprang up in Codeforces like mushrooms after rain.

»
3 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

That would be great if you add constraints

»
3 месяца назад, скрыть # |
 
Проголосовать: нравится +13 Проголосовать: не нравится

yay, first AK and first solves for B, D and E!

I really enjoyed this contest, thank you. I wish codeforces had official long contests (even if unrated), I think I like this format better than 2-3 hour contests.

»
3 месяца назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

Hi, for the second example in B, shouldn't it be -1? it's not possible to add anything to 2 such that the sum stays <= 3 while also making it divisible by 10. The given answer implies that the new number will be either 2+2=4, or 2+0=2 which are not divisible by 10.

Please correct me if I am misunderstanding something.

»
3 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I wonder if Problem D can be solved using Python. I tried my best but failed. Hope someone would finally succeed.

  • »
    »
    3 месяца назад, скрыть # ^ |
     
    Проголосовать: нравится +5 Проголосовать: не нравится

    After several attempts with Gemini, I managed to create a solution that works:

    #Created by Gemini
    
    import sys
    import math
    
    def solve():
        line = sys.stdin.readline()
        if not line:
            return
        n = int(line.strip())
        
        res = []
        res_append = res.append
        gcd = math.gcd
        
        limit_r = int(n**0.5) + 1
        for r in range(2, limit_r):
            r_sq = r * r
            start_s = 1 if r & 1 == 0 else 2
            for s in range(start_s, r, 2):
                z_p = r_sq + s * s
                if z_p > n:
                    break
                
                if gcd(r, s) == 1:
                    x_p = r_sq - s * s
                    y_p = (r * s) << 1
                    
                    if x_p > y_p:
                        x_p, y_p = y_p, x_p
                    
                    p = (x_p << 40) | (y_p << 20) | z_p
                    curr = p
                    for _ in range(n // z_p):
                        res_append(curr)
                        curr += p
        
        res.sort()
        
        mask = (1 << 20) - 1
        sys.stdout.write("\n".join(f"{v >> 40} {(v >> 20) & mask} {v & mask}" for v in res) + "\n")
    
    if __name__ == "__main__":
        solve()
    

    Naively converting my C++ solution to Python resulted in ~6-7 seconds for $$$n = 1e6$$$ in custom invocation, and my first few attempts at coaxing gemini to optimize it further did not improve that.

    What finally made it work was using integers instead of tuples (my original C++ solution used std::array<long long, 3>).

    To quote gemini:

    The 6-7 second bottleneck you're experiencing in Python is primarily due to object creation overhead. Creating ~814,000 tuple objects and then sorting them is very slow because Python has to allocate memory for each individual tuple and handle pointers for each element.

    To get this under 2 seconds on PyPy 3, we must bypass tuple creation. The fastest approach is to pack the triples into a single 64-bit integer. Since $$$n=1,000,000$$$ fits in 20 bits ($$$2^{20}≈1.04×10^6$$$), we can store $$$(x,y,z)$$$ as one large integer: $$$x⋅2^{40}+y⋅2^{20}+z$$$. Sorting a list of integers is significantly faster than sorting a list of objects.

    Yet another reason python sucks, it feels like every time I use this language I discover another reason to hate it.

    N.B. I don't actually know if what Gemini said is accurate, but I trust it to know more about python than me. And, the following program takes ~600ms vs ~4s in custom invocation when commenting vs uncommenting the data.sort() line, so Python definitely is slow at sorting tuples:

    import random
    
    N = 1_000_000
    LOW = 1
    HIGH = 1_000_000
    
    data = [
        (random.randint(LOW, HIGH),
         random.randint(LOW, HIGH),
         random.randint(LOW, HIGH))
        for _ in range(N)
    ]
    
    data.sort()
    
»
3 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by Murasame-chan (previous revision, new revision, compare).

»
3 месяца назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Auto comment: topic has been updated by Murasame-chan (previous revision, new revision, compare).

»
3 месяца назад, скрыть # |
 
Проголосовать: нравится +5 Проголосовать: не нравится

will there be contest 2?