poj3252Round Numbers非递归数位dp解

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 9894   Accepted: 3569

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 to be milked first.
They can‘t even flip a coin because it‘s so hard to toss using hooves.

They have thus resorted to "round number" matching. The first cow picks an integer less than two billion. The second cow does the same. If the numbers are both "round numbers", the first cow wins,

otherwise the second cow wins.

A positive integer N is said to be a "round number" if the binary representation of
N has as many or more zeroes than it has ones. For example, the integer 9, when written in binary form, is 1001. 1001 has two zeroes and two ones; thus, 9 is a round number. The integer 26 is 11010 in binary; since it has two zeroes and three ones,
it is not a round number.

Obviously, it takes cows a while to convert numbers to binary, so the winner takes a while to determine. Bessie wants to cheat and thinks she can do that if she knows how many "round numbers" are in a given range.

Help her by writing a program that tells how many round numbers appear in the inclusive range given by the input (1 ≤
Start < Finish ≤ 2,000,000,000).

Input

Line 1: Two space-separated integers, respectively
Start
and Finish.

Output

Line 1: A single integer that is the count of round numbers in the inclusive range
Start..Finish

Sample Input

2 12

Sample Output

6

Source

USACO 2006 November Silver

输入两个十进制正整数a和b,求闭区间 [a ,b]
内有多少个Round number

所谓的Round Number就是把一个十进制数转换为一个无符号二进制数,若该二进制数中0的个数大于等于1的个数,则它就是一个Round

第一次独立完成数位dp,后来发现网上都是用递归做的,显然速度上没有我的优,很开森

dp[i][j][k][l]:对于长度为i的二进制数,最高位为j,有k个0,l个1时有多少个数字

#include<map>
#include<string>
#include<cstring>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<queue>
#include<vector>
#include<iostream>
#include<algorithm>
#include<bitset>
#include<climits>
#include<list>
#include<iomanip>
#include<stack>
#include<set>
using namespace std;
int dp[40][2][40][40];
int bit[40],tail;
void cg(int x)
{
	tail=0;
	for(int i=0;x>>i;i++)
		bit[tail++]=(x>>i)&1;
}
int work(int x)
{
	if(x==0)
		return 0;
	cg(x);
	int ans=0;
	for(int i=1;i<tail;i++)
		for(int j=(i+1)/2;j<=i;j++)
			ans+=dp[i][1][j][i-j];
	int sum0=0,sum1=0;
	for(int i=tail-1;i>0;i--)
	{
		if(bit[i])
			sum1++;
		else
			sum0++;
		if(bit[i-1])
			for(int j=0;j<=i;j++)
				if(j+sum0>=i-j+sum1)
					ans+=dp[i][0][j][i-j];
	}
	if(bit[0])
		sum1++;
	else
		sum0++;
	if(sum0>=sum1)
		ans++;
	return ans;
}
int main()
{
	dp[1][0][1][0]=1;
	dp[1][1][0][1]=1;
	for(int i=1;i<32;i++)
		for(int j=0;j<=i;j++)
			{
				dp[i+1][0][j+1][i-j]+=dp[i][0][j][i-j]+dp[i][1][j][i-j];
				dp[i+1][1][j][i-j+1]+=dp[i][0][j][i-j]+dp[i][1][j][i-j];
			}
	int a,b;
	while(cin>>a>>b)
		cout<<work(b)-work(a-1)<<endl;
}
时间: 2024-10-11 17:14:14

poj3252Round Numbers非递归数位dp解的相关文章

SPOJ BALNUM Balanced Numbers 状压+数位DP

一开始想了一个用二进制状压的方法,发现空间需要的太大,光光memset都要超时 = = 其实不用每次都memset 也可以用三进制,一开始直接打表出所有的状态转移就好 #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream&g

hdu 4722 Good Numbers(初涉数位dp)

