<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.universitas.ro/index.php?action=history&amp;feed=atom&amp;title=4171_-_Loop_Over</id>
	<title>4171 - Loop Over - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.universitas.ro/index.php?action=history&amp;feed=atom&amp;title=4171_-_Loop_Over"/>
	<link rel="alternate" type="text/html" href="https://wiki.universitas.ro/index.php?title=4171_-_Loop_Over&amp;action=history"/>
	<updated>2026-05-01T02:49:07Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.42.1</generator>
	<entry>
		<id>https://wiki.universitas.ro/index.php?title=4171_-_Loop_Over&amp;diff=10196&amp;oldid=prev</id>
		<title>RaulOtet: Pagină nouă: Baxibilian în timp ce învață la informatică a descoperit jocul &#039;&#039;&#039;Loopover&#039;&#039;&#039;. Acest joc poate fi descris prin următoarele reguli:  * Jocul se desfășoară pe o tabelă pătratică de &lt;code&gt;n x n&lt;/code&gt; celule, în care atât rândurile cât și coloanele sunt numerotate de la &lt;code&gt;1&lt;/code&gt;; * În starea inițială a tabelei, în fiecare celulă se află câte un număr de la &lt;code&gt;1&lt;/code&gt; la &lt;code&gt;n^2&lt;/code&gt;, astfel încât &lt;code&gt;M[i,j] = (i - 1) * n + j&lt;/code&gt; * As...</title>
		<link rel="alternate" type="text/html" href="https://wiki.universitas.ro/index.php?title=4171_-_Loop_Over&amp;diff=10196&amp;oldid=prev"/>
		<updated>2024-07-30T15:01:45Z</updated>

		<summary type="html">&lt;p&gt;Pagină nouă: Baxibilian în timp ce învață la informatică a descoperit jocul &amp;#039;&amp;#039;&amp;#039;Loopover&amp;#039;&amp;#039;&amp;#039;. Acest joc poate fi descris prin următoarele reguli:  * Jocul se desfășoară pe o tabelă pătratică de &amp;lt;code&amp;gt;n x n&amp;lt;/code&amp;gt; celule, în care atât rândurile cât și coloanele sunt numerotate de la &amp;lt;code&amp;gt;1&amp;lt;/code&amp;gt;; * În starea inițială a tabelei, în fiecare celulă se află câte un număr de la &amp;lt;code&amp;gt;1&amp;lt;/code&amp;gt; la &amp;lt;code&amp;gt;n^2&amp;lt;/code&amp;gt;, astfel încât &amp;lt;code&amp;gt;M[i,j] = (i - 1) * n + j&amp;lt;/code&amp;gt; * As...&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;Baxibilian în timp ce învață la informatică a descoperit jocul &amp;#039;&amp;#039;&amp;#039;Loopover&amp;#039;&amp;#039;&amp;#039;. Acest joc poate fi descris prin următoarele reguli:&lt;br /&gt;
