<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Manas Gupta on Medium]]></title>
        <description><![CDATA[Stories by Manas Gupta on Medium]]></description>
        <link>https://medium.com/@guptamanas95?source=rss-30271b9e2be5------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*hgqDyEcqYIiJl4PFKYrRew.jpeg</url>
            <title>Stories by Manas Gupta on Medium</title>
            <link>https://medium.com/@guptamanas95?source=rss-30271b9e2be5------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 01:32:01 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@guptamanas95/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Game Theory in Competitive Programming Part 13]]></title>
            <link>https://medium.com/intellectually-yours/game-theory-in-competitive-programming-part-13-6258baeda671?source=rss-30271b9e2be5------2</link>
            <guid isPermaLink="false">https://medium.com/p/6258baeda671</guid>
            <dc:creator><![CDATA[Manas Gupta]]></dc:creator>
            <pubDate>Thu, 07 Apr 2022 12:03:27 GMT</pubDate>
            <atom:updated>2022-04-07T12:03:27.750Z</atom:updated>
            <content:encoded><![CDATA[<p>Atcoder: Prime Sum Game</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/665/0*ZV5XquMY-cof-0KW.jpg" /></figure><p><em>Welcome to part 13 of this series, in case you have missed part 12, here is the link: </em><a href="https://medium.com/intellectually-yours/game-theory-in-competitive-programming-part-12-3f5036a21c14"><em>Part 1</em></a><em>2</em></p><h4><strong>Problem Statement</strong></h4><p>Takahashi and Aoki are playing a game.</p><ul><li>First, Takahashi chooses an integer between A and B (inclusive) and tells it to Aoki.</li><li>Next, Aoki chooses an integer between C and D (inclusive).</li><li>If the sum of these two integers is a prime, then Aoki wins; otherwise, Takahashi wins.</li></ul><p>When the two players play optimally, which player will win?</p><h4>Problem Link</h4><p><a href="https://atcoder.jp/contests/abc239/tasks/abc239_d">D - Prime Sum Game</a></p><h4><strong>Input</strong></h4><p>Input is given from Standard Input in the following format:</p><p>A B C D</p><h4><strong>Output</strong></h4><p>If Takahashi wins when the two players play optimally, print “Takahashi”; if Aoki wins, print “Aoki”.</p><h4><strong>Example</strong></h4><p><strong>Input:</strong> 2 4 3 4</p><p><strong>Output:</strong> Aoki</p><p><strong>Explanation:</strong> Takahashi can choose any number from [2,4] and Aoki can choose any number from [3,4]. If Takahashi chooses 2, then Aoki can choose 3, as 2+3=5, which is a prime number. Similarly for 3 and 4, Aoki can choose 4 and 3 respectively, as they both add up to 7, which is a prime number. So, Aoki wins no matter what Takahashi chooses.</p><p><strong>Input:</strong> 3 8 2 4</p><p><strong>Output:</strong> Takahashi</p><p><strong>Explanation:</strong> If Takahashi chooses 6, then Aoki can form 8,9 and 10 only, and we can observe that all of them are composite numbers. So, Takahashi wins in this case!</p><h4><strong>Hint</strong></h4><p>Can we find even a single number in [a,b], suppose x, such that the range [x+c, x+d] has all composite numbers only?</p><h3><strong>Solution</strong></h3><p>Whenever Takahashi chooses a number, suppose x, then Aoki can form any number in the range [x+c, x+d].</p><p>So first, we observe the range of all possible sums that may arise, which is simply [a+c, b+d].</p><p>Our task is to find d-c+1 consecutive numbers in this range which are composite. If we are able to complete this task, then Takahashi wins, otherwise Aoki wins.</p><h4><strong>Brute Force Approach:</strong></h4><p>Define variables <em>start = a+c, end = b+d, gap = d-c+1</em> and <em>countComposite = 0</em>. Then iterate from <em>start </em>to <em>end</em> with a for-loop and check if each number is a prime or not.</p><p>If the number is not prime, then increment <em>countComposite</em> by 1.</p><p>If the number is prime, check whether the current value of <em>countComposite</em> is greater than or equal to <em>gap</em>. If it is greater, then print “Takahashi” and break from the for-loop.</p><p>Otherwise, reset <em>countComposite</em> to 0 and continue with the for-loop. If the for-loop ends, print “Aoki”.</p><p>To check if a number is prime, we can define a function checkPrime(int n) which takes the number to be checked as an argument. Inside the function, we can run a loop from i=2 to i=squareRoot(n) and check whether any <em>i</em> is a factor of n.</p><p><strong>Time Complexity:</strong> O((b+d-a-c)^(3/2))</p><p><strong>Space Complexity: </strong>O(1)</p><h4><strong>Optimal Approach:</strong></h4><p>Generate a sieve of prime numbers till<em> b+d </em>using the <a href="https://www.geeksforgeeks.org/sieve-of-eratosthenes/">Sieve of Eratosthenes</a> method. Create an array (say <em>nums</em>) which stores numbers of prime numbers that are less than or equal to <em>i</em> (0≤i≤201). Now we only need to check if nums[i+d] — nums[i+c-1] is equal to 0 or not for all <em>i</em> [a,b]. (We take nums[i+c-1] because we increment an element in nums[] when we find a prime number and that value remains same for rest of the composite numbers AHEAD until another prime is found)</p><p>If we find any <em>i </em>that satisfies the above condition, then we can print <em>Takahashi</em>, else we have to print <em>Aoki</em>.</p><p><strong>Time Complexity:</strong> O((b+d)*log(log(b+d))) ≈ O(b+d) {as <em>b </em>and <em>d</em> are quite small}</p><p><strong>Space Complexity:</strong> O(b+d) {to store the sieve}</p><h3><strong>Code</strong></h3><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/2b240ffd2a729124899450e49650e973/href">https://medium.com/media/2b240ffd2a729124899450e49650e973/href</a></iframe><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6258baeda671" width="1" height="1" alt=""><hr><p><a href="https://medium.com/intellectually-yours/game-theory-in-competitive-programming-part-13-6258baeda671">Game Theory in Competitive Programming Part 13</a> was originally published in <a href="https://medium.com/intellectually-yours">Intellectually Yours</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>