Magical Candy Store SOLUTION CNDYGAME

Magical Candy Store November Challenge 2020

Chef and Chefu are at a magical candy store playing a game with the following rules:
There are two candy counters; each of them stores an infinite number of candies. At any time, only one of the counters is open and the other is closed.
Exactly one player is present at each of the counters. Initially, Chef is at the open counter and Chefu is at the closed counter.
There is a sequence of N distinct integers A1,A2,…,AN. The game consists of R turns; in the i-th turn, the open counter offers only C=A(i−1)%N+1 candies to the player present at this counter. This player should choose a positive number of candies M to accept, where 1≤M≤C.
If this player accepts an odd number of candies, the players have to swap their positions (each player goes to the other counter).
After each N turns, the counter which was currently open is closed and the counter which was currently closed is opened.
The primary goal of each player is to maximise his own number of candies after R turns. As a second priority, each player wants to minimise the number of candies his opponent has after R turns.
You should process Q queries. In each query, you are given R and you should find the number of candies Chef has after R turns, assuming that both players play the game optimally. Since this number could be very large, compute it modulo 109+7.
Input
The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.
The first line of each test case contains a single integer N.
The second line contains N space-separated integers A1,A2,…,AN.
The third line contains a single integer Q.
Each of the next Q lines contains a single integer R describing a query.
Output
For each query, print a single line containing one integer ― the maximum number of candies Chef can get, modulo 109+7.
Constraints
1≤T≤25
1≤N≤105
1≤Ai≤109 for each valid i
A1,A2,…,AN are pairwise distinct
1≤Q≤105
1≤R≤1012
the sum of N+Q over all test cases does not exceed 106
Subtasks
Subtask #1 (15 points):
N≤10
Q≤35
R≤35
Subtask #2 (85 points): original constraints
Example Input
1
4
4 10 2 1
2
4
5
Example Output
17
21
Explanation
Example case 1: In the 1-st, 2-nd and 3-rd turn, Chef takes 4, 10 and 2 candies (16 in total) respectively. In the 4-th turn, Chef takes 1 candy (17 in total; this is the answer to the first query), which is odd and hence he has to go to the counter which is closed. However, since N=4 turns are just completed, the counter which was currently open closes and the other one (where Chef went) opens. In the 5-th round, Chef can take 4 candies, so he has 21 candies.
 
Solution:
for  _ in range(int(input())):
    n=int(input())
    l=list(map(int,input().split()))
    s=0
    q=int(input())
    k=10**9+7
    if n==1:
        if l[0]%2==0:
            for i in range(q):
                r=int(input())
                print(((r//n-1)*(l[0]-1)+l[0])%k)
        else:
              for i in range(q):
                r=int(input())
                print(((r//n)*l[0])%k)
    elif 1 not in l or l[-1]==1:
        dp=[0]+[i if i%2==0 else i-1 for i in l[:-1:]]+[l[-1]-1 if l[-1]%2==0 else l[-1]]
        dp2=[]
        for i in dp:
            s=s+i
            dp2.append(s)
        dp=[]
        for i in range(q):
            r=int(input())
            if r%n==0:
                if l[-1]%2==1:
                  print((dp2[r%n]+r//n*s)%k)
                else:
                    print((r//n*s+1)%k)
            else:
                print((dp2[r%n]+r//n*s+l[r%n-1]%2)%k)
    elif l[0]==1:
      
        for i in range(q):
            r=int(input())
            t=0
            if r!=1 and r%n==1:
                print(((r+n-1)//n-1)%k)
            else:
                print(((r+n-1)//n)%k)
    else:
        x=l.index(1)
        dp=[0]
        s=0
        for i in range(n-1):
            if x-1==i:
                if l[i]%2==0:
                    dp.append(l[i]-1)
                else:
                    dp.append(l[i])
            else:
                if l[i]%2==0:
                    dp.append(l[i])
                else:
                    dp.append(l[i]-1)
        if l[-1]%2==0:
                dp.append(l[-1]-1)
        else:
                dp.append(l[-1])
        dp2=[]
       
        for i in dp:
            s=s+i
            dp2.append(s)
        dp=[]
    
        for i in range(q):
            r=int(input())
            if r%n==0:
                if l[-1]%2==1:
                  print((dp2[r%n]+r//n*s)%k)
                else:
                    print((dp2[r%n]+r//n*s+1)%k)
            else:
                if l[r%n-1]==1:
                    t=0
                    if l[x-1]%2==0:
                       t=2
                    print((dp2[r%n]+r//n*s+t)%k)
                elif l[r%n]==1:
                    t=0
                    if l[x-1]%2==0:
                        t=1
                    print((dp2[r%n]+r//n*s+t)%k)
                else:  
                  print((dp2[r%n]+r//n*s+l[r%n-1]%2)%k)
            

November Challenge 2020 SOLUTION CodeChef

October Lunchtime 2020 CodeChef SOLUTIONS

RELATED :

Related :

Related :

2 thoughts on “Magical Candy Store SOLUTION CNDYGAME”

Leave a Comment