POJ 1715 Hexadecimal Numbers 组合数学

POJ 1715 Hexadecimal Numbers 组合数学

题目地址

题意:

一个十六进制,最多8位而且每一位都不能重复,求所有符合的数中第n大的数。注意不能有前导0。

分析:

可以发现,第i位的任何一个取值,都有P(unused, i - 1)个数字串,只要从高位向低位,从F到1找过去,看第n个是否在这个区间里面,如果没有的话就把那位置为0,然后找下一位就行了。

代码:

/*
*  Author:      illuz <iilluzen[at]gmail.com>
*  File:        1715.cpp
*  Create Date: 2014-05-27 09:11:41
*  Descripton:
*/

#include <cstdio>

char ch[20], res[10];
int n;

int P(int tot, int get) {
	int ret = 1;
	while (get--) {
		ret *= tot;
		tot--;
	}
	return ret;
}

void init() {
	for (int i = 0; i < 10; i++)
		ch[i] = '0' + i;
	for (int i = 0; i < 6; i++)
		ch[i + 10] = 'A' + i;

}

void solve(int n) {
	bool vis[16] = {0}, head = false;
	int used = 0, tmp;
	for (int i = 8; i > 0; i--) {
		int j;
		for (j = 15; j > 0; j--) {
			if (!vis[j]) {
				tmp = P(16 - used - 1, i - 1);
				if (tmp < n) {
					n -= tmp;
				} else {
					vis[j] = true;
					break;
				}
			}
		}
		res[i] = ch[j];
		if (head || j != 0)
			used++;
		if (j != 0)
			head = true;
	}
}

int main()
{
	init();

	scanf("%d", &n);

	solve(n);
	bool head = false;
	for (int i = 8; i > 0; i--)
		if (head || res[i] != '0') {
			printf("%c", res[i]);
			head = true;
		}
	puts("");

	return 0;
}

POJ 1715 Hexadecimal Numbers 组合数学

时间: 2024-08-03 20:09:18

POJ 1715 Hexadecimal Numbers 组合数学的相关文章

poj 1715 Hexadecimal Numbers 排列组合

1 /** 2 大意: 给定16进制数的16个字母,,求第k大的数,,要求数的长度最大为8.,并且每个数互不相同. 3 思路: 从高到低挨个枚举,每一位能组成的排列数 ,拿最高位来说,能做成的排列数为15*A(15,len-i) 4 第二位 A(14,len-2)..这样就可以找到k大的数的长度 5 接下来 .找第k大的数.同上理 ,挨个枚举每一位即可..若加上该位的排列数大于k,则该位就是这个数,继续枚举下一位 6 **/ 7 8 /** 大神思路 9 首先确定数字串的长度Len:从大到小枚举

POJ 3252 Round Numbers 组合数学

Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 13381   Accepted: 5208 Description The cows, as you know, have no fingers or thumbs and thus are unable to play Scissors, Paper, Stone' (also known as 'Rock, Paper, Scissors', 'Ro, Sham, Bo

POJ 2154 Color(组合数学-波利亚计数,数论-欧拉函数,数论-整数快速幂)

Color Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 7693   Accepted: 2522 Description Beads of N colors are connected together into a circular necklace of N beads (N<=1000000000). Your job is to calculate how many different kinds of th

POJ 3252 Round Numbers 数学题解

Description The cows, as you know, have no fingers or thumbs and thus are unable to play Scissors, Paper, Stone' (also known as 'Rock, Paper, Scissors', 'Ro, Sham, Bo', and a host of other names) in order to make arbitrary decisions such as who gets

poj 3252 Round Numbers 【推导&#183;排列组合】

以sample为例子 [2,12]区间的RoundNumbers(简称RN)个数:Rn[2,12]=Rn[0,12]-Rn[0,1] 即:Rn[start,finish]=Rn[0,finish]-Rn[0,start-1] 所以关键是给定一个X,求出Rn[0,X] 现在假设X=10100100  这个X的二进制总共是8位,任何一个小于8位的二进制都小于X 第一部分,求出长度为[0,7]区间内的二进制是RoundNumber的个数  对于一个长度为Len的二进制(最高位为1),如何求出他的Rou

POJ 3641 Pseudoprime numbers 米勒罗宾算法

链接:http://poj.org/problem?id=3641 题意:由费马小定理可得,对于素数p,a^p = a (mod p),但是对于某些非素数p,也有比较小的可能满足a^p = a (mod p),如果满足,则称p是a条件下的伪素数,现给出p,a,问p是不是a条件的伪素数. 思路:首先用米勒 罗宾判断p是不是素数,如果不是,判断a^p = a (mod p)是否成立. 代码: #include <iostream> #include <cstdio> #include

poj 3252 Round Numbers (组合数学)

链接 :poj 3252 题意:一个数转化成二进制之后,0的个数大于等于1的为round数, 给定一个区间[m,n],问这区间内有多少round数 分析:要求[m,n]间的的round数, 可以用[1,n+1)的个数减去[1,m)的个数, 对于比N小的round数的个数: 化为二进制的长度比N的长度小的数:如果长度为L,那么高位肯定是1, 然后枚举0的个数,利用组合数求即可 长度和N相等但比N小的数: 从高位开始枚举,若出现1,则将这位看作0,再枚举之后的低位,肯定是比原数小的 依次下去,注意统

POJ 3252 Round Numbers(组合数学)

Round Numbers Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10223   Accepted: 3726 Description The cows, as you know, have no fingers or thumbs and thus are unable to play Scissors, Paper, Stone' (also known as 'Rock, Paper, Scissors',

POJ 1316 Self Numbers

题目链接: http://poj.org/problem?id=1316 Description In 1949 the Indian mathematician D.R. Kaprekar discovered a class of numbers called self-numbers. For any positive integer n, define d(n) to be n plus the sum of the digits of n. (The d stands for digi