http://acm.hdu.edu.cn/showproblem.php?pid=4722 大致题意:若一个整数的各位数字之和是10的倍数,称这个数为"good number".给出区间[A,B],求出该区间内"good number"的数的个数. 第一道数位dp,折腾了半天才明白怎么回事. 设dp[site][mod]表示到第site位(由高位向低位)前面各位数字之和对10取余为mod的数的个数,进行记忆化搜索.有两个很重要的点,首先是变量up,表示是否到达边界

【BZOJ1662】[Usaco2006 Nov]Round Numbers 圆环数 数位DP

[BZOJ1662][Usaco2006 Nov]Round Numbers 圆环数 Description 正如你所知,奶牛们没有手指以至于不能玩"石头剪刀布"来任意地决定例如谁先挤奶的顺序.她们甚至也不能通过仍硬币的方式. 所以她们通过"round number"竞赛的方式.第一头牛选取一个整数,小于20亿.第二头牛也这样选取一个整数.如果这两个数都是 "round numbers",那么第一头牛获胜,否则第二头牛获胜. 如果一个正整数N的二

bzoj 1833: [ZJOI2010]count 数字计数【数位dp】

非典型数位dp 先预处理出f[i][j][k]表示从后往前第i位为j时k的个数,然后把答案转换为ans(r)-ans(l-1),用预处理出的f数组dp出f即可(可能也不是dp吧--) #include<iostream> #include<cstdio> using namespace std; long long l,r,t[25]; struct dp { long long a[15]; dp operator + (dp x) { dp r; for(int i=0;i&l

【POJ3208】传说中POJ最难的数位DP?(正解AC自动机,二解数位DP,吾异与之)

题意: 多组数据,每组求第n个包含'666'的数(不能断开),如1:666,2:1666,14:6667. 题解: AC自动机解法没去想,数位DP没学,这里有一种类似于数位DP,却又与数位DP不同,我称为数位树. 数位树: 将数n如线段树一样地拆分成多个小段,进行递归处理得出答案. 本题详(lue)解: 直接看每一位应该是什么数,然后n减去相应的数,使得在下一层转换为子问题"在开头有b个连续的6时,求第a个带'666'的数".就是如此简单,如此简单!!!! 代码来啦! #include

CodeForces 55D Beautiful numbers(数位dp&amp;&amp;离散化)

题目链接:[kuangbin带你飞]专题十五 数位DP A - Beautiful numbers 题意 ps:第一道数位dp,题真好,虽然是参考大牛方法悟过才a,但仍收获不少. 求一个区间内的Beautiful numbers有多少个.Beautiful numbers指:一个数能整除所有组成它的非0数字. 例如15可以被1和5整除,所以15是Beautiful numbers. 思路 Beautiful numbers指:一个数能整除所有组成它的非0数字. 等同于 一个数能整除 所有组成它的

LightOJ 1205 - Palindromic Numbers (数位dp)

LightOJ 1205 - Palindromic Numbers (数位dp) ACM 题目地址:SPOJ MYQ10 Mirror Number 题意: 求[a,b]中回文的个数. 分析: 是SPOJ MYQ01的简单版...其实有非递归方法的. 代码: /* * Author: illuz <iilluzen[at]gmail.com> * Blog: http://blog.csdn.net/hcbbt * File: 1205.cpp * Create Date: 2014-08-

lightOJ 1205(Palindromic Numbers数位DP)

Palindromic Numbers Time Limit: 2000MS Memory Limit: 32768KB 64bit IO Format: %lld & %llu Submit Status Description A palindromic number or numeral palindrome is a 'symmetrical' number like 16461 that remains the same when its digits are reversed. In

CodeForces 55D Beautiful numbers 数位DP+数学

题意大概是,判断一个正整数区间内有多少个整数能被它自身的每一个非零的数字整除. 因为每一个位置上的整数集s = {0,1,2,3,4,5,6,7,8,9} lcm(s) = 2520 现在有一个整数t是由s中一个或者多个数字构成的,记为abcde,显然t = a*10^4+b*10^3+c*10^2+d*10^1+e 要使得t能被a,b,c,d,e整除,必然有t % lcm(a,b,c,d,e) = 0 因为a,b,c,d,e去重之后一定是s的一个子集,所以lcm(s)一定是lcm(a,b,c,