eBash

It is not the mountain we conquer but ourselves

Project Euler Problem 24

| Comments

Table of Contents

1 Problem

A permutation is an ordered arrangement of objects. For example, 3124 is one possible permutation of the digits 1, 2, 3 and 4. If all of the permutations are listed numerically or alphabetically, we call it lexicographic order. The lexicographic permutations of 0, 1 and 2 are:

012 021 102 120 201 210

What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9?

2 Solution

看到排列组合的问题,立马就想到了用排列组合的数学方法来求解。
只需要高中数学的知识即可,计算过程如下:
9!*2 <= 1000000 < 9!*3
最高位为从小到大排列未被使用的第三位数字2
1000000 – 9!*2 = 274240

8!*6 <= 27420 < 8!*7
现在数字2已经被使用,故从小到大排列未被使用的第七位数字为7
27420 – 241920 = 32320

…..

按这个过程计算出来的为2783915604,但这并不是真正所求的,而是所求序列的下一个。很容易知道2783915604序列的前一个数为2783915460

3 Answer

2783915460

Source:C++

Comments