- 論壇徽章:
- 2
|
本帖最后由 l2y3n2 于 2011-05-06 20:16 編輯
隨便用Python寫了個(gè)窮舉的,用時(shí)間正好3分鐘、Python自帶的大數(shù)運(yùn)算、程序也沒(méi)有優(yōu)化過(guò)。
就是窮舉9、8、7、……、0的個(gè)數(shù),然后對(duì)比結(jié)果。
用C的話時(shí)間上應(yīng)該還是沒(méi)問(wèn)題的
~$ time ./f.py
449177399146038697307
128468643043731391252
real 2m54.915s
user 2m54.871s
sys 0m0.012s
- #! /usr/bin/env python
- # -*- coding: UTF-8 -*-
- def check(value, nums):
- tmp = [0] * 10
- for c in str(value):
- tmp[ord(c) - ord('0')] += 1
- for i in range(0, 10):
- if tmp[i] != nums[i]:
- return False
- return True
- def action(exps, nums, n, leftn, value, idx):
- value += exps[idx] * leftn
- nums[idx] = leftn
- if len(str(value)) < n:
- return
- if (idx == 0):
- if (check(value, nums)):
- print(value)
- return
- for i in range(0, leftn + 1):
- if len(str(value)) <= n:
- action(exps, nums, n, leftn - nums[idx], value, idx - 1)
- value -= exps[idx]
- nums[idx] -= 1
- def main(n):
- exps = [i ** n for i in range(0, 10)]
- nums = [0] * 10
- action(exps, nums, n, n, 0, 9)
- if __name__ == '__main__':
- main(21)
復(fù)制代碼 |
|