$30 off During Our Annual Pro Sale. View Details »

Grundy

yosupo
November 18, 2016

 Grundy

yosupo

November 18, 2016
Tweet

More Decks by yosupo

Other Decks in Technology

Transcript

  1. Grundy਺ @yosupot

  2. ҎԼͷ໰୊Λߟ͑·͢ N௖఺MลͷDAG͕༩͑ΒΕΔɻ௖఺1ʹ͸͕ۨஔ͍ͯ͋Δ A͞ΜͱB͞Μ͕ަޓʹۨΛಈ͔͢ɻ ۨ͸ลͰͭͳ͕ͬͨ௖఺ʹಈ͔ͤͯɺಈ͔ͤͳ͘ͳͬͨΒ ෛ͚ Ͳ͕ͬͪউͭʁ

  3. ղ๏ dfsͰղ͘ ಈ͔ͤΔ௖఺ʹʮޙʯͱॻ͔Εͨ௖఺͕͋Ε͹ʮઌʯ
 ͳ͚Ε͹ʮޙʯͱॻ͘ ௖఺0ʹʮઌʯͱॻ͔ΕΕ͹ઌखඞউ
 ʮޙʯͱॻ͔ΕΕ͹ޙखඞউ

  4. ྫ ޙ ޙ ઌ ઌ ௖఺1

  5. ҎԼͷ໰୊Λߟ͑·͢ N௖఺MลͷDAG͕Kछྨ༩͑ΒΕΔɻͦΕͧΕͷάϥϑͷ ௖఺1ʹ͸͕ۨஔ͍ͯ͋Δ A͞ΜͱB͞Μ͕ަޓʹɺͲΕ͔ͷάϥϑͷۨΛಈ͔͢ɻ ۨ͸ลͰͭͳ͕ͬͨ௖఺ʹಈ͔ͤͯɺಈ͔ͤͳ͘ͳͬͨΒ ෛ͚ Ͳ͕ͬͪউͭʁ

  6. ղ๏ ࣮͸ෳ਺ͷάϥϑ͸1ͭͷάϥϑʹ·ͱΊΒΕΔ (άϥϑ1ͷۨͷҐஔ, άϥϑ2ͷۨͷҐஔ, …, άϥϑKͷۨͷ Ґஔ)Λ௖఺ͱͯ͠ద੾ʹลΛషΔ ͜ͷάϥϑͰಉ͡Α͏ʹdfs͕Ͱ͖Δ ܭࢉྔ͸O((N+M)^K)

  7. O((N+M)^K)ʂʁ N=M=5000ͱͯ͠(N+M)^K͸ʁ K=1 → 10000 K=2 → 1,000,000,000 K=3 →

    10,000,000,000,000 K=100 → 10^400
  8. ΋ͬͱޮ཰ྑ͘ܭࢉͰ͖ͳ͍͔ʁ ·ͣ͸K=2ͷ৔߹Λߟ͑Δ

  9. ߟ࡯ 2ͭͷάϥϑ͕ͲͪΒ΋ޙखউͪͳΒ͹ɺશମͰ΋ޙखউͪ 2ͭͷάϥϑ͕(ઌखউͪ + ޙखউͪ)ͳΒ͹ɺશମͰ͸ઌख উͪ ͩͬͨΒɺ(ઌखউͪ + ઌखউͪ)ͷ৔߹͸ʁ ࣮͸શମͰઌखউͪʹͳΔྫ΋ޙखউͪʹͳΔྫ΋࡞ΕΔ

  10. Ұҙʹఆ·Βͳ͍ ʴ ʴ ʹ ʹ ઌखউͪ ޙखউͪ

  11. Ͳ͏͢Δʁ άϥϑΛʮઌखউͪʯͱʮޙखউͪʯʹ·ͱΊΔ͜ͱ͸Ͱ ͖ͳ͍ Ͱ΋2छྨ͸ఘΊΔͱͯ͠΋ɺԿछྨ͔ʹάϥϑΛ෼ྨ͢Δ ͜ͱ͸Ͱ͖ͳ͍ͷ͔ʁ

  12. (άϥϑA + άϥϑX)ͷউഊͱ(άϥϑB + άϥϑX)ͷউഊ ͕ɺXʹ͔͔ΘΒͣ౳͍࣌͠ɺάϥϑA,BΛʮຊ࣭తʹ౳͠ ͍ʯͱ͢Δ (ޙखউͪ+ޙखউͪ)ͱ(ޙखউͪ+ઌखউͪ)͸Ұҙʹఆ·Δ ͷͰɺޙखউͪͲ͏͠͸ʮຊ࣭తʹ౳͍͠ʯ ͳͷͰɺઌखউͪͲ͏͠΋ʮຊ࣭తʹ౳͍͠ʯͷͰ͸ʁͱ

    Ծఆࣦͯ͠ഊͨ͠ͷ͕ࠓͷঢ়ଶ ઌखউͪͷάϥϑ͸1छྨʹ·ͱΊΔ͜ͱ͸Ͱ͖ͳ͍ʂ
  13. ͱ͜ΖͰ ͖ͬࣔͨ͞͠Α͏ʹɺҎԼͷ2ͭͷάϥϑ͸౳͘͠ͳ͍ ͳͥʁ

  14. ͱ͜ΖͰ ࠨͷάϥϑ͸ޙखউͪʹ͔͠ߦ͚ͳ͍͕ɺӈͷάϥϑ͸ޙखউͪͱઌखউͪͷ ྆ํʹߦ͚Δ ޙखউͪͱઌखউ͕ͪ྆ํඋΘΓ࠷ڧʹݟ͑Δ(ӈͷάϥϑͷํ͕ࣗ༝౓͕ߴͦ ͏)

  15. ͡Ό͋ ͜ͷάϥϑ͸ߋʹ࠷ڧͳͷͰ͸ʁ

  16. ͡Ό͋ ͜ͷάϥϑ͸ߋʹ௒࠷ڧͳͷͰ͸ʁ

  17. ࣮͸ ׬શDAGͲ͏͠͸ޓ͍ʹ౳͘͠ͳ͍͜ͱ͕ࣔͤΔ(TODO) ͳͷͰগͳ͘ͱ΋༗ݶछྨͰ͸ͳ͍

  18. ΄΅׬શDAG͸ʁ ׬શDAGಉ࢜͸ҟͳΔ͜ͱ͕Θ͔ͬͨ ͡Ό͋׬શDAGʹ1ݸ௖఺ΛՃ͑ͨ΋ͷ͸ʁ ׬શDAGͷ͏͍͔ͪͭ͘ͷ௖఺ʹɺ௖఺1͔Βล͕ੜ͍͑ͯ ΔɺΑ͏ͳάϥϑ

  19. ྫ͑͹ ͜Ε͸௖఺1͔Β௖఺਺1, 3ͷDAGʹߦ͚ΔΑ͏ͳάϥϑ

  20. ྫ͑͹ ͜Ε͸௖఺1͔Β௖఺਺1, 3ͷ׬શDAGʹߦ͚ΔΑ͏ͳάϥϑ ͜Ε͸௖఺਺2ͷ׬શDAGͱ౳͍͠

  21. ͳͥʁ ௖఺਺3ͷ׬શDAGʹߦͬͯɺ༗རʹͳΔ͜ͱ͸ͳ͍ ͳͥͳΒ͹ɺ૬ख͸࣍ͷखͰ௖఺਺2ͷ׬શDAG΁ҠಈͰ͖ΔͨΊ ݁ہɺ௖఺਺1ͷ׬શDAG΁ߦ͔͘͠ͳ͍ͨΊɺ௖఺਺2ͷ׬શDAGͱຊ࣭తʹ౳͍͠ ͜ͷลΛ࢖ͬͯ༗རʹͳΔ͜ͱ͸ͳ͍

  22. ΄΅׬શDAG ௖఺਺{a, b, c, …}ʹߦ͚Δͱ͢Δͱɺ௖఺਺=ʮ{a, b, c, …}ʹ ग़ͯ͜ͳ͍࠷খͷਖ਼੔਺ʯͷ׬શDAGͱຊ࣭తʹ౳͍͠ ྫ͑͹{1,

    3, 4, 5}ʹߦ͚ΔͳΒ͹௖఺਺=2 ͳ͔ͥͱ͍͏ͱɺ3,4,5ͷͲΕʹߦ͘ͷ΋ɺ࣍ͷखͰ૬खʹ2 ʹ໭͞ΕΔͷͰɺ༗རʹͳΕͳ͍
  23. Ұൠʹ ઌड़ͷنଇΑΓؼೲతʹɺશͯͷάϥϑ͸׬શDAGͷͲΕ͔ͱ ຊ࣭తʹ౳͍͠ ௖఺਺=kͷ׬શDAGͱຊ࣭తʹ౳͍͠άϥϑΛlevel (k-1)ͷάϥ ϑͱݺͿ (0-indexedͰͳ͍ͱ৭ʑ໘౗) άϥϑͷlevel͸࠶ؼతʹܭࢉͰ͖ΔͨΊɺO(N+M)ͰٻΊΒΕΔ

  24. ݁ہ (௖఺਺aͷ׬શDAG + ௖఺਺bͷ׬શDAG)ͱมܗͰ͖Δ ͜Εͷඞউ൑ఆ͸؆୯(a = b͔൱͔) O(N+M)

  25. K=3 K=3ͷ৔߹͸ʁ ࠷ॳͷ۪௚ղΛࢥ͍ग़͢ͱɺෳ਺ݸͷάϥϑ͸1ݸͷάϥϑʹ·ͱΊΒΕΔ ͦͯ͠ɺk=2ͳΒ͹શͯͷάϥϑ͸׬શDAG΁มܗͰ͖Δ͜ͱΛ࢖͏ (άϥϑA + άϥϑB + άϥϑC)=
 {άϥϑA

    + (άϥϑBͱάϥϑCΛ·ͱΊͨ΋ͷ)}=
 (׬શDAG + άϥϑB + άϥϑC) Αͬͯk=2ͷ࣌ͱಉ͡نଇͰ׬શDAG΁มܗͯ͠Α͍
  26. K=4 K=4ͷ৔߹͸ʁ (άϥϑA + άϥϑB + άϥϑC + άϥϑD)=
 {άϥϑA+(άϥϑBͱάϥϑCͱάϥϑDΛ·ͱΊͨ΋ͷ)}=


    (׬શDAG + άϥϑB + άϥϑC + άϥϑD)
  27. ݁ہ (׬શDAG + ׬શDAG + … + ׬શDAG)ʹมܗͰ͖Δ ͜ͷඞউ൑ఆ͸ʁ(࣮͸NimήʔϜͱ͍͏༗໊ͳܗͷήʔϜʹͳ͍ͬͯΔ…) (׬શDAG

    + ׬શDAG + … + ׬શDAG)=
 {(׬શDAGͱ׬શDAGΛ·ͱΊͨ΋ͷ) + (K-2ݸͷ׬શDAGΛ·ͱΊͨ΋ͷ)}=
 ʹมܗͰ͖Δ ͱ͜ΖͰɺK=2ͳͷͰ(׬શDAGͱ׬શDAGΛ·ͱΊͨ΋ͷ)͸׬શDAG΁มܗͰ͖Δ ͭ·Γɺ(׬શDAGͱ׬શDAGΛ·ͱΊͨ΋ͷ)Λ׬શDAGʹมܗͰ͖ΔͱɺK-1ʹؼ ணͰ͖Δ
  28. ׬શDAG + ׬શDAG (׬શDAGͱ׬શDAGΛ·ͱΊͨ΋ͷ)ͱ౳͍͠׬શDAGͷ ௖఺਺͸ʁ ࣮͸ɺ௖఺਺(a+1)ͱ(b+1)Λ·ͱΊΔͱɺ௖఺਺(a xor b)+1 ͱ౳͘͠ͳΔ(TODOઆ໌) ͭ·Γɺlevel

    aͱlevel bΛ߹੒͢Δͱɺlevel (a xor b)ʹͳΔ (͜Ε͕0-indexedʹͨ͠ཧ༝)
  29. ղ๏ ͱ͍͏͜ͱ͸ɺlevel a + b + …ͳΒ͹ɺlevel (a xor b

    xor …) ͱ౳͍͠ ͜Ε͕0͔Ͳ͏͔൑ผ͢Ε͹౴͑ O(K(N+M))ʂ