CSES - Datatähti 2023 alku - Results
Submission details
Task:Ruudukko
Sender:okkokko
Submission time:2022-11-04 18:25:04 +0200
Language:PyPy3
Status:READY
Result:61
Feedback
groupverdictscore
#1ACCEPTED28
#2ACCEPTED33
#30
Test results
testverdicttimegroup
#1ACCEPTED0.04 s1, 2, 3details
#2ACCEPTED0.04 s1, 2, 3details
#3ACCEPTED0.04 s1, 2, 3details
#4ACCEPTED0.06 s2, 3details
#5ACCEPTED0.09 s2, 3details
#6ACCEPTED0.10 s2, 3details
#7ACCEPTED0.21 s3details
#8--3details
#9--3details

Code

# completes tests 1 and 2, but not 3
import random
import time
import sys


def I():
    return map(int, input().split())


DIVISOR = 10**9 + 7


def test():
    n = 400

    def text_gen():
        yield str(n)
        random.seed(1001)
        for i in range(n):
            yield " ".join((str(random.randint(1, n * n)) for _ in range(n)))

    return text_gen().__next__


# input = test()

count_row_skip_1 = 0
count_col_skip_1 = 0


def main():

    timer_0 = time.perf_counter()
    n, = I()
    grid = [list(I()) for _ in range(n)]
    total = 0

    timer_1 = time.perf_counter()
    line_values_row = [0] * n
    line_values_row_last_used_high = [1] * n
    line_values_col = [0] * n
    line_values_col_last_used_high = [1] * n
    line_values_row_added = [0] * n
    line_values_col_added = [0] * n

    exists_row = [{grid[y][x] for x in range(n)} for y in range(n)]
    exists_col = [{grid[y][x] for y in range(n)} for x in range(n)]
    exists_all = set().union(*exists_col)
    # print("exists_all size:", len(exists_all))

    def get_line_values_row(y: int, v: int):
        """tells how many routes can start from a tile of value v and first go along row y \n
        sum of the route values of tiles less than v on row y"""
        if v == line_values_row_last_used_high[y]:  # false <==> first time with this y and v
            global count_row_skip_1
            count_row_skip_1 += 1
            return line_values_row[y]

        line_values_row_last_used_high[y] = v

        s = line_values_row[y] + line_values_row_added[y]
        line_values_row_added[y] = 0
        line_values_row[y] = s

        return s

    def get_line_values_col(x: int, v: int):
        if v == line_values_col_last_used_high[x]:
            global count_col_skip_1
            count_col_skip_1 += 1
            return line_values_col[x]

        line_values_col_last_used_high[x] = v

        s = line_values_col[x] + line_values_col_added[x]
        line_values_col_added[x] = 0
        line_values_col[x] = s

        return s

    def calculate_routes(y: int, x: int, number: int):
        if number == 1:
            value = 1
        else:
            value = (get_line_values_col(x, number) + get_line_values_row(y, number) + 1) % DIVISOR
        nonlocal total
        total += value
        line_values_row_added[y] += value
        line_values_col_added[x] += value

    timer_2 = time.perf_counter()
    for a in range(1, n * n + 1):
        if a not in exists_all:
            continue
        for y in range(n):
            if a not in exists_row[y]:
                continue
            x = -1
            try:
                while True:
                    x = grid[y].index(a, x + 1)
                    calculate_routes(y, x, a)
            except ValueError:
                pass
    timer_3 = time.perf_counter()
    print("timer 1:", timer_1 - timer_0, "\ntimer 2:", timer_2 - timer_1, "\ntimer 3:", timer_3 - timer_2, file=sys.stderr)

    def summod(x):
        return sum(y % DIVISOR for y in x) % DIVISOR

    # w = summod(map(summod, route_grid))
    # assert w == total % DIVISOR
    # return w
    return total % DIVISOR


timer_start = time.perf_counter()
w = main()
print(w)
print("time:", time.perf_counter() - timer_start,
      "\nsame" if w == 452330452 else "\ndifferent",
      "\nrow skips:", count_row_skip_1,
      "\ncol skips:", count_col_skip_1, file=sys.stderr)

Test details

Test 1

Group: 1, 2, 3

Verdict: ACCEPTED

input
3
1 1 1
1 1 1
1 1 1

correct output
9

user output
9

Error:
timer 1: 0.00016634899657219648 
timer 2: 4.161300603300333e-05 
timer 3: 3.28779569827020...

Test 2

Group: 1, 2, 3

Verdict: ACCEPTED

input
3
1 2 3
6 5 4
7 8 9

correct output
135

user output
135

Error:
timer 1: 0.00017005996778607368 
timer 2: 4.250003257766366e-05 
timer 3: 7.75759690441191...

Test 3

Group: 1, 2, 3

Verdict: ACCEPTED

input
3
7 8 1
4 5 4
3 9 6

correct output
57

user output
57

Error:
timer 1: 0.0001710680080577731 
timer 2: 4.161801189184189e-05 
timer 3: 7.388502126559615...

Test 4

Group: 2, 3

Verdict: ACCEPTED

input
100
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

correct output
10000

user output
10000

Error:
timer 1: 0.006499365030322224 
timer 2: 0.004131437977775931 
timer 3: 0.00638794299447909...

Test 5

Group: 2, 3

Verdict: ACCEPTED

input
100
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

correct output
187458477

user output
187458477

Error:
timer 1: 0.006669589958619326 
timer 2: 0.006027174007613212 
timer 3: 0.03092472499702125...

Test 6

Group: 2, 3

Verdict: ACCEPTED

input
100
2995 8734 1018 2513 7971 5063 ...

correct output
964692694

user output
964692694

Error:
timer 1: 0.0068387609790079296 
timer 2: 0.006050412019249052 
timer 3: 0.0425964570022188...

Test 7

Group: 3

Verdict: ACCEPTED

input
1000
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 ...

correct output
1000000

user output
1000000

Error:
timer 1: 0.08496811200166121 
timer 2: 0.04955322598107159 
timer 3: 0.035033484979067
tim...

Test 8

Group: 3

Verdict:

input
1000
1 2 3 4 5 6 7 8 9 10 11 12 13 ...

correct output
229147081

user output
(empty)

Test 9

Group: 3

Verdict:

input
1000
520283 805991 492643 75254 527...

correct output
951147313

user output
(empty)