POJ 3252 Round Numbers 数位dp(入门

题目链接:点击打开链接

题意:

给定一个区间,求区间内有多少个合法数(当这个数的二进制中0的个数>=1的个数称为合法数 二进制无前导0)

思路:

cnt[i]表示二进制长度为i位(即最高位为1,其他位任意)时的合法数个数。

sum[i] 就是二进制长度<=i位的合法数个数。

然后从最高位枚举到低位即可。维护当前0的个数。

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <iostream>
template <class T>
inline bool rd(T &ret) {
	char c; int sgn;
	if (c = getchar(), c == EOF) return 0;
	while (c != '-' && (c<'0' || c>'9')) c = getchar();
	sgn = (c == '-') ? -1 : 1;
	ret = (c == '-') ? 0 : (c - '0');
	while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
	ret *= sgn;
	return 1;
}
template <class T>
inline void pt(T x) {
	if (x <0) {
		putchar('-');
		x = -x;
	}
	if (x>9) pt(x / 10);
	putchar(x % 10 + '0');
}
using namespace std;
typedef long long ll;
const int N = 100;
int bit[N];
ll cnt[N], C[N][N], sum[N];
ll dfs(int len){
	ll ans = sum[len - 1];//最高位(即len位一定是1)二进制长度<=len-1的合法数个数

	int zero = bit[len]==0;
	for (int i(len-1); i; i--)
	{
		if (bit[i] == 0){
			zero++;
			continue;
		}
		int tmp = zero + 1;//此时第i位可以填0也可以填1,若填0,则后面siz位可以任意填,填出来的数都不会大于x
		int siz = i - 1;
		if (tmp >= len - tmp)ans += 1LL << siz;
		else {
			for (int j = 0; j <= siz; j++)//同样枚举0的个数
			if (tmp + j >= len - tmp - j)
				ans += C[siz][j];
		}
	}
	ans += zero >= len - zero;//x这个数是不是合法
	return ans;
}
ll solve(ll x){
	if (x <= 1)return 1;
	int len = 0;
	for (ll tmp = x; tmp; tmp >>= 1)bit[++len] = tmp & 1;
	return dfs(len);
}
int main() {
	memset(C, 0, sizeof C);//计算组合数
	C[1][0] = C[1][1] = 1;
	for (int i = 2; i < N; i++){
		C[i][0] = 1;
		for (int j = 1; j < N; j++)
			C[i][j] = C[i - 1][j] + C[i - 1][j - 1];
	}
	cnt[1] = 1; sum[1] = 1;
	for (int i = 2; i < N; i++){
		cnt[i] = 0;//二进制长度为i,最高位是1,其他位任意时的合法数个数
		for (int j = 0; j <= i - 1; j++) //因为最高位是1,所以自由位数有i-1个,枚举其中0的个数j,若0的个数j>=1的个数i-j,则cnt[i]的方法数+=在i-1个位置中选j个0
		if (j >= i - j)
			cnt[i] += C[i - 1][j];
		sum[i] = cnt[i] + sum[i - 1];
	}

	long long l, r;
	while (cin >> l >> r)
		cout << solve(r) - solve(l - 1) << endl;
	return 0;
}
/*
1 12
1 10
1 20

*/
时间: 2024-10-12 09:13:52

POJ 3252 Round Numbers 数位dp(入门的相关文章

POJ 3252 Round Numbers(数位dp&amp;amp;记忆化搜索)

题目链接:[kuangbin带你飞]专题十五 数位DP E - Round Numbers 题意 给定区间.求转化为二进制后当中0比1多或相等的数字的个数. 思路 将数字转化为二进制进行数位dp,由于一个二进制数的最高位必须为1.所以设置变量first记录前面位是否有1,若有1,则可随意放,否则,仅仅可放1. 同一时候.上面的推断决定了搜索时len的大小与二进制本身的长度不一定相等,所以需两个变量对1和0的个数进行记录. 用dp[a][b][c]保存长度a,b个0,c个1的数字个数.记忆化搜索.

POJ 3252 Round Numbers (数位DP)

题意:求区间内一个数二进制位1的数量大于等于0的数的个数. 析:dp[i][j][k] 表示前 i 位,长度为 j 的,1的数量是 k.注意前导0. 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <cstdlib> #include <cmath> #include <

POJ 3252 round numbers(数位DP)

#include <iostream> #include <cstdio> #include <cstdlib> #include <algorithm> #include <cmath> #include <vector> #include <queue> #include <cstring> #include <set> #include <stack> #include <m

poj 3252 Round Number 数位dp

Round Numbers Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11481   Accepted: 4305 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 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 3252 Round Numbers(数位DP)

Round Numbers Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11003   Accepted: 4064 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 3252 Round Numbers

Round Numbers Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12824   Accepted: 4946 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 3252 —— Round Numbers

Round Numbers Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u 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, S