&lt;br /&gt;
* Jocul se desfășoară pe o tabelă pătratică de &amp;lt;code&amp;gt;n x n&amp;lt;/code&amp;gt; celule, în care atât rândurile cât și coloanele sunt numerotate de la &amp;lt;code&amp;gt;1&amp;lt;/code&amp;gt;;&lt;br /&gt;
* În starea inițială a tabelei, în fiecare celulă se află câte un număr de la &amp;lt;code&amp;gt;1&amp;lt;/code&amp;gt; la &amp;lt;code&amp;gt;n^2&amp;lt;/code&amp;gt;, astfel încât &amp;lt;code&amp;gt;M[i,j] = (i - 1) * n + j&amp;lt;/code&amp;gt;&lt;br /&gt;
* Asupra tabelei se pot aplica patru tipuri de operații de oricâte ori:&lt;br /&gt;
&lt;br /&gt;
- &amp;lt;code&amp;gt;L x&amp;lt;/code&amp;gt; – Toate valorile din celulele de pe linia &amp;lt;code&amp;gt;x&amp;lt;/code&amp;gt; se vor deplasa ciclic la stânga cu o unitate, adică &amp;lt;code&amp;gt;M[x,i] = M[x,i+1]&amp;lt;/code&amp;gt; pentru &amp;lt;code&amp;gt;i &amp;lt; n&amp;lt;/code&amp;gt; și &amp;lt;code&amp;gt;M[x,n] = M[x,1]&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
- &amp;lt;code&amp;gt;R x&amp;lt;/code&amp;gt; – Toate valorile din celulele de pe linia &amp;lt;code&amp;gt;x&amp;lt;/code&amp;gt; se vor deplasa ciclic la dreapta cu o unitate, adică &amp;lt;code&amp;gt;M[x,i] = M[x,i-1]&amp;lt;/code&amp;gt; pentru &amp;lt;code&amp;gt;i &amp;gt; 1&amp;lt;/code&amp;gt; și &amp;lt;code&amp;gt;M[x,1] = M[x,n]&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
- &amp;lt;code&amp;gt;U x&amp;lt;/code&amp;gt; – Toate valorile din celulele de pe coloana &amp;lt;code&amp;gt;x&amp;lt;/code&amp;gt; se vor deplasa ciclic în sus cu o unitate, adică &amp;lt;code&amp;gt;M[i,x] = M[i+1,x]&amp;lt;/code&amp;gt; pentru &amp;lt;code&amp;gt;i &amp;lt; n&amp;lt;/code&amp;gt; și &amp;lt;code&amp;gt;M[n,x] = M[1,x]&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
- &amp;lt;code&amp;gt;D x&amp;lt;/code&amp;gt; – Toate valorile din celulele de pe coloana &amp;lt;code&amp;gt;x&amp;lt;/code&amp;gt; se vor deplasa ciclic în jos cu o unitate, adică &amp;lt;code&amp;gt;M[i,x] = M[i-1,x]&amp;lt;/code&amp;gt; pentru &amp;lt;code&amp;gt;i &amp;gt; 1&amp;lt;/code&amp;gt; și &amp;lt;code&amp;gt;M[1,x] = M[n,x]&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Cerința =&lt;br /&gt;
Cum Baxibilian nu are timp să analizeze prea mult jocul Loopover, deoarece are de învățat, el dorește să știe următoarele lucruri:&lt;br /&gt;
&lt;br /&gt;
* Fiind dată starea unei tabele asupra căreia s-au făcut fie doar operații asupra liniilor, fie doar operații asupra coloanelor, care este numărul minim de operații pe care Baxibilian ar trebui să le aplice pentru a reveni în starea inițială?&lt;br /&gt;
* Fiind dată o secvență de &amp;lt;code&amp;gt;m&amp;lt;/code&amp;gt; operații, care este numărul minim de aplicări ale acestei secvențe asupra unei tabele de dimensiune &amp;lt;code&amp;gt;n x n&amp;lt;/code&amp;gt; aflate în starea inițială astfel încât starea finală să fie aceeași ca starea inițială? Întrucât rezultatul poate fi un număr foarte mare, Baxibilian este mulțumit dacă află doar restul împărțirii acestui număr la &amp;lt;code&amp;gt;1.000.000.007&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
= Date de intrare =&lt;br /&gt;
De pe prima linie a fișierului &amp;lt;code&amp;gt;loopover.in&amp;lt;/code&amp;gt; se vor afla două numere separate printr-un spațiu &amp;lt;code&amp;gt;t&amp;lt;/code&amp;gt;  cerința care trebuie rezolvată și &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt; dimensiunea tabelei. În continuare:&lt;br /&gt;
&lt;br /&gt;
* Dacă &amp;lt;code&amp;gt;t = 1&amp;lt;/code&amp;gt;, pe următoarele &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt; linii se vor afla, câte &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt; numere separate prin câte un spațiu, reprezentând configurația tabelei.&lt;br /&gt;
* Dacă &amp;lt;code&amp;gt;t = 2&amp;lt;/code&amp;gt;, pe ce-a de-a doua linie se va afla un singur număr &amp;lt;code&amp;gt;m&amp;lt;/code&amp;gt;, iar pe următoarele &amp;lt;code&amp;gt;m&amp;lt;/code&amp;gt; linii se vor afla, separate prin câte un spațiu, un caracter &amp;lt;code&amp;gt;c&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;&amp;lt;/code&amp;gt; &amp;lt;code&amp;gt;∈ {L,R,U,D}&amp;lt;/code&amp;gt; reprezentând tipul operației și un număr &amp;lt;code&amp;gt;x&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;&amp;lt;/code&amp;gt; reprezentând indexul liniei sau coloanei asupra căreia se aplică operația &amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
= Date de ieșire =&lt;br /&gt;
În fișierul &amp;lt;code&amp;gt;loopover.out&amp;lt;/code&amp;gt; se va afișa în funcție de cerință:&lt;br /&gt;
&lt;br /&gt;
* dacă &amp;lt;code&amp;gt;t = 1&amp;lt;/code&amp;gt;, un singur număr reprezentând numărul minim de operații pentru a aduce tabela în starea inițială.&lt;br /&gt;
* dacă &amp;lt;code&amp;gt;t = 2&amp;lt;/code&amp;gt;, un singur număr reprezentând restul împărțirii la &amp;lt;code&amp;gt;1.000.000.007&amp;lt;/code&amp;gt; al numărului minim de aplicări ale secvenței de operații asupra tabelei pentru ca aceasta să ajungă înapoi în starea inițială.&lt;br /&gt;
&lt;br /&gt;
= Restricții și precizări =&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;2 ≤ n ≤ 1000&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;1 ≤ m ≤ 1000&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;t ∈ {1, 2}&amp;lt;/code&amp;gt;&lt;br /&gt;
* &amp;lt;code&amp;gt;1 ≤ x&amp;lt;sub&amp;gt;i&amp;lt;/sub&amp;gt;&amp;lt;/code&amp;gt; &amp;lt;code&amp;gt;≤ n&amp;lt;/code&amp;gt; pentru orice &amp;lt;code&amp;gt;1 ≤ i ≤ m&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Exemplul 1: =&lt;br /&gt;
&amp;lt;code&amp;gt;loopover.in&amp;lt;/code&amp;gt;&lt;br /&gt;
 1 4&lt;br /&gt;
 2 3 4 1&lt;br /&gt;
 8 5 6 7&lt;br /&gt;
 11 12 9 10&lt;br /&gt;
 13 14 15 16&lt;br /&gt;
