Project Euler 24 Lexicographic permutations( 康拓逆展开 )
题意:
排列指的是将一组物体进行有顺序的放置。例如,3124是数字1、2、3、4的一个排列。如果把所有排列按照数字大小或字母先后进行排序,我们称之为字典序排列。0、1、2的字典序排列是:012 021 102 120 201 210
数字0、1、2、3、4、5、6、7、8、9的字典序排列中第一百万位的排列是什么?
/*************************************************************************
> File Name: euler024.c
> Author: WArobot
> Blog: http://www.cnblogs.com/WArobot/
> Created Time: 2017年06月28日 星期三 13时08分35秒
************************************************************************/
#include <stdio.h>
#include <inttypes.h>
#define MAX_N 10
int32_t main() {
int32_t Target = 3;
int32_t vis[MAX_N] = {0} , fac[MAX_N] = {0};
int32_t ans[MAX_N + 1] = {0};
int32_t n = 10;
fac[0] = 1;
for (int32_t i = 1 ; i < n ; i++) {
fac[i] = fac[i - 1] * i;
}
Target--;
for (int32_t i = n - 1 ; i >= 0 ; i--) {
int32_t t = Target / fac[i];
int32_t j;
for (j = 0 ; j < n ; j++) {
if (vis[j]) continue;
if (t == 0) break;
t--;
}
vis[j] = 1;
ans[ ++ans[0] ] = j;
Target %= fac[i];
}
for (int32_t i = 1 ; i <= ans[0] ; i++) {
printf("%d ",ans[i]);
}
puts("");
return 0;
}
Project Euler 24 Lexicographic permutations( 康拓逆展开 )的更多相关文章
- Project Euler 62: Cubic permutations
立方数\(41063625 (345^3)\)的各位数重新排列形成另外两个立方数\(6623104 (384^3)\)和\(66430125 (405^3)\).事实上,\(41063625\)是满足 ...
- Project Euler Problem 24-Lexicographic permutations
全排列的生成,c++的next_permutation是O(n)生成全排列的.具体的O(n)生成全排列的算法,在 布鲁迪 的那本组合数学中有讲解(课本之外,我就看过这一本组合数学),冯速老师翻译的,具 ...
- Project Euler 44: Find the smallest pair of pentagonal numbers whose sum and difference is pentagonal.
In Problem 42 we dealt with triangular problems, in Problem 44 of Project Euler we deal with pentago ...
- 康托展开&康托逆展开 的写法
康托展开 康托展开解决的是当前序列在全排序的名次的问题. 例如有五个数字组成的数列:1,2,3,4,5 那么1,2,3,4,5就是全排列的第0个[注意从0开始计数] 1,2,3,5,4就是第1个 1, ...
- Python练习题 039:Project Euler 011:网格中4个数字的最大乘积
本题来自 Project Euler 第11题:https://projecteuler.net/problem=11 # Project Euler: Problem 10: Largest pro ...
- [project euler] program 4
上一次接触 project euler 还是2011年的事情,做了前三道题,后来被第四题卡住了,前面几题的代码也没有保留下来. 今天试着暴力破解了一下,代码如下: (我大概是第 172,719 个解出 ...
- HDU 1027 Ignatius and the Princess II(康托逆展开)
Ignatius and the Princess II Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K ( ...
- Python练习题 029:Project Euler 001:3和5的倍数
开始做 Project Euler 的练习题.网站上总共有565题,真是个大题库啊! # Project Euler, Problem 1: Multiples of 3 and 5 # If we ...
- Project Euler 9
题意:三个正整数a + b + c = 1000,a*a + b*b = c*c.求a*b*c. 解法:可以暴力枚举,但是也有数学方法. 首先,a,b,c中肯定有至少一个为偶数,否则和不可能为以上两个 ...
随机推荐
- mongodb--find高级用法
链式查询 db.person.find().limit(4).sort({sex:-1}) // sort来说,1 是升序, -1 是降序 尽量不要用mongodb去做一些复杂的运算 分页的写法 ·· ...
- mybatis中useGeneratedKeys和keyProperty的使用
领域模型主键属性是shopId,使用JDBC的getGenereatedKeys方法获取主键并赋值到keyProperty设置的领域模型shopId属性中,配置参考如下:<insert id=& ...
- 洛谷 P1692 部落卫队
P1692 部落卫队 题目描述 原始部落byteland中的居民们为了争夺有限的资源,经常发生冲突.几乎每个居民都有他的仇敌.部落酋长为了组织一支保卫部落的队伍,希望从部落的居民中选出最多的居民入伍, ...
- gdb的follow-fork-mode使用以及多线程操作
对于多线程,如果希望让其他线程不执行,只有调试线程执行,使用 set scheduler-locking [on|off|step]
- Foundation框架和文件操作
NSString --实例化方法-------------- NSString *str = [[NSString alloc] init]; NSString *str = [[[NSString ...
- eclipse配置默认的jdk
1.window-preferences-java-Installed JREs-Add-Standard VM-[添加jre6或jre7的路径]
- Mac上搭建android环境:Android Studio+GreenVPN
1.下载Android Studio,https://developer.android.com/sdk/index.html 2.使用GreenVPN,感觉还能够.18/月.http://www.g ...
- UML的基本图(二)
Both sequence diagrams and communication diagrams are kinds of interaction diagrams. An interacti ...
- Eclipse 连接hsqldb数据库
初学Java,在接触数据库根本无从下手,不知如何将程序和数据库连接起来,今天做一个记录. 数据库是:hsqldb_1_8_0_5 附链接百度云盘 链接:https://pan.baidu.com/s/ ...
- ORACLE 11g 生产中高水位线(HWM)处理
数据库中表不断的insert,delete,update,导致表和索引出现碎片.这会导致HWM之前有很多的空闲空间,而oracle在做全表扫描的时候会读取HWM一下的所有块,这样会产生更多的IO,影响 ...