【算法】数位DP

【题解】动态规划

写了预处理函数却忘了调用是一种怎样的体验?

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int f[][];
void init()
{
f[][]=;
for(int i=;i<=;i++)
{
f[i][]=;
for(int j=;j<=i;j++)f[i][j]=f[i-][j]+f[i-][j-];
}
}
int find(int x,int k)
{
int tot=,ans=;
for(int i=;i>;i--)
{
if((<<i)&x)
{
tot++;
if(tot>k)break;//超过k就没有意义了
x^=(<<i);
}
if((<<(i-))<=x)ans+=f[i-][k-tot];
}
if(tot+x==k)ans++;//判断x本身
return ans;
}
int calc(int l,int r,int k)//统计0...x内二进制表示含k个1的数的个数
{
int s,now=;
for(s=;now<k;s++)now+=find(r,s)-find(l-,s);
s--;now=k-now+find(r,s)-find(l-,s);
int left=l;
while(l<r)
{
int mid=(int)(((long long)l+(long long)r)>>);//int+int会范围爆炸
if(find(mid,s)-find(left-,s)>=now)r=mid;else l=mid+;
}
return l;
}
int main()
{
int TT,l,r,k;
init();
scanf("%d",&TT);
while(TT--)
{
scanf("%d%d%d",&l,&r,&k);
if(r<=)
{
if(r==)r--,k--;
l^=(<<),r^=(<<);
printf("%d\n",(<<)|calc(l,r,k));
continue;
}
if(l==)l++,k--;
printf("%d\n",calc(l,r,k));
}
return ;
}

【SPOJ】1182 Sorted bit sequence的更多相关文章

  1. 【SPOJ】NUMOFPAL - Number of Palindromes(Manacher,回文树)

    [SPOJ]NUMOFPAL - Number of Palindromes(Manacher,回文树) 题面 洛谷 求一个串中包含几个回文串 题解 Manacher傻逼题 只是用回文树写写而已.. ...

  2. 【SPOJ】Substrings(后缀自动机)

    [SPOJ]Substrings(后缀自动机) 题面 Vjudge 题意:给定一个长度为\(len\)的串,求出长度为1~len的子串中,出现最多的出现了多少次 题解 出现次数很好处理,就是\(rig ...

  3. 【SPOJ】Longest Common Substring II (后缀自动机)

    [SPOJ]Longest Common Substring II (后缀自动机) 题面 Vjudge 题意:求若干个串的最长公共子串 题解 对于某一个串构建\(SAM\) 每个串依次进行匹配 同时记 ...

  4. 【SPOJ】Longest Common Substring(后缀自动机)

    [SPOJ]Longest Common Substring(后缀自动机) 题面 Vjudge 题意:求两个串的最长公共子串 题解 \(SA\)的做法很简单 不再赘述 对于一个串构建\(SAM\) 另 ...

  5. 【SPOJ】Distinct Substrings(后缀自动机)

    [SPOJ]Distinct Substrings(后缀自动机) 题面 Vjudge 题意:求一个串的不同子串的数量 题解 对于这个串构建后缀自动机之后 我们知道每个串出现的次数就是\(right/e ...

  6. 【SPOJ】Distinct Substrings/New Distinct Substrings(后缀数组)

    [SPOJ]Distinct Substrings/New Distinct Substrings(后缀数组) 题面 Vjudge1 Vjudge2 题解 要求的是串的不同的子串个数 两道一模一样的题 ...

  7. 【SPOJ】Power Modulo Inverted(拓展BSGS)

    [SPOJ]Power Modulo Inverted(拓展BSGS) 题面 洛谷 求最小的\(y\) 满足 \[k\equiv x^y(mod\ z)\] 题解 拓展\(BSGS\)模板题 #inc ...

  8. 【SPOJ】QTREE7(Link-Cut Tree)

    [SPOJ]QTREE7(Link-Cut Tree) 题面 洛谷 Vjudge 题解 和QTREE6的本质是一样的:维护同色联通块 那么,QTREE6同理,对于两种颜色分别维护一棵\(LCT\) 每 ...

  9. 【SPOJ】QTREE6(Link-Cut-Tree)

    [SPOJ]QTREE6(Link-Cut-Tree) 题面 Vjudge 题解 很神奇的一道题目 我们发现点有黑白两种,又是动态加边/删边 不难想到\(LCT\) 最爆力的做法,显然是每次修改单点颜 ...

随机推荐

  1. noauth authentication required redis

    解决方案: 这是出现了认证的问题,是因为设置了认证密码. 127.0.0.1:6379> auth "yourpassword" 例如:

  2. STL--heap概述:make_heap,sort_heap,pop_heap,push_heap

    heap并不属于STL容器组件,它分为 max heap 和min heap,在缺省情况下,max-heap是优先队列(priority queue)的底层实现机制. 而这个实现机制中的max-hea ...

  3. thinkphp5学习记录一

    1 使用composer安装 composer create-project topthink/think=5.0.* tpblog --prefer-dist 2 配置环境vim /usr/loca ...

  4. 【Quartz.net】- Cron表达式

    一.结构 corn从左到右(用空格隔开):秒 分 小时 月份中的日期 月份 星期中的日期 年份 二.各字段的含义   字段 允许值 允许的特殊字符 秒(Seconds) 0~59的整数 , - * / ...

  5. 内核blackhole

    1) 当arp表项不存在的时候,数据包等待表项存在了再发,还是直接把数据包给丢掉; 2)如果网络目的地址不可达,是在那一层把数据丢弃,再是路由层就判断还是arp层呢?

  6. [C/C++] 原码、反码、补码问题

    正确答案:D 解析: C语言中变量以补码形式存放在内存中,正数的补码与原码相同,负数求补码方式为(符号位不变,其余各位取反,最后末尾加1): 32位机器:int 32位,short 16位.  x = ...

  7. [OS] 信号量(Semaphore)

    一个信号量S是一个整型量,除对其初始化外,它只能由两个原子操作P和V来访问.P和V的名称来源于荷兰文proberen(测试)和verhogen(增量),后面亦将P/V操作分别称作wait(), sig ...

  8. codeforces 730 j.bottles

    J. Bottles time limit per test 2 seconds memory limit per test 512 megabytes input standard input ou ...

  9. [洛谷P1714]切蛋糕

    题目大意:给你n个整数,求出其中长度不超过m的最大字段和. 题解:单调队列维护前缀和最小值,然后用当前值减去当前有效最小值即可 C++ Code: #include<cstdio> usi ...

  10. bzoj4390: [Usaco2015 dec]Max Flow(LCA+树上差分)

    题目大意:给出一棵树,n(n<=5w)个节点,k(k<=10w)次修改,每次给定s和t,把s到t的路径上的点权+1,问k次操作后最大点权. 对于每次修改,给s和t的点权+1,给lca(s, ...