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中肯定有至少一个为偶数,否则和不可能为以上两个 ...
随机推荐
- iostat -x 1 查看磁盘的IO负载
Linux系统出现了性能问题.一般我们能够通过top.iostat,vmstat等命令来查看初步定位问题.当中iostat能够给我们提供丰富的IO状态数据 $ iostat -x -1 avg-cp ...
- Data Binding Guide——google官方文档翻译(下)
这篇博客是Data Binding Guide官网文档翻译的下篇.假设没看过前半部分翻译的能够先看Data Binding Guide--google官方文档翻译(上) 一,数据对象 不论什么不含业 ...
- [Javascript] IntersectionObserver -- Lazy Load Images on a Website
When it comes to websites performance is king. How long it takes for a page to load can mean the dif ...
- [Angular] Performance Caching Policy - Cache First, Network Last
If you want to cache API response by using angular service-worker, you can do it in: src/ngsw-config ...
- C#实体转换
using System; using System.Collections.Generic; using System.Data; using System.Data.Common; using S ...
- 基于aspectj的aop的操作
1.引入相关的jar包 2.建两个类 public class Book { public void add(){ System.out.println("add-----------&qu ...
- Linux - 控制台界面,虚拟界面,字符界面
tty控制台终端. pts虚拟终端. tty1 图形界面. tty2 字符界面. Ctrl+Alt+F2-6 在字符界面下,通过Alt+F2 切换回来.或者切换到其他的字符界面. Alt+F2 pts ...
- Node.js:文件系统
ylbtech-Node.js:文件系统 1.返回顶部 1. Node.js 文件系统 Node.js 提供一组类似 UNIX(POSIX)标准的文件操作API. Node 导入文件系统模块(fs)语 ...
- 使用串口终端安装AIX操作系统
使用串口终端安装AIX操作系统 一.配置超级终端 首先,配置超级终端:在笔记本电脑上(Windows XP系统),点击开始à程序->附件->通讯->超级终端,配置名称为test的超级 ...
- postgresql 常规操作以及检查备份
一.建表时,复制源表的信息test=# test=# \d test.t1 Table "test.t1" Column | Type | Collation | Nullable ...