POJ 3252 Round Numbers 组合数

本文介绍了一种算法,用于计算给定范围内二进制形式中零的数量大于等于一的数量的正整数(即“圆数”)的数量。通过寻找规律并使用组合数学的方法,实现了高效的计算。

Round Numbers
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 12765 Accepted: 4913

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

题意是求两个数之间  "round number"数的个数

 "round number"是指二进制0个数大于等于1的个数的数

细心找规律。。。

坑点:看讨论区说的longlong不能过,int才能过。。。。没试

#include<stdio.h>
#include<string.h>
#include<math.h>
#include<string>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
#include<set>
#define ll long long
using namespace std;
int myc(int n,int r)
{
	int sum=1;
	for(int i=1;i<=r;i++)
	sum=sum*(n+1-i)/i;
	return sum;
}
int numm(int x)
{
	int a[60],i=0,j,ans=0,len;
	while(x)
	{
		a[i++]=x&1;
		x>>=1;
	}
	len=i;
//	for(i=len-1;i>=0;i--)
//	printf("%d",a[i]);
	for(i=2;i<len;i++)
	{
		for(j=i-1;j>=(i+1)/2;j--)
		{
			ans+=myc(i-1,j);
		}	
	}
//	printf("%d\n",ans);
	int zero=0,one=1;
	for(i=len-2;i>=0;i--)
	{
		if(a[i]&1)
		{
			for(j=i;(j+zero+1>=i-j+one)&&j>=0;j--)
			{
				ans+=myc(i,j);
			}
			one++;
		}
		else
		zero++;
	}
	return ans;
	
}

int main()
{
 int a,b,c,ans,one=0,zero=0;
	scanf("%d %d",&a,&b);
 
 	ans=one=zero=0;
 	c=b;
 	while(c)
 	{
 		if(c&1)
 			one++;
 		else
 			zero++; 
 		c>>=1;
 	}
	ans=numm(b)-numm(a);
	if(zero>=one)
 		ans++;
 	printf("%d\n",ans);
 
return 0;
}



MATLAB主动噪声和振动控制算法——对较大的次级路径变化具有鲁棒性内容概要:本文主要介绍了一种在MATLAB环境下实现的主动噪声和振动控制算法,该算法针对较大的次级路径变化具有较强的鲁棒性。文中详细阐述了算法的设计原理与实现方法,重点决了传统控制系统中因次级路径动态变化导致性能下降的问题。通过引入自适应机制和鲁棒控制策略,提升了系统在复杂环境下的稳定性和控制精度,适用于需要高精度噪声与振动抑制的实际工程场景。此外,文档还列举了多个MATLAB仿真实例及相关科研技术服务内容,涵盖信号处理、智能优化、机器学习等多个交叉领域。; 适合人群:具备一定MATLAB编程基础和控制系统理论知识的科研人员及工程技术人员,尤其适合从事噪声与振动控制、信号处理、自动化等相关领域的研究生和工程师。; 使用场景及目标:①应用于汽车、航空航天、精密仪器等对噪声和振动敏感的工业领域;②用于提升现有主动控制系统对参数变化的适应能力;③为相关科研项目提供算法验证与仿真平台支持; 阅读建议:建议读者结合提供的MATLAB代码进行仿真实验,深入理算法在不同次级路径条件下的响应特性,并可通过调整控制参数进一步探究其鲁棒性边界。同时可参考文档中列出的相关技术案例拓展应用场景。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值