Guest Student (Codeforces 1089G)
Tags: Math, math

Berland State University invites people from all over the world as guest students. You can come to the capital of Berland and study with the best teachers in the country.Berland State University works every day of the week, but classes for guest students are held on the following schedule. You know the sequence of seven integers $$$a_1, a_2, \dots, a_7$$$ ($$$a_i = 0$$$ or $$$a_i = 1$$$):  $$$a_1=1$$$ if and only if there are classes for guest students on Sundays;  $$$a_2=1$$$ if and only if there are classes for guest students on Mondays;  ...   $$$a_7=1$$$ if and only if there are classes for guest students on Saturdays. The classes for guest students are held in at least one day of a week.You want to visit the capital of Berland and spend the minimum number of days in it to study $$$k$$$ days as a guest student in Berland State University. Write a program to find the length of the shortest continuous period of days to stay in the capital to study exactly $$$k$$$ days as a guest student.

Input: The first line of the input contains integer $$$t$$$ ($$$1 \le t \le 10\,000$$$) — the number of test cases to process. For each test case independently solve the problem and print the answer. Each test case consists of two lines. The first of them contains integer $$$k$$$ ($$$1 \le k \le 10^8$$$) — the required number of days to study as a guest student. The second line contains exactly seven integers $$$a_1, a_2, \dots, a_7$$$ ($$$a_i = 0$$$ or $$$a_i = 1$$$) where $$$a_i=1$$$ if and only if classes for guest students are held on the $$$i$$$-th day of a week.

Output: Print $$$t$$$ lines, the $$$i$$$-th line should contain the answer for the $$$i$$$-th test case — the length of the shortest continuous period of days you need to stay to study exactly $$$k$$$ days as a guest student.

Note: In the first test case you must arrive to the capital of Berland on Monday, have classes on this day, spend a week until next Monday and have classes on the next Monday. In total you need to spend $$$8$$$ days in the capital of Berland.

Sample Inputs:
3
2
0 1 0 0 0 0 0
100000000
1 0 0 0 1 0 1
1
1 0 0 0 0 0 0

Sample Outputs:
8
233333332
1

Solution (Python):
import sys
input = sys.stdin.readline
for _ in range(int(input())):
    k = int(input())
    w = list(map(int, input().split()))
    x = sum(w)
    a = k // x - (k % x == 0)
    b = k % x
    if b == 0:
        b = x
    q = 8
    for i in range(7):
        c = 0
        t = 0
        j = i
        while c < b:
            c += w[j]
            j += 1
            t += 1
            if j == 7:
                j -= 7
        q = min(t, q)
    print(a * 7 + q)

Solution (Python):
from sys import stdin
for _ in range(int(stdin.readline())):
    K = int(stdin.readline())
    lst = [*map(int, stdin.readline().split())]
    (ans, item) = ([], 0)
    for (i, x) in enumerate(lst):
        if x == 1:
            ans.append(i)
            item += 1
    res = 100000000000000000000000
    for (i, x) in enumerate(ans):
        k = K
        elem = 0
        if k <= item - i:
            elem = ans[i + k - 1] - x + 1
        else:
            elem += 7 - x
            k -= item - i
            elem += k // item * 7
            k = k % item
            if k == 0:
                elem -= 7 - ans[-1] - 1
            else:
                elem += ans[k - 1] + 1
        res = min(res, elem)
    print(res)

Solution (Python):
from math import ceil

def main():
    k = int(input())
    d = [idx for (idx, di) in enumerate(input().split()) if di == '1']
    dow = len(d)
    w = ceil(k / dow) - 1
    m = k % dow
    ans = [d[(m - 1 + x) % dow] + 7 * (w + int(m - 1 + x >= (dow if m else 0))) - d[x] + 1 for x in range(dow)]
    print(min(ans))
for _ in range(int(input())):
    main()