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中肯定有至少一个为偶数,否则和不可能为以上两个 ...
随机推荐
- ElasticSearch中profile API的使用
1. 前言 profile API 是 Elasticsearch 5.x 的一个新接口.通过这个功能,可以看到一个搜索聚合请求,是如何拆分成底层的 Lucene 请求,并且显示每部分的耗时情况. 2 ...
- 0926mysql join的原理
转自 http://www.cnblogs.com/shengdimaya/p/7123069.html MySQL JOIN原理 先看一下实验的两张表: 表comments,总行数28856 表 ...
- 第三篇:SpringBoot - 数据库结构版本管理与迁移
SpringBoot支持了两种数据库结构版本管理与迁移,一个是flyway,一个是liquibase.其本身也支持sql script,在初始化数据源之后执行指定的脚本,本章是基于 Liquibase ...
- pl/sql developer br 文件
pl/sql developer br 文件 Version=1 RightMargin=72 Indent=2 UseTabCharacter=FALSE TabCharacterSize=2 Al ...
- Meteor 前端 RESTful API 通过后端 API 下载文件
Meteor 下载文件 问题场景 后端 HTTP server提供一个下载接口,可是须要前端 Meteor 可以给浏览器用户开一个URL来下载这个文件. 举例:在线的Meteor Logo文件就好比后 ...
- 【POJ 2486】 Apple Tree(树型dp)
[POJ 2486] Apple Tree(树型dp) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8981 Acce ...
- 怎样 获取 ios的系统版本
获得Ios系统版本的函数,比方 函数定义: [cpp] view plaincopy + (float)getIOSVersion; 函数实现: [cpp] view plaincopy + (flo ...
- Android开发时经经常使用的LogUtil
在开发过程中经经常使用到Log.我们常写的一种方式就是自己定义一个LogUtil工具类 private static boolean LOGV = true; private static boole ...
- 能够在子线程绘画的View SurfaceView
转载请注明出处:王亟亟的大牛之路 近期两天都没有写文章,一方面是自己在看书.一方面不知道写什么,本来昨天想写Glide或者RxAndroid的东西结果公司的"狗屎"网怎么都刷不好G ...
- ubuntu下创建第一个rails应用程序
一.创建一个新的应用程序 在控制台输入 > rails new demo create create README.rdoc create Rakefile create config.ru ...