&amp;lt;code&amp;gt;loopover.out&amp;lt;/code&amp;gt;&lt;br /&gt;
 4&lt;br /&gt;
&lt;br /&gt;
=== Explicație ===&lt;br /&gt;
Operațiile ce trebuie aplicate sunt:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;R 1&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;L 2&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;L 3&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;L 3&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Exemplul 2: =&lt;br /&gt;
&amp;lt;code&amp;gt;loopover.in&amp;lt;/code&amp;gt;&lt;br /&gt;
 1 3&lt;br /&gt;
 7 8 6&lt;br /&gt;
 1 2 9&lt;br /&gt;
 4 5 3&lt;br /&gt;
&amp;lt;code&amp;gt;loopover.out&amp;lt;/code&amp;gt;&lt;br /&gt;
 3&lt;br /&gt;
&lt;br /&gt;
=== Explicație ===&lt;br /&gt;
Operațiile ce trebuie aplicate sunt:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;U 1&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;U 2&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;D 3&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Exemplul 3: =&lt;br /&gt;
&amp;lt;code&amp;gt;loopover.in&amp;lt;/code&amp;gt;&lt;br /&gt;
 2 3&lt;br /&gt;
 5&lt;br /&gt;
 U 1&lt;br /&gt;
 R 1&lt;br /&gt;
 U 2&lt;br /&gt;
 R 1&lt;br /&gt;
 L 2&lt;br /&gt;
&amp;lt;code&amp;gt;loopover.out&amp;lt;/code&amp;gt;&lt;br /&gt;
 6&lt;br /&gt;
&lt;br /&gt;
=== Explicație ===&lt;br /&gt;
După aplicarea secvenței de &amp;lt;code&amp;gt;5&amp;lt;/code&amp;gt; operații, matricea obținută de Baxibilian este:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;2 3 5&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;8 6 7&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;code&amp;gt;1 4 9&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Numărul minim de aplicări ale secvenței pentru a ajunge la matricea identitate este &amp;lt;code&amp;gt;6&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
= Exemplul 4: =&lt;br /&gt;
&amp;lt;code&amp;gt;loopover.in&amp;lt;/code&amp;gt;&lt;br /&gt;
 2 8&lt;br /&gt;
 10&lt;br /&gt;
 R 6&lt;br /&gt;
 L 8&lt;br /&gt;
 R 4&lt;br /&gt;
 U 3&lt;br /&gt;
 L 3&lt;br /&gt;
 L 1&lt;br /&gt;
 R 3&lt;br /&gt;
 U 5&lt;br /&gt;
 U 6&lt;br /&gt;
 U 3&lt;br /&gt;
&amp;lt;code&amp;gt;loopover.out&amp;lt;/code&amp;gt;&lt;br /&gt;
 4284&lt;br /&gt;
&lt;br /&gt;
=== Explicație ===&lt;br /&gt;
Sunt necesare &amp;lt;code&amp;gt;4284&amp;lt;/code&amp;gt; de aplicări ale secvenței pentru a reveni în starea inițială.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot; line=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
MOD = 1000000007&lt;br /&gt;
&lt;br /&gt;
def gcd(a, b):&lt;br /&gt;
    while b:&lt;br /&gt;
        a, b = b, a % b&lt;br /&gt;
    return a&lt;br /&gt;
&lt;br /&gt;
def lcm(a, b):&lt;br /&gt;
    return a * b // gcd(a, b)&lt;br /&gt;
