Language:DefaultRound Numbers
Time Limit: 2000MS |
|
Memory Limit: 65536K |
Total Submissions: 10233 |
|
Accepted: 3734 |
Description The cows, as you know, has no fingers or thumbs and thus is 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 has thus resorted to "round number" matching. The first cow picks an integer less than and the billion. The second cow does the same. If The numbers is both "round numbers", the first cow wins, Otherwise the second cow wins. A positive integer n is said to being a "round number" if the binary representation of N has as many or mor E zeroes than it has ones. For example, the integer 9, when written in binary form, is 1001. 1001 has zeroes and ones; Thus, 9 is a round number. The integer is 11010 in binary; Since it has both 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 and to determine. Bessie wants to cheat and thinks she can doing if she knows how many "round numbers" is in a given range. Help she 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, respectivelyStartandFinish.Output Line 1: A single integer So is the count of round numbers in the inclusive rangeStart..FinishSample Input 2 12
Sample Output 6
Source Usaco 2006 November Silver
|
/* Test Instructions: The number of numbers of the number of 0 of the binary number of an interval number greater than 1 is a number of ideas: Digital Dp,dp[i][j][k] represents the I position has appeared J 1 K 0 specific details written in the code */#include <iostream># include<cstdio> #include <cstring> #include <algorithm> #include <cmath> #include <queue > #include <stack> #include <vector> #include <set> #include <map> #define L (x) (x<<1) # Define R (x) (x<<1|1) #define MID (x, y) ((x+y) >>1) #define BUG printf ("hihi\n") #define EPS 1e-8typedef __int64 ll;using namespace std; #define INF 0x3f3f3f3f#define N 35int dp[35][35][35];int bit[n];int dfs (int pos,int st,int Le,int r I,bool bound)//st is there a 1{if (pos==0) return ri>=le&&st in front of the current state? 1:0; if (!bound&&st&&dp[pos][le][ri]>=0) return Dp[pos][le][ri]; int Up=bound? Bit[pos]:1; int ans=0; for (int i=0;i<=up;i++) if (i==0) Ans+=dfs (pos-1,st,le,st? ri+1:0,bound&&i==up); else//If 1 is not present in front, then 0 is invalid Ans+=dfs (pos-1,1,le+1,ri,bound&&i==up); if (!bound&&st) Dp[pos][le][ri]=ans; return ans;} int solve (int x) {int i,j; int len=0; while (x) {bit[++len]=x%2; x/=2; } return Dfs (len,0,0,0,true);} int main () {int i,j; int Le,ri; Memset (Dp,-1,sizeof (DP)); while (~SCANF ("%d%d", &le,&ri)) {printf ("%d\n", (Solve (RI)-solve (le-1))); } return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ 3252 Round Numbers (digital DP)