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
#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>#include<cmath>usingnamespace std;inlineintgi(){int f =1, x =0;char c =getchar();while(c <'0'|| c >'9'){if(c =='-')
f =-1;
c =getchar();}while(c >='0'&& c <='9'){
x = x *10+ c -'0';
c =getchar();}return f * x;}//以上不解释inlineboolpd(int x)//判断一个数是不是"Round Number"{int y =0, z =0;//y是存二进制数中1的个数,z是存0的个数while(x >0){if(x &1)//如果这一位是0{++y;//y就加1}else{++z;//否则z就加1}
x = x >>1;//x除以2}return z >= y;//这个语句的意思是:如果z>=y,就返回true,否则返回false。}int a, b, sum;//a、b是题目中的意思,sum是答案intmain(){
a =gi(), b =gi();//输入a、bfor(int i = a; i <= b; i++)//从a到b枚举{if(pd(i))//如果i是“Round Number"{++sum;//sum就加一}}printf("%d", sum);//最后输出sumreturn0;//结束}
#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>#include<cmath>usingnamespace std;inlineintgi(){int f =1, x =0;char c =getchar();while(c <'0'|| c >'9'){if(c =='-')
f =-1;
c =getchar();}while(c >='0'&& c <='9'){
x = x *10+ c -'0';
c =getchar();}return f * x;}int dp[35][35][35], wei[35];intdfs(int a,int n0,int n1,int ddd,int flag){if(a ==0){if(flag ==0)//这里不判断flag==0也可以,判不判断的区别在于是不是把0算上,判断就不把0算上了{if(n0 >= n1){return1;}else{return0;}}return0;}if(ddd ==0&& dp[a][n0][n1]!=-1){return dp[a][n0][n1];}int ed = ddd ? wei[a]:1, ans =0, nu0, nu1;for(int i =0; i <= ed; i++){if(flag && i ==0){
nu0 = nu1 =0;}else{if(i ==0){
nu0 = n0 +1, nu1 = n1;}else{
nu0 = n0, nu1 = n1 +1;}}
ans = ans +dfs(a -1, nu0, nu1, ddd && i == ed, flag && i ==0);}if(ddd ==0){
dp[a][n0][n1]= ans;}return ans;}intsolve(int x){int tot =0;while(x){
wei[++tot]= x &1;
x = x >>1;}returndfs(tot,0,0,1,1);}int a, b;intmain(){memset(dp,-1,sizeof(dp));
a =gi(), b =gi();printf("%d\n",solve(b)-solve(a -1));return0;}
动态规划代码:
#include<iostream>#include<cstdio>#include<cstdlib>#include<cstring>#include<algorithm>#include<cmath>usingnamespace std;inlineintgi(){int f =1, x =0;char c =getchar();while(c <'0'|| c >'9'){if(c =='-')
f =-1;
c =getchar();}while(c >='0'&& c <='9'){
x = x *10+ c -'0';
c =getchar();}return f * x;}int dp[40][40];inlineintgetans(int x){int t = x, wei[40], len =0, sum =0, n0 =0, n1 =0;;while(t){
wei[++len]= t &1;
t = t >>1;}for(int i = len -1; i >=1; i--)//这里先把第len位变为0,然后一次枚举最高的位数在第i位{for(int j =0; j <= i -1; j++){if(j >= i - j){
sum = sum + dp[i -1][j];}}}
n1 =1;for(int i = len -1; i >=1; i--)//这里是在第len位为1的情况下进行dp{if(wei[i]==1){if(i ==1){if(n0 +1>= n1){++sum;}}else{for(int j =0; j <= i -1; j++){if(j + n0 +1>= n1 + i -1- j){
sum = sum + dp[i -1][j];}}}++n1;}else{++n0;}}return sum;}int a, b;intmain(){
a =gi(), b =gi();memset(dp,0,sizeof(dp));
dp[1][1]=1, dp[1][0]=1;for(int i =1; i <=32; i++){for(int j =0; j <= i; j++){
dp[i +1][j]= dp[i +1][j]+ dp[i][j], dp[i +1][j +1]= dp[i +1][j +1]+ dp[i][j];}}printf("%d",getans(b +1)-getans(a));return0;}
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core内存结构体系(Windows环境)底层原理浅谈
· C# 深度学习:对抗生成网络(GAN)训练头像生成模型
· .NET 适配 HarmonyOS 进展
· .NET 进程 stackoverflow异常后,还可以接收 TCP 连接请求吗?
· SQL Server统计信息更新会被阻塞或引起会话阻塞吗?
· 传国玉玺易主,ai.com竟然跳转到国产AI
· 本地部署 DeepSeek:小白也能轻松搞定!
· 自己如何在本地电脑从零搭建DeepSeek!手把手教学,快来看看! (建议收藏)
· 我们是如何解决abp身上的几个痛点
· 普通人也能轻松掌握的20个DeepSeek高频提示词(2025版)