<?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=2048_-_mixperm</id>
	<title>2048 - mixperm - Revision history</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.universitas.ro/index.php?action=history&amp;feed=atom&amp;title=2048_-_mixperm"/>
	<link rel="alternate" type="text/html" href="https://wiki.universitas.ro/index.php?title=2048_-_mixperm&amp;action=history"/>
	<updated>2026-06-17T07:47:40Z</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=2048_-_mixperm&amp;diff=9563&amp;oldid=prev</id>
		<title>Raul: Pagină nouă: Se consideră două șiruri de numere naturale, ambele de lungime &lt;code&gt;n&lt;/code&gt;, &lt;code&gt;a=(a[1],a[2],...,a[n])&lt;/code&gt; și &lt;code&gt;b=(b[1],b[2],...,b[n])&lt;/code&gt;. Se știe că elementele din cele două șiruri sunt numere naturale, nu neapărat distincte, din mulțimea &lt;code&gt;{1,2,…,n}&lt;/code&gt;. Cu cele două șiruri se poate face următoarea operație: se aleg doi indici &lt;code&gt;i&lt;/code&gt; și &lt;code&gt;j&lt;/code&gt;, cu &lt;code&gt;1≤i≤j≤n&lt;/code&gt;, apoi prin interschimbarea secvențelor &lt;code...</title>
		<link rel="alternate" type="text/html" href="https://wiki.universitas.ro/index.php?title=2048_-_mixperm&amp;diff=9563&amp;oldid=prev"/>
		<updated>2024-01-31T15:59:06Z</updated>

		<summary type="html">&lt;p&gt;Pagină nouă: Se consideră două șiruri de numere naturale, ambele de lungime &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;a=(a[1],a[2],...,a[n])&amp;lt;/code&amp;gt; și &amp;lt;code&amp;gt;b=(b[1],b[2],...,b[n])&amp;lt;/code&amp;gt;. Se știe că elementele din cele două șiruri sunt numere naturale, nu neapărat distincte, din mulțimea &amp;lt;code&amp;gt;{1,2,…,n}&amp;lt;/code&amp;gt;. Cu cele două șiruri se poate face următoarea operație: se aleg doi indici &amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt; și &amp;lt;code&amp;gt;j&amp;lt;/code&amp;gt;, cu &amp;lt;code&amp;gt;1≤i≤j≤n&amp;lt;/code&amp;gt;, apoi prin interschimbarea secvențelor &amp;lt;code...&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;Se consideră două șiruri de numere naturale, ambele de lungime &amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;a=(a[1],a[2],...,a[n])&amp;lt;/code&amp;gt; și &amp;lt;code&amp;gt;b=(b[1],b[2],...,b[n])&amp;lt;/code&amp;gt;. Se știe că elementele din cele două șiruri sunt numere naturale, nu neapărat distincte, din mulțimea &amp;lt;code&amp;gt;{1,2,…,n}&amp;lt;/code&amp;gt;. Cu cele două șiruri se poate face următoarea operație: se aleg doi indici &amp;lt;code&amp;gt;i&amp;lt;/code&amp;gt; și &amp;lt;code&amp;gt;j&amp;lt;/code&amp;gt;, cu &amp;lt;code&amp;gt;1≤i≤j≤n&amp;lt;/code&amp;gt;, apoi prin interschimbarea secvențelor &amp;lt;code&amp;gt;a[i],a[i+1],...,a[j]&amp;lt;/code&amp;gt; cu &amp;lt;code&amp;gt;b[i],b[i+1],...,b[j]&amp;lt;/code&amp;gt; se obțin șirurile:&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;a[1], a[2], ...,a[i-1], b[i],b[i+1],…, b[j], a[j+1],a[j+2], …, a[n]&amp;lt;/code&amp;gt;  și&lt;br /&gt;
