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意外)上的数字的整数个数,例如 ...
随机推荐
- 【AI】图像识别-物体检测-百度AI-EasyDL-NodeJS
var https = require('https') var express = require('express'); var app = express(); var bodyParser = ...
- (数字IC)低功耗设计入门(一)——低功耗设计目的与功耗的类型
低功耗设计这个专题整理了好久,有一个月了,有图有证据: 然而最近一直有些烦心事.郁闷事,拖延了一下,虽然现在还是有点烦,但是还是先发表了吧.下面我们就来聊聊低功耗设计吧,由于文章比较长,因此我就不一次 ...
- webform的学习(2)
突然回想一下,两周之后放假回家,三周之后重返学习,四周之后就要真正的面对社会,就这样有好多的舍不得在脑海中回旋,但是又是兴奋的想快点拥有自己的小生活,似乎太多的人在说程序的道路甚是艰难,我不知道我的选 ...
- link常用的作用
1 引入样式 2设置网页标题上面图标
- open-falcon之dashboard\portal说明.md
dashboard 功能 为用户展示监控数据 配置文件 gunicorn.conf - workers,dashboard并发进程数 - bind,dashboard的http监听端口 - proc_ ...
- 【转载】Yui.Compressor高性能ASP.NET开发:自动压缩CSS、JS
在开发中编写的js.css发布的时候,往往需要进行压缩,以减少文件大小,减轻服务器的负担.这就得每次发版本的时候,对js.js进行压缩,然后再发布.有没有什么办法,让代码到了服务器上边,它自己进行压缩 ...
- Android开发训练之第五章第五节——Resolving Cloud Save Conflicts
Resolving Cloud Save Conflicts IN THIS DOCUMENT Get Notified of Conflicts Handle the Simple Cases De ...
- enum hack
关于占用内存的大小,enum类型本身是不占内存的,编译器直接替换.但是enum类型的变量肯定是占内存的. class A{ public: //enum类型本身不占内存 enumEnumTest{ a ...
- xmlWriter
MemoryStream msXml = new MemoryStream(); XmlTextWriter xmlWriter = new XmlTextWriter(msXml, Encoding ...
- UINavigationItem 设置UIBarButtonItem
转:http://hi.baidu.com/ivan_xu/item/237bb1ad77eff9b028ce9d7c 有A.B两个ViewController,假如A push B: UINavig ...