Codeforces Gym 100231L Intervals 数位DP
Intervals
题目连接:
http://codeforces.com/gym/100231/attachments
Description
Start with an integer, N0, which is greater than 0. Let N1 be the number of ones in the binary representation of N0. So, if N0 = 27, N1 = 4. For all i > 0, let Ni be the number of ones in the binary
representation of Ni−1. This sequence will always converge to one. For any starting number, N0, let K be the minimum value of i ≥ 0 for which Ni = 1. For example, if N0 = 31, then N1 = 5, N2 = 2, N3 = 1, so K = 3. Given a range of consecutive numbers, and a value X, how many numbers in the range have a K value equal to X?
Input
There will be several test cases in the input. Each test case will consist of three integers on a single line:
l, r, X, where l and r (1 ≤ l ≤ r ≤ 1018) are the lower and upper limits of a range of integers, and X
(0 ≤ X ≤ 10) is the target value for K. The input will end with a line with three 0s.
Output
For each test case, output a single integer, representing the number of integers in the range from l to
r (inclusive) which have a K value equal to X in the input. Print each integer on its own line with no
spaces. Do not print any blank lines between answers.
Sample Input
31 31 3
31 31 1
27 31 1
27 31 2
1023 1025 1
1023 1025 2
0 0 0
Sample Output
1
0
0
3
1
1
Hint
题意
首先给你Ni的定义,表示第几轮的时候,这个数是多少,Ni = Ni-1二进制表示下的1的个数
k 表示第几步的时候,Ni = 1
给你l,r,x
问你在l,r区间内,k等于x的数有多少个
题解:
我们首先预处理vis[i]表示有i个1的时候的步数,这个用dp很容易解决
然后我们就可以数位dp去做了,做[1,x]里面二进制数为k个的数量
注意特判1的情况,比较麻烦
代码
#include<bits/stdc++.h>
using namespace std;
long long l,r,t;
int vis[100];
long long ans = 0;
int getone(long long x)
{
int c=0;
while(x>0)
{
if((x&1)==1)
c++;
x>>=1;
}
return c;
}
long long f[70][70];
void init()
{
memset(f,0,sizeof(f));
f[0][0] = 1LL;
for(int i=1;i<=62;i++)
{
f[i][0] = 1LL;
for(int j=1;j<=i;j++)
{
f[i][j] = f[i-1][j-1] + f[i-1][j];
}
}
}
long long calc(long long x,int k)
{
int tot = 0;
long long ans = 0;
for(long long i=62;i>0;i--)
{
if(x&(1LL<<i))
{
tot++;
if(tot>k) break;
x ^= (1LL<<i);
}
if((1LL<<(i-1LL))<=x)
{
if(k>=tot)
ans += f[i-1][k-tot];
}
}
if(tot + x == k) ans++;
return ans;
}
long long solve(long long limit,int x)
{
ans=0;
for(int i=1;i<=61;i++)
if(vis[i]==x)
{
if(i==1)
ans--;
ans+=calc(limit,i);
}
return ans;
}
int main()
{
init();
vis[1]=1;
for(int i=2;i<=61;i++)
vis[i]=vis[getone(i)]+1;
while(scanf("%lld%lld%d",&l,&r,&t)!=EOF)
{
if(l==0&&r==0&&t==0)return 0;
if(t==0)
{
if(l==1)
printf("1\n");
else
printf("0\n");
continue;
}
if(t==1)
{
if(l==1)
printf("%lld\n",solve(r,t)-solve(l-1,t)-1);
else
printf("%lld\n",solve(r,t)-solve(l-1,t));
}
else
printf("%lld\n",solve(r,t)-solve(l-1,t));
}
}
Codeforces Gym 100231L Intervals 数位DP的更多相关文章
- 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)
D. Beautiful numbers time limit per test 4 seconds memory limit per test 256 megabytes input standar ...
- Codeforces - 55D Beautiful numbers (数位dp+数论)
题意:求[L,R](1<=L<=R<=9e18)区间中所有能被自己数位上的非零数整除的数的个数 分析:丛数据量可以分析出是用数位dp求解,区间个数可以转化为sum(R)-sum(L- ...
- CodeForces - 55D - Beautiful numbers(数位DP,离散化)
链接: https://vjudge.net/problem/CodeForces-55D 题意: Volodya is an odd boy and his taste is strange as ...
- Codeforces Gym 100231B Intervals 线段树+二分+贪心
Intervals 题目连接: http://codeforces.com/gym/100231/attachments Description 给你n个区间,告诉你每个区间内都有ci个数 然后你需要 ...
- CodeForces 628D Magic Numbers (数位dp)
题意:找到[a, b]符合下列要求的数的个数. 1.该数字能被m整除 2.该数字奇数位全不为d,偶数位全为d 分析: 1.dp[当前的位数][截止到当前位所形成的数对m取余的结果][当前数位上的数字是 ...
- FZU2179/Codeforces 55D beautiful number 数位DP
题目大意: 求 1(m)到n直接有多少个数字x满足 x可以整出这个数字的每一位上的数字 思路: 整除每一位.只需要整除每一位的lcm即可 但是数字太大,dp状态怎么表示呢 发现 1~9的LCM 是2 ...
- CodeForces - 55D Beautiful numbers —— 数位DP
题目链接:https://vjudge.net/problem/CodeForces-55D D. Beautiful numbers time limit per test 4 seconds me ...
- Codeforces 981 D.Bookshelves(数位DP)
Codeforces 981 D.Bookshelves 题目大意: 给n个数,将这n个数分为k段,(n,k<=50)分别对每一段求和,再将每个求和的结果做与运算(&).求最终结果的最大 ...
随机推荐
- free-jqGrid
PM> Install-Package free-jqGrid jqGrid 是一个用来显示网格数据的jQuery插件,通过使用jqGrid可以轻松实现前端页面与后台数据的ajax异步通信.文档 ...
- Java并发编程-synchronized
多线程的同步机制对资源进行加锁,使得在同一个时间,只有一个线程可以进行操作,同步用以解决多个线程同时访问时可能出现的问题.同步机制可以使用synchronized关键字实现.synchronized关 ...
- Dyslexic Gollum
题意: 求长度是n的二进制串中,不含长度大于等于k的回文串的个数 分析: dp[i][j][k]表示长度i,后11位状态是j不含长度大于等于k的回文串的个数(因为k最大是10,所把后11位状态压缩,d ...
- python27+django1.9添加api
我们进入Python的交互 shell 并使用Django提供的API.要进入Python shell,使用python manage.py shell 使用这个而不是简单的输入"pytho ...
- SQLite数据库和JPA简单介绍
SQLite数据库和JPA简单介绍 一.SQLite简单使用 SQLite是遵循ACID的关系数据库管理系统,它的处理速度很快,它的设计目标是嵌入式的,只需要几百K的内存就可以了. 1.下载SQLit ...
- python中函数总结之装饰器闭包
1.前言 函数也是一个对象,从而可以增加属性,使用句点来表示属性. 如果内部函数的定义包含了在外部函数中定义的对象的引用(外部对象可以是在外部函数之外),那么内部函数被称之为闭包. 2.装饰器 装饰器 ...
- The remote SSH server rejected X11 forwarding request
两台相同的虚拟机,一台没有错误,一个经常出现警告,内容如下所示: The remote SSH server rejected X11 forwarding request 找了很多方法,最后发现是安 ...
- Window Redis分布式部署方案 java
Redis分布式部署方案 Window 1. 基本介绍 首先redis官方是没有提供window下的版本, 是window配合发布的.因现阶段项目需求,所以研究部署的是window版本的,其实都 ...
- 200 OK (from cache) 与 304 Not Modified
解释: 200 OK (from cache) 是浏览器没有跟服务器确认,直接用了浏览器缓存: 304 Not Modified 是浏览器和服务器多确认了一次缓存有效性,再用的缓存. 触发区别: 2 ...
- 20151227感知机(perceptron)
1 感知机 1.1 感知机定义 感知机是一个二分类的线性分类模型,其生成一个分离超平面将实例的特征向量,输出为+1,-1.导入基于误分类的损失函数,利用梯度下降法对损失函数极小化,从而求得此超平面,该 ...