Python
Alignmentã®Backtrackã®ã¨ãã«ãæåãåã«è¿½å ããªããã°ãªããªãããå½ç¶å¾ãã«è¿½å ãã¦æå¾ã«æååãéã«ããæ¹ãéãã¨æããããããããã®ãããªã³ã¼ãã§ã import sys import time def f_add_to_head(N): s = '' for _ in range(N): s = 'c' + s retâ¦
Problem 490ã§ä½ã£ãããæ¼ãã³ã¼ããg++ã§ã³ã³ãã¤ã«ãã¦åããã¦ã¿ãããã¼ã¸ã§ã³ã¯4.8.4-2ãããããã®ã³ã¼ãã¯æ¬å½ã«ååæ¼ç®ããè¡ã£ã¦ããªããSTLã使ã£ã¦ããªãåç´ãªãã®ã§ããããããã«ããã¯éããªããªãã¨æã£ããã Win(MSVC) 2208s Ubuntu(g++)â¦
ã¡ã¢ãªã®ä½¿ç¨éãè¦ã¦ã¿ãã ã¾ãã¯listããã #!/usr/bin/python import time a = [ n + 1 for n in xrange(10 ** 7) ] time.sleep(10) Python(Win) 160MB PyPy(Win) 40MB Python(Ubuntu) 320MB PyPy(Ubuntu) 80MBint64ã ããåã«ãªãã®ã¯ãããã #!/usr/bâ¦
32ããããè¶ ããæ´æ°Ubuntuã®Pythonãéãã£ãããããã§ãªãå ´åã¯ã©ãã ãããç°¡åãªä¾ã§ç¢ºããã¦ã¿ãã #!/usr/bin/python import sys import time def H(n): return sum(n / k for k in xrange(1, n + 1)) t0 = time.clock() print H(10 ** 8) print >>â¦
PyPyã¯ãããã¦ãã®å ´åPythonã®ã³ã¼ããã®ãã®ãåããã¦ãPythonããæ°åéããªããåç´ãªè¨ç®ãªãC++並ã¿ã«éããªãã ããããè¾æ¸ãæ±ãé¨åã主ã«ãªãã¨é端ã«é ããªããPythonããé ããªãã Ubuntuã®Python/PyPyã§ã¯ã©ããªãã®ã調ã¹ã¦ã¿ãã #!/usrâ¦
以åã«ãWindowsãBashããµãã¼ããããã¨ããè¨äºããã£ãããããããã°Pythonãéãããã¯ãã ããªããªããWindowsçã®Pythonã¯32ããããè¶ ããã¨å¤åé·æ´æ°ã¨ããã¦ãã¾ãããLinuxã®Pythonãªã64ãããã¾ã§ãªãintæ±ãã«ãªãã¯ãã ããã ãããã§ãPytâ¦
Problem 468ã®è¨ç®éãè¦ç©ããã¨ãã«ãããªåé¡ãæãæµ®ãã³ã¾ããã N以ä¸ãç´ å æ°å解ããã¨ããç´ å æ°ã®åæ°ã®å¹³åã¯ï¼ ä¾ãã°ã2ã3ã4 = 22ã5ãªã1åã6 = 2 * 3ãªã2åã¨ãã¾ããlogNããã¯ãã£ã¨å°ããã¨æããã¾ããã¨ããããã³ã¼ããæ¸ãã¦èª¿ã¹â¦
ä¸çã¯2ä¹ã§ã§ãã¦ãã èªç¶ã«ã²ããå¹³æ¹æ°ã®ä¸æè° (ãã«ã¼ããã¯ã¹)ä½è : å°å³¶å¯ä¹åºç社/ã¡ã¼ã«ã¼: è¬è«ç¤¾çºå£²æ¥: 2013/08/20ã¡ãã£ã¢: æ°æ¸ãã®ååãå«ãããã° (13件) ãè¦ããã®æ¬ã«ããªã¤ã©ã¼ã®ãã¼ã¼ã«åé¡ã«å¯¾ããå·å¿µãæ¸ããã¦ãã¾ããããã¼ã¼â¦
Problem 226ãã®æ²ç·ã¯é«æ¨æ²ç·ã¨ãã¦ãç¥ããã¦ãã¾ããé«æ¨ã¨ã¯ãã¡ããé«æ¨è²æ²»ã®ãã¨ã§ãã ãã®åé¡ã解ãã«ããããé£ç¶æ§ã¨å¾®åä¸å¯è½æ§ã«ã¤ãã¦ã®è¨¼æãèããã®ã§ãããããã©ããªã®ã§ããã«ã¯æ¸ãã¾ããã ãã©ã¼ã©ã ã«æ¸ãã¾ããã
Problem 227 ãã©ã¼ã©ã ã«æ¸ãã¾ããã
Problem 220 forumã«æ¸ãã¾ããã
Pythonã§ã¯æ´æ°ã¯å¤§ãããªãã¨åæã«å¤åé·æ´æ°ã¨ãã¦è¡¨ãã®ã§èª¤å·®ã¯åºã¾ããããæµ®åå°æ°ç¹æ°ãä»ããã¨é端ã«èª¤å·®ãçºçãã¾ããProject Eulerã§ç¹ã«åé¡ã«ãªãã®ã¯ãæ´æ°ã®å¹³æ¹æ ¹ã®è¨ç®ã§ããããªãã¡ã âânâ ã®è¨ç®ã§ãããã®è¨ç®ã¯æ´æ°nãæµ®åå°æ°ç¹æ°â¦
Problem 303ãã©ã¼ã©ã ãè¦ã¦ãã ããã
Problem 208ãããã°ã大å¤ã ã£ãã
Problem 202èãæ¹ã¯ç°¡åã§ãããã³ã¼ãããªããªãæ¸ããªãã¦ã
ãªã¹ãã®é¨åãªã¹ããå¾ãã¨ããã¤ãã®ããã«ãã¾ãã a = range(5) # [0, 1, 2, 3, 4] a[1:3] # [1, 2] range(1, 3)ã«å¯¾å¿ãã¦ãã¾ããåæ§ã«ã¹ãããå¹ ãæå®ã§ãã¾ãã a[1:4:2] # [1, 3] a[3:0:-1] # [3, 2, 1] ãããrange(1, 4, 2)ãrange(3, 0, -1)ã«â¦
inæ¼ç®åã便å©ã§ãããããããªæãã§ãã a = [ 1, 2, 3 ] print 2 in a # True C#ãªãã var a = new int[] { 1, 2, 3 }; Console.WriteLine(a.Contains(2)); // True ã¡ãã£ã¨ãã£ã¨ããããC++ã«è³ã£ã¦ã¯ã int a[] = { 1, 2, 3 }; std::cout << (std::â¦
ä¾ãã°ããããªã³ã¼ããããã¨ãã¾ãã def divs1(N): return [ N / n for n in xrange(1, N + 1) ] N = 10 ** 8 print sum(div1(N)) å å 表è¨ã§ããããããééã£ã¦ãappendã使ã£ã¦æ¬¡ã®ããã«æ¸ãã¦ã¯ããã¾ããã def divs2(N): a = [] for n in xrange(â¦
ä¾ãã°ç´ å æ°å解ãä¸ãã¦ç´æ°ãå ¨ã¦åæããã³ã¼ããæ¸ãã¾ãããããªæãã§ããããã def pows(n, e): yield 1 q = 1 for _ in xrange(e): q *= n yield q def divisors(fs): if not fs: yield 1 else: p, e = fs[0] qs = pows(p, e) for n in divisors(fâ¦
Problem 1ãèãã¾ãããµã¤ãã«æ¸ãã¨ãããªæãã§ãã N = 10 ** 8 # ãªãªã¸ãã«ã¯1000 print sum(n for n in xrange(1, N) if n % 3 == 0 or n % 5 == 0) ãã®åé¡ã¯ããã°ã©ããé¢æ°åã«å°ãããã®ãã®ãªã®ã§ãã ãããåããã¨ãPythonã§13ç§ãPyPyã§3.â¦
Problem 36ãã®åé¡ã¯ãåã«2ã¨10ã®ä¸¡æ¹ã®åºæ°ã§åææ°ã«ãªããã調ã¹ãã¨ããåç´ãªæ¹æ³ã§ãååéãã§ããèªç¶æ°nã®åææ°ã®å¤å®ã¯O(logn)ã®è¨ç®éãªã®ã§ãNã¾ã§ã§O(NlogN)ã®è¨ç®éã§ããåææ°ãçæããã»ããéãã§ãã10é²ã§åææ°ãçæãã¦ã2é²ã§åâ¦
Project Eulerã§ãã°ãã°ãââãæºããNæªæºã®æ大ã®æ´æ°ãæ±ãããã¨ãã£ãåé¡ãè¦ããã¾ããä¾ãã°ãProblem 26ã§ãããã®åé¡ãèªã¿æ¿ããã¨ãã10ãFpã®çæå ã«ãªãNæªæºã®æ大ã®ç´ æ°ãæ±ãããã¨ã»ã¼ãªãã¾ããããããåé¡ã¯ãN-1ããé çªã«æ´æ°ãé¡â¦
PyPyã¯åãã½ã¼ã¹ãåããã¦ãPythonããããã¦ãéãã®ã§ãããé ããªããã¨ãããã¾ãããã®å¯¾å¦æ³ãããã¤ãããã®ã§ç´¹ä»ãã¦ããã¾ããä»åã¯ãªã¹ãã®ã½ã¼ãã§ãããããªã³ã¼ããåããã¦ã¿ã¾ãã from itertools import * import time def gen_S(): S =â¦
ãããã¾ã§ã§æ大ã®ç´ æ°ããçºè¦ << WIRED.jpä¹ ãã¶ãã«æ°ããªã¡ã«ã»ã³ãç´ æ°ãçºè¦ãããããã§ããã¡ã«ã»ã³ãæ°ã¯ã Mn = 2n - 1 ã¨ããå½¢ããã¦ããã®ã§ããããªããã®å½¢ã®ç´ æ°ãæ±ãããã¨ãããã¨ããã¨ãé常ã«é«éãªç´ æ°å¤å®æ³ãããããã§ãããã¦ãâ¦
å°ä½é¡ ã¾ãã¯å°ä½é¡ã®èª¬æãããã¾ããä¾ãã°15ã®å°ä½ã§èãã¾ãã2ã¨17ã¨32ã¯15ã§å²ãã¨ä½ãã2ã«ãªãã¾ãããããã¯åãã¨ã¿ãªãã¦å°ä½é¡ã¨å¼ã³ã¾ãã H0 = { â¦, -15, 0, 15, 30, ⦠} H1 = { â¦, -14, 1, 16, 31, ⦠} H2 = { â¦, -13, 2, 17, 32, ⦠} ⦠-13â¦
åå²æ°ã¨ããã®ã¯ãä¾ãã°5ãèªç¶æ°ã§åå²ãããã¨ãèãã¾ããåãæ°ãä½åº¦ã使ã£ã¦ãããã§ãããé åºéãã¯åºå¥ãã¾ãããããããã¨ã 5 = 5 = 4 + 1 = 3 + 2 = 3 + 1 + 1 = 2 + 2 + 1 = 2 + 1 + 1 + 1 = 1 + 1 + 1 + 1 + 1ã®7éãããã¾ããããã p(5)â¦
æ¨æ¥ã®Problem 405ã«é¢é£ããã解ãã®ã«ã¯å¿ è¦ã®ãªãæ å ±ã 10eä¹ã®å°ä½ã¯ãPythonã«ã¯powé¢æ°ãç¨æããã¦ç°¡åã«è¨ç®ã§ãã¾ããä¾ãã°ã3ã®1010ä¹ã®123456789ã®å°ä½ã¯ã pow(3, 10 ** 10, 123456789)ã§æ±ãããã¾ããããããææ°ã大ãããªã£ã¦ããã¨æ®µã â¦
Problem 169 ã³ã¼ãã¯ãã©ã¼ã©ã ã«ã
ã¨ã©ãã¹ããã¹ã®ãããªãªã¹ãã使ãè¨ç®ã¯Pythonã§ã¯é常ã«é ãã§ãããNumpyã使ãã¨éããªãã¾ããhttp://stackoverflow.com/questions/2068372/fastest-way-to-list-all-primes-below-n-in-pythonã«æ¸ããã¦ããprimesfrom2toã¨ããé¢æ°ã§ãã import nuâ¦
Problem 389ããã¯ç¢ºçè¨ç®ãããåé¡ãªã®ã§ããã¤ãã®ããã«æ¯é¢æ°ã®è¨ç®ããããã¨ã«ãªãã¾ããããããå¤é å¼ããªã¹ãã¨ãã¦è¨ç®ããã¨ãPythonã¯é常ã«é ããã§ãããããããªã¨ããNumPyã使ãã¨åçã«éããªãã¾ãããã¦ã³ãã¼ãã¯ãã®ãããããã htâ¦