<?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=1228_-_SSK</id>
	<title>1228 - SSK - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.universitas.ro/index.php?action=history&amp;feed=atom&amp;title=1228_-_SSK"/>
	<link rel="alternate" type="text/html" href="https://wiki.universitas.ro/index.php?title=1228_-_SSK&amp;action=history"/>
	<updated>2026-06-17T07:12:31Z</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=1228_-_SSK&amp;diff=10048&amp;oldid=prev</id>
		<title>AjM: Pagină nouă: == Enunt == Manole a învățat de la profesorul de informatică cum să calculeze suma elementelor oricărei matrice A cu N linii și M coloane. El numerotează liniile de la 1 la N și coloanele de la 1 la M. Mai mult, Manole fiind extrem de pasionat de numere, va calcula sumele tuturor subtablourilor din cadrul matricei A. Șirul acestor sume îl scrie pe o hârtie, după ce l-a ordonat crescător.  Prin subtablou el înțelege o zonă dreptunghiulară din matricea A, iden...</title>
		<link rel="alternate" type="text/html" href="https://wiki.universitas.ro/index.php?title=1228_-_SSK&amp;diff=10048&amp;oldid=prev"/>
		<updated>2024-06-03T17:35:50Z</updated>

		<summary type="html">&lt;p&gt;Pagină nouă: == Enunt == Manole a învățat de la profesorul de informatică cum să calculeze suma elementelor oricărei matrice A cu N linii și M coloane. El numerotează liniile de la 1 la N și coloanele de la 1 la M. Mai mult, Manole fiind extrem de pasionat de numere, va calcula sumele tuturor subtablourilor din cadrul matricei A. Șirul acestor sume îl scrie pe o hârtie, după ce l-a ordonat crescător.  Prin subtablou el înțelege o zonă dreptunghiulară din matricea A, iden...&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== Enunt ==&lt;br /&gt;
Manole a învățat de la profesorul de informatică cum să calculeze suma elementelor oricărei matrice A cu N linii și M coloane. El numerotează liniile de la 1 la N și coloanele de la 1 la M. Mai mult, Manole fiind extrem de pasionat de numere, va calcula sumele tuturor subtablourilor din cadrul matricei A. Șirul acestor sume îl scrie pe o hârtie, după ce l-a ordonat crescător.&lt;br /&gt;
&lt;br /&gt;
Prin subtablou el înțelege o zonă dreptunghiulară din matricea A, identificată prin colțul stânga-sus (x1,y1) şi colţul dreapta-jos (x2,y2), elementele subtabloului fiind toate elementele A[i][j] pentru care x1≤i≤x2 şi y1≤j≤y2. Suma unui subtablou este suma tuturor elementelor sale.&lt;br /&gt;
== Cerinţa ==&lt;br /&gt;
Scrieţi un program care, cunoscând valorile elementelor matricei A, determină al K-lea termen din șirul ordonat al sumelor tuturor subtablourilor matricei A.&lt;br /&gt;
== Date de intrare ==&lt;br /&gt;
Fișierul de intrare ssk.in conţine pe prima linie numerele naturale N M K separate prin câte un spațiu, având semnificația din enunț. Pe următoarele N linii se află câte M numere naturale separate prin spaţii, reprezentând elementele matricei A.&lt;br /&gt;
== Date de ieșire ==&lt;br /&gt;
Fişierul de ieşire ssk.out va conține o singură linie pe care va fi scris un număr natural reprezentând răspunsul la cerinţă.&lt;br /&gt;
== Restricţii şi precizări ==&lt;br /&gt;
* 1 ≤ N ≤ 150&lt;br /&gt;
* 1 ≤ M ≤ 150&lt;br /&gt;
* 1 ≤ K ≤ numărul de termeni din şirul ordonat&lt;br /&gt;
* 0 ≤ A[i][j] ≤ 1000 unde 1 ≤ i ≤ N și 1 ≤ j ≤ M&lt;br /&gt;
Numerotarea termenilor din şirul ordonat al sumelor tuturor subtablourilor se va face începând de la 1.&lt;br /&gt;
== Exemplu ==&lt;br /&gt;
; ssk.in&lt;br /&gt;
 2 3 14&lt;br /&gt;
 3 2 7 &lt;br /&gt;
 4 1 0 &lt;br /&gt;
; ssk.out&lt;br /&gt;
 9&lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Explicație ==&lt;br /&gt;
Șirul ordonat al tuturor sumelor subtablourilor matricei este&lt;br /&gt;
&lt;br /&gt;
0 1 1 2 3 3 4 5 5 5 7 7 7 9 10 10 12 17&lt;br /&gt;
&lt;br /&gt;
A patrusprezecea sumă este 9.&lt;br /&gt;
== Rezolvare ==&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot; line&amp;gt;&lt;br /&gt;
def calculate_subtable_sums(matrix):&lt;br /&gt;
    n = len(matrix)&lt;br /&gt;
    m = len(matrix[0])&lt;br /&gt;
&lt;br /&gt;
    # Initialize the cumulative sum matrix&lt;br /&gt;
    cumulative_sum = [[0] * (m + 1) for _ in range(n + 1)]&lt;br /&gt;
&lt;br /&gt;
    # Calculate cumulative sum matrix&lt;br /&gt;
    for i in range(1, n + 1):&lt;br /&gt;
        for j in range(1, m + 1):&lt;br /&gt;
            cumulative_sum[i][j] = matrix[i - 1][j - 1] + cumulative_sum[i - 1][j] + cumulative_sum[i][j - 1] - cumulative_sum[i - 1][j - 1]&lt;br /&gt;
&lt;br /&gt;
    # Calculate the sum of each subtable and store it in a list&lt;br /&gt;
    subtable_sums = []&lt;br /&gt;
    for i1 in range(1, n + 1):&lt;br /&gt;
        for j1 in range(1, m + 1):&lt;br /&gt;
            for i2 in range(i1, n + 1):&lt;br /&gt;
                for j2 in range(j1, m + 1):&lt;br /&gt;
                    subtable_sum = cumulative_sum[i2][j2] - cumulative_sum[i2][j1 - 1] - cumulative_sum[i1 - 1][j2] + cumulative_sum[i1 - 1][j1 - 1]&lt;br /&gt;
                    subtable_sums.append(subtable_sum)&lt;br /&gt;
&lt;br /&gt;
    # Sort the list of subtable sums&lt;br /&gt;
    subtable_sums.sort()&lt;br /&gt;
    &lt;br /&gt;
    return subtable_sums&lt;br /&gt;
&lt;br /&gt;
def main():&lt;br /&gt;
    with open(&amp;quot;ssk.in&amp;quot;, &amp;quot;r&amp;quot;) as fin:&lt;br /&gt;
        N, M, K = map(int, fin.readline().split())&lt;br /&gt;
        matrix = [list(map(int, fin.readline().split())) for _ in range(N)]&lt;br /&gt;
&lt;br /&gt;
    # Calculate subtable sums&lt;br /&gt;
    subtable_sums = calculate_subtable_sums(matrix)&lt;br /&gt;
&lt;br /&gt;
    # Retrieve the K-th term&lt;br /&gt;
    kth_term = subtable_sums[K - 1]&lt;br /&gt;
&lt;br /&gt;
    # Write the result to the output file&lt;br /&gt;
    with open(&amp;quot;ssk.out&amp;quot;, &amp;quot;w&amp;quot;) as fout:&lt;br /&gt;
        fout.write(str(kth_term) + &amp;quot;\n&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    main()&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>AjM</name></author>
	</entry>
</feed>