<?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=1687_-_Omogene</id>
	<title>1687 - Omogene - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.universitas.ro/index.php?action=history&amp;feed=atom&amp;title=1687_-_Omogene"/>
	<link rel="alternate" type="text/html" href="https://wiki.universitas.ro/index.php?title=1687_-_Omogene&amp;action=history"/>
	<updated>2026-05-01T07:42:41Z</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=1687_-_Omogene&amp;diff=8738&amp;oldid=prev</id>
		<title>Vasiliu Costel Andrei: Pagină nouă: == Enunț == Se consideră o matrice cu L linii și C coloane care memorează doar valori din mulțimea {0,1,2}. O submatrice nevidă (formată din cel puțin o linie și cel puțin o coloană) a acestei matrice o numim omogenă dacă numărul valorilor de 0 este egal cu numărul de valori de 1 și egal cu numărul valorilor de 2. De exemplu, în matricea  0 1 2 0&lt;br&gt; 1 2 0 1&lt;br&gt; sunt șase submatrice omogene, acestea fiind:&lt;br&gt;  0 1 2&lt;br&gt; 1 2 0 &lt;br&gt;&lt;br&gt; 1 2 0&lt;br&gt; 2 0 1&lt;br&gt;&lt;b...</title>
		<link rel="alternate" type="text/html" href="https://wiki.universitas.ro/index.php?title=1687_-_Omogene&amp;diff=8738&amp;oldid=prev"/>
		<updated>2024-01-01T10:22:13Z</updated>

		<summary type="html">&lt;p&gt;Pagină nouă: == Enunț == Se consideră o matrice cu L linii și C coloane care memorează doar valori din mulțimea {0,1,2}. O submatrice nevidă (formată din cel puțin o linie și cel puțin o coloană) a acestei matrice o numim omogenă dacă numărul valorilor de 0 este egal cu numărul de valori de 1 și egal cu numărul valorilor de 2. De exemplu, în matricea  0 1 2 0&amp;lt;br&amp;gt; 1 2 0 1&amp;lt;br&amp;gt; sunt șase submatrice omogene, acestea fiind:&amp;lt;br&amp;gt;  0 1 2&amp;lt;br&amp;gt; 1 2 0 &amp;lt;br&amp;gt;&amp;lt;br&amp;gt; 1 2 0&amp;lt;br&amp;gt; 2 0 1&amp;lt;br&amp;gt;&amp;lt;b...&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;== Enunț ==&lt;br /&gt;
