codeiq.jp
åé¡ã¯ä»¥ä¸ã®ãããªæã
\(10^9\) 以ä¸ã®æ£ã®æ´æ°ã®ãã¡ã
3, 5, 7, 11, 13, 17, 19, 23, 29, 31
ã®å°ãªãã¨ãã©ããä¸ã¤ã®åæ°ã¨ãªããã®ã®ç·åãæ±ãã¦ãã ããã
æç´ã«\(10^9\)ã¾ã§ã®ãã¹ã¦ã®æ£ã®æ´æ°ã«ã¤ãã¦ä¸ã¤ä¸ã¤å²ããã確èªãã¦ããã¨ãªããªãçµãããªã
å
é¤åçã¨çå·®æ°åã®ç·åã®å
¬å¼ã使ãã°ãã©ã®æ°å以ä¸ã¾ã§ãã§ãã¯ãããã¯ãã¾ãå½±é¿ããªããªã
包除原理 - Wikipedia
import itertools, operator N = 10**9 p = [3, 5, 7, 11, 13, 17, 19, 23, 29, 31] ret = 0 for i in xrange(len(p)): for j in itertools.combinations(p, i + 1): m = reduce(operator.mul, j) d = N / m ret += d * (d + 1) / 2 * m * (2 * (len(j) % 2) - 1) print ret
â工夫ããã¨7,80æåãããã§è§£ããããã«ãªããããï¼