poj3252Round Numbers【组合数】【数位dp】
Round Numbers
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
Output
Sample Input
2 12
Sample Output
6
emmm思路总体差不多 就是先把输入转化为二进制,然后固定0的个数 用组合数做
后来没考虑到数不能超出finish卡了一下 再后来感觉有点想混了
原来好像直接就想 算0 的个数比n的位数的一半多就可以了
但是发现小于n的数里面 边界条件也会变化的
看了题解
思路:
先求位数小于n的roundnumber
尽管排列组合,结果肯定不会超过n的
然后算位数刚好等于n的roundnumber
先固定最高位 因为肯定是1 并且不能变动
往下数 后一位如果是0 那么也不能变动
如果是1 那么假设这一位是0 剩下的位数再对剩余的zero的个数进行排列组合
AC代码【poj用c++会挂】
//#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string.h>
#include <cstring>
#include <cmath>
#define inf 0x3f3f3f3f
#define ll long long
using namespace std;
const int maxn = 35;
int start, finish;
int cntnuma, cntnumb, c[maxn][maxn];
int binarya[maxn], binaryb[maxn];
void Cmn()
{
c[0][0] = c[1][0] = c[1][1] = 1;
for(int i = 2; i < maxn; i++){
c[i][0] = 1;
for(int j = 1; j < i; j++){
c[i][j] = c[i - 1][j] + c[i - 1][j - 1];
}
c[i][i] = 1;
}
}
void digtobinary(int n, int *binary)
{
binary[0] = 0;
while(n){
binary[++binary[0]] = n % 2;
n /= 2;
}
return;
}
int solve(int n, int *binary)
{
//if(n <= 1) return 0;
//int len = digtobinary(n, binary);
int st;
digtobinary(n, binary);
int len = binary[0];
int ans = 0;
//小于len的可以随便填肯定不会超过finish
for(int i = 1; i < len - 1; i++){
//if(i % 2) st = i / 2 + 1;
//else st = i / 2;
for(int j = i / 2 + 1; j <= i; j++){
ans += c[i][j];
}
}
int zero = 0;
//if(len % 2) st = len / 2 + 1;
//else st = len / 2;
for(int i = len - 1; i >= 1; i--){
if(!binary[i]){
zero++;
}
else{
for(int j = (len + 1) / 2 - zero - 1; j <= i - 1; j++){
ans += c[i - 1][j];
}
}
}
return ans;
}
int main()
{
Cmn();
while(scanf("%d%d",&start,&finish)!= EOF){
int ansa = solve(start, binarya);
int ansb = solve(finish + 1, binaryb);
cout<< ansb - ansa<< endl;
}
return 0;
}
poj3252Round Numbers【组合数】【数位dp】的更多相关文章
- BZOJ_3209_花神的数论题_组合数+数位DP
BZOJ_3209_花神的数论题_组合数+数位DP Description 背景 众所周知,花神多年来凭借无边的神力狂虐各大 OJ.OI.CF.TC …… 当然也包括 CH 啦. 描述 话说花神这天又 ...
- Codeforces Round #235 (Div. 2) D. Roman and Numbers (数位dp、状态压缩)
D. Roman and Numbers time limit per test 4 seconds memory limit per test 512 megabytes input standar ...
- CodeForces 55D "Beautiful numbers"(数位DP+离散化处理)
传送门 参考资料: [1]:CodeForces 55D Beautiful numbers(数位dp&&离散化) 我的理解: 起初,我先定义一个三维数组 dp[ i ][ j ][ ...
- Balanced Numbers (数位dp+三进制)
SPOJ - BALNUM 题意: Balanced Numbers:数位上的偶数出现奇数次,数位上的奇数出现偶数次(比如2334, 2出现1次,4出现1次,3出现两次,所以2334是 Balance ...
- Codeforces #55D-Beautiful numbers (数位dp)
D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
- Codeforces 55D. Beautiful numbers(数位DP,离散化)
Codeforces 55D. Beautiful numbers 题意 求[L,R]区间内有多少个数满足:该数能被其每一位数字都整除(如12,24,15等). 思路 一开始以为是数位DP的水题,觉得 ...
- poj3252 Round Numbers(数位dp)
题目传送门 Round Numbers Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 16439 Accepted: 6 ...
- CodeForces 55D Beautiful numbers(数位dp)
数位dp,三个状态,dp[i][j][k],i状态表示位数,j状态表示各个位上数的最小公倍数,k状态表示余数 其中j共有48种状态,最大的是2520,所以状态k最多有2520个状态. #include ...
- 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位dp)
题目链接:https://ac.nowcoder.com/acm/contest/163/J 题目大意:给定一个数N,求区间[1,N]中满足可以整除它各个数位之和的数的个数.(1 ≤ N ≤ 1012 ...
- Codeforces Beta Round #51 D. Beautiful numbers(数位dp)
题目链接:https://codeforces.com/contest/55/problem/D 题目大意:给你一段区间[l,r],要求这段区间中可以整除自己每一位(除0意外)上的数字的整数个数,例如 ...
随机推荐
- linux环境下pytesseract的安装和央行征信中心的登录验证码识别
首先是安装,我参考的是这个 http://blog.csdn.net/xinghun_4/article/details/47860645 我是centos,使用yum yum install pyt ...
- Android 程序打包及签名(转)
为什么要签名??? 开发Android的人这么多,完全有可能大家都把类名,包名起成了一个同样的名字,这时候如何区分?签名这时候就是起区分作用的. 由于开发商可能通过使用相同的Package Name来 ...
- 分表需要解决的问题 & 基于MyBatis 的轻量分表落地方案
分表:垂直拆分.水平拆分 垂直拆分:根据业务将一个表拆分为多个表. 如:将经常和不常访问的字段拆分至不同的表中.由于与业务关系密切,目前的分库分表产品均使用水平拆分方式. 水平拆分:根据分片算法将一个 ...
- MyBatis踩坑记录
在线文档: 动态SQL http://www.mybatis.org/mybatis-3/zh/dynamic-sql.html 1. Error setting null for paramete ...
- Python 爬虫系列:糗事百科最热段子
1.获取糗事百科url http://www.qiushibaike.com/hot/page/2/ 末尾2指第2页 2.分析页面,找到段子部分的位置, 需要一点CSS和HTML的知识 3.编写 ...
- Java最快的maven仓库地址,国内Maven地址,超快的Maven地址
Java最快的maven地址,国内Maven地址,超快的Maven地址 >>>>>>>>>>>>>>>> ...
- CentOS7--系统设置语言环境
设置语言: 系统范围的区域设置存储在/etc/locale.conf文件中,在systemd守护进程提前引导时读取.配置的区域设置/etc/locale.conf由每个服务或用户继承,除非个别程序或个 ...
- [XPath] XPath 与 lxml (二)XPath 语法
XPath 选取节点时使用的表达式是一种路径表达式.节点是通过路径(path)或者步(steps)来选取的. 本章使用以下 XML 文档作为示例. <?xml version="1.0 ...
- linux计划任务之crontab
语法: crontab [ -u user ] file crontab [ -u user ] [ -i ] { -e | -l | -r } 说明: crontab命令 ...
- css笔记 - 张鑫旭css课程笔记之 line-height 篇
一.line-height line-height: 指两行文字基线之间的距离. 行高200px表示两行文字基线之间的距离是200px: 二.基线:baseline 字母x下边缘的位置 基线是任意线定 ...