Se consideră o matrice cu L linii și C coloane care memorează doar valori din mulțimea {0,1,2}. O submatrice nevidă (formată din cel puțin o linie și cel puțin o coloană) a acestei matrice o numim omogenă dacă numărul valorilor de 0 este egal cu numărul de valori de 1 și egal cu numărul valorilor de 2. De exemplu, în matricea&lt;br /&gt;
&lt;br /&gt;
0 1 2 0&amp;lt;br&amp;gt;&lt;br /&gt;
1 2 0 1&amp;lt;br&amp;gt;&lt;br /&gt;
sunt șase submatrice omogene, acestea fiind:&amp;lt;br&amp;gt;&lt;br /&gt;
&lt;br /&gt;
0 1 2&amp;lt;br&amp;gt;&lt;br /&gt;
1 2 0 &amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
1 2 0&amp;lt;br&amp;gt;&lt;br /&gt;
2 0 1&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
0 1 2&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
1 2 0&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
1 2 0&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
2 0 1&amp;lt;br&amp;gt;&amp;lt;br&amp;gt;&lt;br /&gt;
Submatricele a treia și a patra sunt formate din prima linie a matricei inițială, iar submatricele a cincea și a șasea sunt formate din a doua linie.&lt;br /&gt;
&lt;br /&gt;
== Cerința ==&lt;br /&gt;
Să se determine câte submatrice nevide omogene există.&lt;br /&gt;
&lt;br /&gt;
== Date de intrare ==&lt;br /&gt;
Fișierul de intrare omogenein.txt conține pe prima linie numerele naturale L și C. Pe următoarele L linii se află câte C numere naturale separate prin spații reprezentând câte o linie din matrice.&lt;br /&gt;
&lt;br /&gt;
== Date de ieșire ==&lt;br /&gt;
Fișierul de ieșire omogeneout.txt va conține pe prima linie un singur număr natural reprezentând numărul submatricelor nevide omogene.&lt;br /&gt;
&lt;br /&gt;
== Restricții și precizări ==&lt;br /&gt;
* 2 &amp;amp;les; &amp;#039;&amp;#039;&amp;#039;l&amp;#039;&amp;#039;&amp;#039; &amp;amp;les; &amp;#039;&amp;#039;&amp;#039;c&amp;#039;&amp;#039;&amp;#039; &amp;amp;les; 500&lt;br /&gt;
* 4 &amp;amp;les; &amp;#039;&amp;#039;&amp;#039;l*c&amp;#039;&amp;#039;&amp;#039; &amp;amp;les; 65536&lt;br /&gt;
* Atenție, o submatrice este formată dintr-o secvență continuă de linii și coloane, deci, de exemplu, dacă se aleg dintr-o matrice liniile 1, 2 și 5, atunci acestea nu formează o submatrice.&lt;br /&gt;
* Numărul submatricelor omogene va fi mai mic decât 2*10^9&lt;br /&gt;
* Întreaga matrice poate fi submatrice omogenă&lt;br /&gt;
&lt;br /&gt;
== Exemplul 1 ==&lt;br /&gt;
; Intrare&lt;br /&gt;
; omogenein.txt&lt;br /&gt;
: 2 4&lt;br /&gt;
: 0 1 2 0&lt;br /&gt;
: 1 2 0 1&lt;br /&gt;
; Ieșire&lt;br /&gt;
: Datele de intrare corespund restricțiilor impuse&lt;br /&gt;
; omogeneout.txt&lt;br /&gt;
: 6&lt;br /&gt;
&lt;br /&gt;
=== Explicație ===&lt;br /&gt;
Cele șase submatrice au fost menționate în enunț.&lt;br /&gt;
&lt;br /&gt;
== Exemplul 2 ==&lt;br /&gt;
; Intrare&lt;br /&gt;
; omogenein.txt&lt;br /&gt;
: 3 3&lt;br /&gt;
: 0 1 2&lt;br /&gt;
: 0 2 2&lt;br /&gt;
: 0 1 1&lt;br /&gt;
; Ieșire&lt;br /&gt;
: Datele de intrare corespund restricțiilor impuse&lt;br /&gt;
; omogeneout.txt&lt;br /&gt;
: 3&lt;br /&gt;
&lt;br /&gt;
== Exemplul 3 ==&lt;br /&gt;
; Intrare&lt;br /&gt;
; omogenein.txt&lt;br /&gt;
: 1 3&lt;br /&gt;
: 0 1 2&lt;br /&gt;
: 0 2 2&lt;br /&gt;
: 0 1 1&lt;br /&gt;
; Ieșire&lt;br /&gt;
: Datele de intrare NU corespund restricțiilor impuse&lt;br /&gt;
&lt;br /&gt;
== Rezolvare ==&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot; line&amp;gt;&lt;br /&gt;
#1687 - Omogene&lt;br /&gt;
&lt;br /&gt;
def validare_date(l, c, matrice):&lt;br /&gt;
    if not (2 &amp;lt;= l &amp;lt;= c &amp;lt;= 5000):&lt;br /&gt;
        return False&lt;br /&gt;
    if not (4 &amp;lt;= l * c &amp;lt;= 65536):&lt;br /&gt;
        return False&lt;br /&gt;
    if len(matrice) != l or any(len(row) != c for row in matrice):&lt;br /&gt;
        return False&lt;br /&gt;
    return True&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
def numar_submatrici_omogene(l, c, matrice):&lt;br /&gt;
    if not validare_date(l, c, matrice):&lt;br /&gt;
        return 0&lt;br /&gt;
&lt;br /&gt;
    numar_submatrici = 0&lt;br /&gt;
&lt;br /&gt;
    for i in range(l):&lt;br /&gt;
        for j in range(c):&lt;br /&gt;
            for x in range(i, l):&lt;br /&gt;
                for y in range(j, c):&lt;br /&gt;
                    submatrice = [matrice[k][j:y + 1] for k in range(i, x + 1)]&lt;br /&gt;
                    numar_0 = sum(row.count(0) for row in submatrice)&lt;br /&gt;
                    numar_1 = sum(row.count(1) for row in submatrice)&lt;br /&gt;
                    numar_2 = sum(row.count(2) for row in submatrice)&lt;br /&gt;
&lt;br /&gt;
                    if numar_0 == numar_1 == numar_2:&lt;br /&gt;
                        numar_submatrici += 1&lt;br /&gt;
&lt;br /&gt;
    return numar_submatrici&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
with open(&amp;quot;omogenein.txt&amp;quot;, &amp;quot;r&amp;quot;) as f:&lt;br /&gt;
    l, c = map(int, f.readline().split())&lt;br /&gt;
    matrice = [list(map(int, f.readline().split())) for _ in range(l)]&lt;br /&gt;
&lt;br /&gt;
    if validare_date(l, c, matrice):&lt;br /&gt;
        print(&amp;quot;Datele de intrare corespund restricțiilor impuse&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
        rezultat = numar_submatrici_omogene(l, c, matrice)&lt;br /&gt;
&lt;br /&gt;
        with open(&amp;quot;omogeneout.txt&amp;quot;, &amp;quot;w&amp;quot;) as f:&lt;br /&gt;
            f.write(str(rezultat))&lt;br /&gt;
&lt;br /&gt;
    else:&lt;br /&gt;
        print(&amp;quot;Datele de intrare NU corespund restricțiilor impuse&amp;quot;)&lt;br /&gt;
        exit(0)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Vasiliu Costel Andrei</name></author>
	</entry>
</feed>