- プログラミングのお題スレ Part3
604 :デフォルトの名無しさん[sage]:2014/04/28(月) 10:33:49.06 ID:3V71eLEm - >>573 Python
import time, itertools def f573(n): t = time.clock() ans = [] q = [i**n for i in range(10)] cwr = itertools.combinations_with_replacement for p in cwr(range(10), n): s = sum(q[d] for d in p) if len(str(s)) == n and s == sum(q[int(c)] for c in str(s)): ans.append(s) ans.sort() print "n=%d -> %s %f" % (n, str(ans), time.clock() - t) for n in [3,4,5,6,7,8]: f573(n) n=3 -> [153, 370, 371, 407] 0.000000 n=4 -> [1634, 8208, 9474] 0.000000 n=5 -> [54748, 92727, 93084] 0.016000 n=6 -> [548834] 0.031000 n=7 -> [1741725, 4210818, 9800817, 9926315] 0.078000 n=8 -> [24678050, 24678051, 88593477] 0.172000
| - 【初心者歓迎】C/C++室 Ver.90【環境依存OK】
499 :デフォルトの名無しさん[sage]:2014/04/28(月) 11:03:17.60 ID:3V71eLEm - >>494
オーバーフローを回避するにはキャストすると良いよ A = ((long long)A * A) % 100000;
| - プログラミングのお題スレ Part3
610 :デフォルトの名無しさん[sage]:2014/04/28(月) 22:14:15.80 ID:3V71eLEm - >>607
ループ回数を調べてみた n=3 : 220 n=4 : 715 n=5 : 2002 n=6 : 5005 n=7 : 11440 n=8 : 24310 リファレンスによると、繰り返しを許した組合せ # combinations_with_replacement('ABC', 2) --> AA AB AC BB BC CC で組み合わせの数の計算式は (10+n-1)! / n! / (10-1)!
|
|