&lt;br /&gt;
def min_operations_to_initial(n, grid, is_row_operations):&lt;br /&gt;
    if is_row_operations:&lt;br /&gt;
        operations_needed = 0&lt;br /&gt;
        for row in grid:&lt;br /&gt;
            for j in range(n):&lt;br /&gt;
                if row[j] != (row[0] + j) % n:&lt;br /&gt;
                    shift_amount = (j - row.index(1)) % n&lt;br /&gt;
                    operations_needed = gcd(operations_needed, shift_amount)&lt;br /&gt;
        return operations_needed&lt;br /&gt;
    else:&lt;br /&gt;
        operations_needed = 0&lt;br /&gt;
        for j in range(n):&lt;br /&gt;
            col = [grid[i][j] for i in range(n)]&lt;br /&gt;
            for i in range(n):&lt;br /&gt;
                if col[i] != (col[0] + i * n) % (n * n):&lt;br /&gt;
                    shift_amount = (i - col.index(1)) % n&lt;br /&gt;
                    operations_needed = gcd(operations_needed, shift_amount)&lt;br /&gt;
        return operations_needed&lt;br /&gt;
&lt;br /&gt;
def min_apply_sequence(n, sequence):&lt;br /&gt;
    length_sequence = len(sequence)&lt;br /&gt;
    current_pos = list(range(n * n))&lt;br /&gt;
    for op in sequence:&lt;br /&gt;
        direction, idx = op.split()&lt;br /&gt;
        idx = int(idx) - 1&lt;br /&gt;
        if direction == &amp;#039;L&amp;#039;:&lt;br /&gt;
            row = current_pos[idx * n:(idx + 1) * n]&lt;br /&gt;
            current_pos[idx * n:(idx + 1) * n] = row[1:] + row[:1]&lt;br /&gt;
        elif direction == &amp;#039;R&amp;#039;:&lt;br /&gt;
            row = current_pos[idx * n:(idx + 1) * n]&lt;br /&gt;
            current_pos[idx * n:(idx + 1) * n] = row[-1:] + row[:-1]&lt;br /&gt;
        elif direction == &amp;#039;U&amp;#039;:&lt;br /&gt;
            col = current_pos[idx::n]&lt;br /&gt;
            current_pos[idx::n] = col[1:] + col[:1]&lt;br /&gt;
        elif direction == &amp;#039;D&amp;#039;:&lt;br /&gt;
            col = current_pos[idx::n]&lt;br /&gt;
            current_pos[idx::n] = col[-1:] + col[:-1]&lt;br /&gt;
    target_pos = list(range(n * n))&lt;br /&gt;
    if current_pos == target_pos:&lt;br /&gt;
        return 1&lt;br /&gt;
    cycle_length = 1&lt;br /&gt;
    while True:&lt;br /&gt;
        current_pos = list(range(n * n))&lt;br /&gt;
        for _ in range(cycle_length):&lt;br /&gt;
            for op in sequence:&lt;br /&gt;
                direction, idx = op.split()&lt;br /&gt;
                idx = int(idx) - 1&lt;br /&gt;
                if direction == &amp;#039;L&amp;#039;:&lt;br /&gt;
                    row = current_pos[idx * n:(idx + 1) * n]&lt;br /&gt;
                    current_pos[idx * n:(idx + 1) * n] = row[1:] + row[:1]&lt;br /&gt;
                elif direction == &amp;#039;R&amp;#039;:&lt;br /&gt;
                    row = current_pos[idx * n:(idx + 1) * n]&lt;br /&gt;
                    current_pos[idx * n:(idx + 1) * n] = row[-1:] + row[:-1]&lt;br /&gt;
                elif direction == &amp;#039;U&amp;#039;:&lt;br /&gt;
                    col = current_pos[idx::n]&lt;br /&gt;
                    current_pos[idx::n] = col[1:] + col[:1]&lt;br /&gt;
                elif direction == &amp;#039;D&amp;#039;:&lt;br /&gt;
                    col = current_pos[idx::n]&lt;br /&gt;
                    current_pos[idx::n] = col[-1:] + col[:-1]&lt;br /&gt;
        if current_pos == target_pos:&lt;br /&gt;
            return cycle_length % MOD&lt;br /&gt;
        cycle_length += 1&lt;br /&gt;
&lt;br /&gt;
# Exemplu de utilizare&lt;br /&gt;
n = 4&lt;br /&gt;
grid = [&lt;br /&gt;
    [1, 2, 3, 4],&lt;br /&gt;
    [5, 6, 7, 8],&lt;br /&gt;
    [9, 10, 11, 12],&lt;br /&gt;
    [13, 14, 15, 16]&lt;br /&gt;
]&lt;br /&gt;
sequence = [&amp;quot;L 1&amp;quot;, &amp;quot;D 2&amp;quot;, &amp;quot;R 3&amp;quot;, &amp;quot;U 4&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
print(min_operations_to_initial(n, grid, True)) # Operații pe linii&lt;br /&gt;
print(min_operations_to_initial(n, grid, False)) # Operații pe coloane&lt;br /&gt;
print(min_apply_sequence(n, sequence))&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>RaulOtet</name></author>
	</entry>
</feed>