Pythonã§ç«¶æããã°ã©ãã³ã°ï¼ããã³ã³ï¼
ä»æ´ã§ã¯ãããä»å¾AtCoderã®åé¡ã解ãä¸ã§ä½¿ã£ãã©ã¤ãã©ãªã¨ãææ³ã¨ãã
ããã«ã¾ã¨ãã¦ãããã¨æããpythonéå®ã
å ¥åºå
åºæ¬
i = input() x,y = map(int,raw_input().split())
å¿ç¨
è¤æ°è¡ãä¸æ°ã«èªãã§ãå
¥åºåã®ãªã¼ãã¼ããããç¡ãããã
ï¼è¡æ°ãå¤ãæãç¨ã«ãããããªãã¨TLEã«ãªããï¼
lines = sys.stdin.readlines()
å ã¿ã«ãã¼ã«ã«ã§å®è¡ããã¨ãã¯<ctrl-d>
ã¨ãã§EOFãæ¤ç¥ãããå¿
è¦ãæãã
ãã¼ã«ã«ã§ã®å®è¡
ã¯ãªãããã¼ãã«ãã¹ããã¼ã¿ï¼ããã¹ãï¼ã³ãã¼ãããã¨ã
以ä¸ã§ç°¡åã«å®è¡ã§ããã
pbpaste| python test.py
åé¡ãé£ããã¦ããã¹ããã¼ã¿ãä½åã使ãããã ã¨æã£ãå ´åã¯ã
ãã¡ã¤ã«ã«æ¸ãåºãã¦ããã
pbpaste > dataset1 pbpaste > dataset2 python test.py < dataset1 python test.py < dataset2
ã©ã¤ãã©ãª
heapq
åªå é ä½ä»ããã¥ã¼ãå®ç¾ããã¢ã¸ã¥ã¼ã«ã Donutsã³ã³2015Cåé¡ãªã©ã§ä½¿ç¨ããããããã使ãæ©ä¼ãå¤ãã åºæ¬çã«ãè¦ç´ ã追å ã»æå°å¤ãåé¤ããç¶ç¶çã«å¿ è¦ãªæã«ä½¿ãã
from heapq import heappush, heappop, heapify heap = [2,3,7,1] heapify(heap)
以ä¸ã®3ã¤ã®æ©è½ã«å¯¾å¿ããæä½ãããã
ãã¥ã¼ã«å¯¾ãã¦è¦ç´ ãåªå 度ã¤ãã§è¿½å
heappush(heap,element)
æãé«ãåªå 度ãæã¤è¦ç´ ããã¥ã¼ããåãé¤ãããããè¿ã
heappop(heap)
æãé«ãåªå 度ãæã¤è¦ç´ ãåãé¤ããã¨ãªãåç §
heap[0]
itertools
çµã¿åãããé åã使ãã¨ãã¯ãç´ ç´ã«ã©ã¤ãã©ãªã使ãã®ãè¯ããã Donutsã³ã³2015ã®Båé¡ã§ä½¿ç¨ï¼å®éã«ã¯216ã¯64*1024=105ç¨åº¦ã®ãªã¼ãã¼ããç¡ãã®ã§ååæ±ãããâ¦ï¼
Python で組み合わせや順列を得るときは itertools を使う | CUBE SUGAR STORAGE
- 5C2ã ã¨
itertools.combinations([1,3,4,6,7],2)
Union-Find
Makoto Hiroiã®ãã¼ã¸ãåèã«ãã¾ããã
ARC#032ã®Båé¡ã§ä½¿ç¨ã
class UnionFind3: def __init__(self, size): # è² ã®å¤ã¯ã«ã¼ã (éåã®ä»£è¡¨) ã§éåã®åæ° # æ£ã®å¤ã¯æ¬¡ã®è¦ç´ ã表ã self.table = [-1 for _ in xrange(size)] # éåã®ä»£è¡¨ãæ±ãã def find(self, x): if self.table[x] < 0: return x else: # çµè·¯ã®å§ç¸® self.table[x] = self.find(self.table[x]) return self.table[x] # ä½µå def union(self, x, y): s1 = self.find(x) s2 = self.find(y) if s1 != s2: if self.table[s1] <= self.table[s2]: # å°ããã»ããåæ°ãå¤ã self.table[s1] += self.table[s2] self.table[s2] = s1 else: self.table[s2] += self.table[s1] self.table[s1] = s2 return True return False def main(): lines = sys.stdin.readlines() n,m = map(int,lines[0].split()) uf = UnionFind3(n) for line in lines[1:]: a,b = map(int,line.split()) uf.union(a-1,b-1) size = 0 for v in uf.table: if v < 0: size += 1 print(size-1) main()
æåself.table
ã«ã¯sizeåã®-1ãå
¥ã£ã¦ããã
ããã¯ãè¦ç´ æ°1ã®éåãsizeåããã¨ãããã¨ã
è² ã®å¤ãæã¤ã®ã¯ã«ã¼ãã ããªã®ã§ãè² ã®åæ°ã調ã¹ãã°ç´ éåã®æ°ããããã
nCr mod mï¼ã³ã³ããã¼ã·ã§ã³ã®moduloè¨ç®ï¼
ããã³ã³ã§ã¯çµã¿åããnCrã®è¨ç®çµæãåºåãããéã«ãæ°ã大ãããªã£ã¦ã大ä¸å¤«ãªããã«moduloè¨ç®ãããå ´åãå¤ãã
ï¼ARC#039ã®Båé¡ãªã©ï¼
def modc(a,b,m): c = 1 for i in xrange(b): c = c * (a - i) % m c = c * modinv(i + 1,m) % m return c def egcd(a, b): (x, lastx) = (0, 1) (y, lasty) = (1, 0) while b != 0: q = a // b (a, b) = (b, a % b) (x, lastx) = (lastx - q * x, x) (y, lasty) = (lasty - q * y, y) return (lastx, lasty, a) def modinv(a, m): (inv, q, gcd_val) = egcd(a, m) return inv % m
nCr mod m
ã¯modc(n,r,m)
ã§å¾ãããã
moduloï¼ååå¼ï¼ã®ååæ¼ç®
- å ç®ï¼(a + b) mod m = (a mod m) + (b mod m)
- æ¸ç®ï¼(a - b) mod m = (a mod m) - (b mod m)
- ä¹ç®ï¼(a * b) mod m = (a mod m) * (b mod m)
- é¤ç®ï¼(a / b) mod m = (a mod m) * b'
- b'ã¯b mod mã®éå ï¼éæ°ï¼ã§ãä¸è¨ã®modinvï¼åèï¼ã§æ±ããããã