* &amp;lt;code&amp;gt;b[1], b[2], ...,b[i-1], a[i], a[i+1],…, a[j], b[j+1],b[j+2], …, b[n]&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Dacă măcar unul din șirurile obținute este permutare a mulțimii &amp;lt;code&amp;gt;{1,2,...,n}&amp;lt;/code&amp;gt;, atunci spunem că s-a obținut un &amp;#039;&amp;#039;mixperm&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
= Cerința =&lt;br /&gt;
Să se determine în câte moduri se poate obține un &amp;#039;&amp;#039;mixperm&amp;#039;&amp;#039;.&lt;br /&gt;
&lt;br /&gt;
= Date de intrare =&lt;br /&gt;
Fișierul de intrare &amp;lt;code&amp;gt;mixperm.in&amp;lt;/code&amp;gt; conţine pe prima linie numărul natural n, pe linia a doua, separate prin câte un spațiu, numerele &amp;lt;code&amp;gt;a[1]&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;a[2]&amp;lt;/code&amp;gt;, …, &amp;lt;code&amp;gt;a[n]&amp;lt;/code&amp;gt;, iar pe linia a treia, separate prin câte un spațiu, numerele &amp;lt;code&amp;gt;b[1]&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;b[2]&amp;lt;/code&amp;gt;, …, &amp;lt;code&amp;gt;b[n]&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
= Date de ieșire =&lt;br /&gt;
Fișierul de ieșire &amp;lt;code&amp;gt;mixperm.out&amp;lt;/code&amp;gt; va conţine un singur număr natural reprezentând numărul de posibilități de a se obține un mixperm.&lt;br /&gt;
&lt;br /&gt;
= Restricții și precizări =&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;1 ≤ n ≤ 10.000&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
= Exemplu: =&lt;br /&gt;
&amp;lt;code&amp;gt;mixperm.in&amp;lt;/code&amp;gt;&lt;br /&gt;
 6&lt;br /&gt;
 3 2 1 4 4 5&lt;br /&gt;
 2 3 3 4 6 5&lt;br /&gt;
&amp;lt;code&amp;gt;mixperm.out&amp;lt;/code&amp;gt;&lt;br /&gt;
 8&lt;br /&gt;
