2013年5月9日 星期四

APIO 2011 Table Coloring

題目乍看很像狀態壓縮DP,但其實有深遠的含意在裡頭

N, M, K <= 10^5, 看起來超級無敵瘋狂

Observation 1: 假設知道第一行、第一列,全部就都可以推出來了(2^(N+M-1)種)

a11 a12 a13
a21
a31

假設前三個是a, b, c,這一個 d 就要是a ^ b ^ c ^ 1,如此才會讓 a^b^c^d = 1
(也就是題目的定義)

Observation 2:

在5*4的表格上,會長的如下圖所示,恰好是 X ^ 該行 ^ 該列 ^ (bool)(偶行偶列)


因此,假設已經有填過了,就會變成 a 與 b 之間的條件限制(如果一個是1另一個就是0之類的),如同2-sat一般,而且邊是雙向的,就像以前討論過的題目,可以直接用disjoint set去實現。但比較麻煩的是有可能a1, a2這些單個直接被設限制,雖然依照目前為止的公式就可以求解,但編程複雜度會較高。

Observation 3:

接下來,我們希望把規則化簡。我想的作法是這樣,把第一行全部 xor x,如此後面一大堆的 xor x 都消失了,當然這不會改變任何東西,只是當b1是 1 時可能會變 0 罷了。再者,可以發現大家都是兩個東西在 xor ,為了統一化,把第一列全部再 xor 一個 y,變成如下圖所示。答案則是依此計算岀符合條件的變量種類後,除以二。


 如此一來規則就很簡單,直接給出code。
http://ideone.com/o9XFTY

2013年5月6日 星期一

Primality Testing // from CLRS

Prime Number Theorem: // page 965

pi(N) ~= N / ln(N)

According to Terence Tao, the proof is something like the below:

      Listening to the "music" of the primes. We start with a "sound wave" that is "noisy" at the prime numbers and silent at other numbers; this is the von Mangoldt functionThen we analyze its notes or frequencies by subjecting it to a process akin to Fourier transform; this is the Mellin transform. Then we prove, and this is the hard part, that certain "notes" cannot occur in this music. This exclusion of certain notes leads to the statement of the prime number theorem.

---------------------------------------------------------------------------------------------

Chinese Remainder Theorem: // page 950

n = PRODUCT( ni ) { ni is pairwise relatively prime }
a <---> (a1 ... ak) { ai = a mod ni } have bijection.

PF:
a --> (a1 ... ak), QED
(a1 ... ak) --> a, a = SUM( ai * mi * ( mi^(-1) mode ni ) ), mi = PRODUCT( nj except ni ), QED

Practice: X = 2 (mod 3), X = 3 (mod 5), X = 2 (mod 7), Find X


---------------------------------------------------------------------------------------------

Primality Testing 1: // page 956

if n is prime, only x = 1 or -1, x^2 = 1 (mod n)

PF:
if n = 2, 0^2 = 0, 1^2 = 1, 2^2 = 1, QED
if n > 2, p | (x-1)(x+1). p | (x-1) or p | (x+1), but not both.(Use Contradiction) If p | (x-1), x = 1(mod n). if p | (x+1), x = -1(mod n), QED

Corollary: if exist x != 1 & x != -1, x^2 = 1 (mod n), n is composite.(contrapositive)

---------------------------------------------------------------------------------------------

Primality Testing 2: // page 967

if n is prime, for all x != kn, x^(n-1) = 1 (mod n)

PF:

define Ai = x * i (mod n); P = PRODUCT( Ai ) { i = 1 ~ (n - 1) } ; Q = PRODUCT( i ){ i = 1 ~ (n-1) }

All Ai are distinct. ( Use Contradiction, if Ai = Aj (mod n), then x * (i - j) = 0 (mod n), then n | x * (i - j), then n | (i - j), but i - j < n, Contradict. )

Thus, P = Q (mod n)

x^(n-1) * Q = Q (mod n)

Q( x^(n-1) - 1 ) = 0 (mod n)

x^(n-1) = 1 (mod n)

Corollary: if exist x^(n-1) != 1 (mod n), n is composite.(contrapositive)
But there's something called Carmichael Number, that looks just like Prime under this test.

---------------------------------------------------------------------------------------------

Primality Testing 3: // page 968

For PT2, We use a^(n-1) = 1 (mod n). But here let a^(n-1) = a^(u*2^t), check a^u, (a^u)^2, ((a^u)^2)^2 ... 

So we won't be fooled by Carmichael Number.

And we can prove that if n is composite, then there are (n-1)/2 number a, that can discover it is composite.

http://ideone.com/FzgRvN