[Python] Long table eel shop

1. Problem

The problem of finding how many people can sit on a circular table (n number of tables). If even one person in a group cannot sit, the group will return.

The input consists of m + 1 lines. In the first line, n (number of seats) and m (number of groups) are entered separated by half-width spaces. In the i + 1 line (1 ≤ i ≤ m), two integers a_i (number of people in the group) and b_i (seating start seat number) are entered separated by a half-width space.

conditions In all test cases, the values entered meet the following conditions: 1≦n≦100 1≦m≦100 1≦a_i≦n 1≦b_i≦n

951A057A-C7C6-4595-ADBF-2CBC7D6FC8E0.jpeg

2. How to think

(1) Table number

When there are k people in a group i and they sit from the b_i table This group

b_i,...,b_(i+k-1)

It is supposed to sit at the table up to.

For example, if the number of tables is 6 and (a_i, b_i) = (4,5) above Note that the table number you plan to sit on will be (5,6,1,2) instead of (5,6,7,8). Here, I decided to think that all table numbers are __b_j% n (the remainder of b_j divided by n) __.

(2) Whether a group sits or not: Determined by the check_arr function

Seated table array: ar_table An array of table numbers that a group is supposed to sit on (Created with make_tblarr function): ar_chk If ar_table contains even one element of ar_chk, it is judged that it cannot sit.

(3) Display of the number of people sitting

The group determined to be able to sit in the above adds those table numbers to the seated table array. Finally, the size of the seating table array is displayed.

3. Code example

Operation has been confirmed.


# coding: utf-8
# Your code here!
in1=input()
arr1=in1.split()
n1=int(arr1[0])
n2=int(arr1[1])

in2=[]
for i in range(n2):
    tmp=input()
    in2.append(tmp)
#print(in2)


def make_tblarr(in2,nmax):
    arr3=[]
    arr2=in2.split()
    nn=int(arr2[0])
    st=int(arr2[1])

    for i in range(nn):
        arr3.append((st+i)%nmax)

    return arr3


#ar_table:Array of sitting table numbers
#ar_chk:Array to check
#ar_ar on table_0 when all elements of chk are not included, 1 when included elements are found
def check_arr(ar_table,ar_chk):
    flg=0
    for i in range(len(ar_chk)):
        #The array element to be checked is already ar_If included in table
        if ar_table.count(ar_chk[i])>0:
            #set flg to 1
            flg=1
            break
    return flg


retar=[]
for i in range(n2):
    ar3=make_tblarr(in2[i],n1)
#print(ar3)
    if check_arr(retar,ar3)== 0:
        for j in range(len(ar3)):
            retar.append(ar3[j])

print(len(retar))

Recommended Posts

[Python] Long table eel shop
Django Python shift table
Wrap long expressions in python
Anaconda and Python version correspondence table
[Python] [Table of Contents Links] Python Programming
python datetime format quick reference table
Python Math Series ⓪ Table of Contents
Introductory table of contents for python3
[Python] Multiplication table using for statement