&lt;br /&gt;
=== Explicație ===&lt;br /&gt;
Se pot interschimba secvențele care au ca poziții de început și sfârșit &amp;lt;code&amp;gt;(1,3) (1,4) (3,3) (3,4) (4,6) (5,6) (4,5) (5,5)&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
De exemplu, la interschimbarea secvenţei date de intervalul &amp;lt;code&amp;gt;(3,4)&amp;lt;/code&amp;gt; se obţin şirurile &amp;lt;code&amp;gt;(3,2,3,4,4,5)&amp;lt;/code&amp;gt; şi &amp;lt;code&amp;gt;(2,3,1,4,6,5)&amp;lt;/code&amp;gt;. Al doilea şir este o permutare.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== Încărcare soluție ==&lt;br /&gt;
&lt;br /&gt;
=== Lipește codul aici ===&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;python&amp;quot; line=&amp;quot;1&amp;quot;&amp;gt;&lt;br /&gt;
import sys&lt;br /&gt;
&lt;br /&gt;
nmax = 10005&lt;br /&gt;
inFile = &amp;quot;mixperm.in&amp;quot;&lt;br /&gt;
outFile = &amp;quot;mixperm.out&amp;quot;&lt;br /&gt;
&lt;br /&gt;
a = [0] * nmax&lt;br /&gt;
b = [0] * nmax&lt;br /&gt;
sta = [0] * nmax&lt;br /&gt;
dra = [0] * nmax&lt;br /&gt;
stb = [0] * nmax&lt;br /&gt;
drb = [0] * nmax&lt;br /&gt;
suma_st = [0] * nmax&lt;br /&gt;
sumb_st = [0] * nmax&lt;br /&gt;
suma_dr = [0] * nmax&lt;br /&gt;
sumb_dr = [0] * nmax&lt;br /&gt;
app_st = [0] * nmax&lt;br /&gt;
bpp_st = [0] * nmax&lt;br /&gt;
app_dr = [0] * nmax&lt;br /&gt;
bpp_dr = [0] * nmax&lt;br /&gt;
&lt;br /&gt;
S = 0&lt;br /&gt;
sTotal = 0&lt;br /&gt;
sPP = 0&lt;br /&gt;
&lt;br /&gt;
def Citire():&lt;br /&gt;
    fin = open(inFile, &amp;#039;r&amp;#039;)&lt;br /&gt;
    global n&lt;br /&gt;
    n = int(fin.readline())&lt;br /&gt;
    a[1:n+1] = map(int, fin.readline().split())&lt;br /&gt;
    b[1:n+1] = map(int, fin.readline().split())&lt;br /&gt;
    fin.close()&lt;br /&gt;
&lt;br /&gt;
def Precalculare():&lt;br /&gt;
    global S, sTotal, sPP&lt;br /&gt;
    S = 0&lt;br /&gt;
    sTotal = n * (n + 1) // 2&lt;br /&gt;
    sPP = 1 * n * (n + 1) * (2 * n + 1) // 6&lt;br /&gt;
    for i in range(1, n+1):&lt;br /&gt;
        S = S ^ i&lt;br /&gt;
        sta[i] = sta[i - 1] ^ a[i]&lt;br /&gt;
        stb[i] = stb[i - 1] ^ b[i]&lt;br /&gt;
        suma_st[i] = suma_st[i - 1] + a[i]&lt;br /&gt;
        sumb_st[i] = sumb_st[i - 1] + b[i]&lt;br /&gt;
        app_st[i] = app_st[i - 1] + a[i] * a[i]&lt;br /&gt;
        bpp_st[i] = bpp_st[i - 1] + b[i] * b[i]&lt;br /&gt;
    for i in range(n, 0, -1):&lt;br /&gt;
        dra[i] = dra[i + 1] ^ a[i]&lt;br /&gt;
        drb[i] = drb[i + 1] ^ b[i]&lt;br /&gt;
        suma_dr[i] = suma_dr[i + 1] + a[i]&lt;br /&gt;
        sumb_dr[i] = sumb_dr[i + 1] + b[i]&lt;br /&gt;
        app_dr[i] = app_dr[i + 1] + a[i] * a[i]&lt;br /&gt;
        bpp_dr[i] = bpp_dr[i + 1] + b[i] * b[i]&lt;br /&gt;
&lt;br /&gt;
def Valid(j, i, aSum, bSum):&lt;br /&gt;
    if ((S ^ sta[j - 1] ^ bSum ^ dra[i + 1]) == 0 and sTotal == suma_st[j-1] + suma_dr[i+1] + (sumb_st[i] - sumb_st[j - 1]) and sPP == app_st[j-1] + app_dr[i+1] + (bpp_st[i] - bpp_st[j - 1])):&lt;br /&gt;
        return True&lt;br /&gt;
    if ((S ^ stb[j - 1] ^ aSum ^ drb[i + 1]) == 0 and sTotal == sumb_st[j-1] + sumb_dr[i+1] + (suma_st[i] - suma_st[j - 1]) and sPP == bpp_st[j-1] + bpp_dr[i+1] + (app_st[i] - app_st[j - 1])):&lt;br /&gt;
        return True&lt;br /&gt;
    return False&lt;br /&gt;
&lt;br /&gt;
def Rezolvare():&lt;br /&gt;
    ans = 0&lt;br /&gt;
    for i in range(1, n+1):&lt;br /&gt;
        aSum = bSum = 0&lt;br /&gt;
        for j in range(i, 0, -1):&lt;br /&gt;
            aSum = aSum ^ a[j]&lt;br /&gt;
            bSum = bSum ^ b[j]&lt;br /&gt;
            if (Valid(j, i, aSum, bSum)):&lt;br /&gt;
                ans += 1&lt;br /&gt;
&lt;br /&gt;
    fout = open(outFile, &amp;#039;w&amp;#039;)&lt;br /&gt;
    fout.write(str(ans) + &amp;quot;\n&amp;quot;)&lt;br /&gt;
    fout.close()&lt;br /&gt;
&lt;br /&gt;
if __name__ == &amp;quot;__main__&amp;quot;:&lt;br /&gt;
    Citire()&lt;br /&gt;
    Precalculare()&lt;br /&gt;
    Rezolvare()&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;/div&gt;</summary>
		<author><name>Raul</name></author>
	</entry>
</feed>