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中肯定有至少一个为偶数,否则和不可能为以上两个 ...
随机推荐
- 实践一些js中的prototype, __proto__, constructor
<!DOCTYPE html> <html> <head> <title>ExtJs</title> <meta http-equiv ...
- MySQL必知必会面试题 基础
1.登录数据库 (1).单实例 mysql -uroot -poldboy (2).多实例 mysql -uroot -poldboy -S /data/3306/mysql.sock 2.查看数据库 ...
- Git用<<<<<<<,=======,>>>>>>>标记出不同分支的内容
Git用<<<<<<<,=======,>>>>>>>标记出不同分支的内容 当Git无法自动合并分支时,就必须首先解 ...
- hdu - 4920 - Matrix multiplication(缓存优化+开挂)
题意:求两个n x n的矩阵相乘后模3的结果,n <= 800. 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4920 -->>呀呀 ...
- C语言学习笔记:15_c语言中的进制操作.c
/* * 15_c语言中的进制操作.c * * Created on: 2015年7月5日 * Author: zhong */ #include <stdio.h> #include & ...
- wpf Command canExecute 更新
可以调用以下语句通知 CommandManager.InvalidateRequerySuggested();
- 2016.04.03,英语,《Vocabulary Builder》Unit 09
her/hes: from the Latin verb haerere, means 'to stick' or 'to get stuck'. adhesive means 'sticking' ...
- [Linux]history 显示命令执行的时间
显示历史命令之行时间 这里的环境是centos5.8 vim ~/.bashrc 或者 ~/.bash_profile 增加 export HISTTIMEFORMAT="%F %T & ...
- cdev_init和register_chrdev区别
--- 01:include/linux/fs.h static inline int register_chrdev(unsigned int major, const char *name, co ...
- DNS通道检测 国内学术界研究情况——研究方法:基于特征或者流量,使用机器学习决策树分类算法居多
http://xuewen.cnki.net/DownloadArticle.aspx?filename=BMKJ201104017&dbtype=CJFD<浅析基于DNS协议的隐蔽通道 ...