题目描述

求给定区间 [X,Y] 中满足下列条件的整数个数:这个数恰好等于 KK 个互不相等的 BB 的整数次幂之和。例如,设 X=15,Y=20,K=2,B=2,则有且仅有下列三个数满足题意

输入格式

第一行包含两个整数 X 和 Y,接下来两行包含整数 K 和 B。

输出格式

只包含一个整数,表示满足条件的数的个数。

样例

样例输入

15 20
2
2

样例输出

3

数据范围与提示

对于全部数据,1≤X≤Y≤2^31−1,1≤K≤20,2≤B≤10。

______________________________________________________________________________________________

数位动归

一类区间统计问题,区间较大,无法用暴力求解或组合数学求解。

常见为求区间内满足某类条件的X进制数的个数。条件往往与数位有关。

此题:

首先,区间具有相减的性质。count[i...j]=count[ 0...j ] - count[ 0...i-1 ]。

这样问题就变成了0到i的x进制数有多少个满足条件。

因为是互不相等的k进制数,所以,转成k进制后,每个位上要么是0要么是1。

这样f[i][j]表示长度I的2进制数,有j个1的种类

f[i][j]=f[i-1][j]+f[i-1][j-1]

______________________________________________________________________________________________

 1 #include<bits/stdc++.h>
2 using namespace std;
3 int x,y,k,b;
4 int wei[32];
5 int f[32][32];
6 void prech(int &x,int b)
7 {
8 int js=0;
9 while(x>0)
10 {
11 wei[++js]=x%b;
12 x/=b;
13 }
14 while(js>0)
15 {
16 if(wei[js]==1)x^=1<<(js-1);
17 else if(wei[js]>1)
18 {
19 for(int i=js;i>0;--i)x^=1<<(i-1);
20 break;
21 }
22 --js;
23 }
24 }
25 void dp()
26 {
27 f[0][0]=1;
28 for(int i=1;i<=31;++i)
29 {
30 f[i][0]=f[i-1][0];
31 for(int j=1;j<=i;++j)f[i][j]=f[i-1][j]+f[i-1][j-1];
32 }
33 }
34 int work(int x,int k)
35 {
36 int js=0,ans=0;
37 for(int i=31;i>0;--i)
38 {
39 if(x&(1<<i))
40 {
41 ++js;
42 if(js>k)break;
43 x=x^(1<<i);
44 }
45 if((1<<(i-1))&x)ans+=f[i-1][k-js];
46 }
47 if(js+x==k)ans++;
48 return ans;
49 }
50 int main()
51 {
52 scanf("%d%d%d%d",&x,&y,&k,&b);
53 x--;
54 prech(x,b);prech(y,b);
55 dp();
56 cout<<work(y,k)-work(x,k);
57 return 0;
58 }

LOJ10163 Amount of Degrees的更多相关文章

  1. 【数位DP】[LOJ10163]Amount of Degrees

    发现自己以前对数位DP其实一窍不通... 这题可以做一个很简单的转换:一个数如果在$b$进制下是一个01串,且1的个数恰好有k个,那么这个数就是合法的(刚开始没判断必定是01串,只判断了1的个数竟然有 ...

  2. [TimusOJ1057]Amount of Degrees

    [TimusOJ1057]Amount of Degrees 试题描述 Create a code to determine the amount of integers, lying in the ...

  3. 一本通1585【例 1】Amount of Degrees

    1585: [例 1]Amount of Degrees 时间限制: 1000 ms         内存限制: 524288 KB 题目描述 原题来自:NEERC 2000 Central Subr ...

  4. Timus Online Judge 1057. Amount of Degrees(数位dp)

    1057. Amount of Degrees Time limit: 1.0 second Memory limit: 64 MB Create a code to determine the am ...

  5. [ACM] ural 1057 Amount of degrees (数位统计)

    1057. Amount of Degrees Time limit: 1.0 second Memory limit: 64 MB Create a code to determine the am ...

  6. Ural Amount of Degrees(数位dp)

    传送门 Amount of Degrees Time limit: 1.0 secondMemory limit: 64 MB Description Create a code to determi ...

  7. 【算法•日更•第十二期】信息奥赛一本通1585:【例 1】Amount of Degrees题解

    废话不多说,直接上题: 1585: [例 1]Amount of Degrees 时间限制: 1000 ms         内存限制: 524288 KB提交数: 130     通过数: 68 [ ...

  8. [ural1057][Amount of Degrees] (数位dp+进制模型)

    Discription Create a code to determine the amount of integers, lying in the set [X; Y] and being a s ...

  9. URAL 1057 Amount of Degrees (数位dp)

    Create a code to determine the amount of integers, lying in the set [X;Y] and being a sum of exactly ...

随机推荐

  1. [NOIP2013 提高组] 货车运输

    前言 使用算法:堆优化 \(prim\) , \(LCA\) . 题意 共有 \(n\) 个点,有 \(m\) 条边来连接这些点,每条边有权值.有 \(q\) 条类似于 \(u\) \(v\) 询问, ...

  2. C#——线程总结

    #线程详解 1. Thread基础之从 WinDbg 角度理解你必须知道的时间和空间上的开销 一:空间上的开销 1.thread本身来说就是操作系统的概念... <1> thread的内核 ...

  3. tcp聊天

    package tcp; import java.io.IOException; import java.io.InputStream; import java.net.ServerSocket; i ...

  4. 自定义ClassLoader的使用

    1 import java.io.ByteArrayOutputStream; 2 import java.io.File; 3 import java.io.FileInputStream; 4 i ...

  5. 切换用户后whoami打印用户的问题

    问题: 为何第二个whoami打印的还是root? root@localhost /]# [root@localhost /]# [root@localhost /]# more test.sh #! ...

  6. Nginx集成Naxsi防火墙

    前言 因工作原因,接触到了WAF,今天部署了一下Naxsi,记录一下 GitHub 正文 环境 Centos 7 下载 更新yum yum update -y 安装必要依赖 yum install g ...

  7. Tomcat 配置Vue history模式

    Tomcat 配置Vue  history模式 近日 , 在使用 Tomcat 部署Vue项目时 , 刷新项目出现404的异常 . 原因是 Vue使用了history模式 , 而tomcat没有相关配 ...

  8. JVM-03

    目录 1.1 新生代垃圾收集器 1.1.1 Serial 垃圾收集器(单线程) 1.1.2 ParNew 垃圾收集器(多线程) 1.1.3 Parallel Scavenge 垃圾收集器(多线程) 2 ...

  9. Java多线程-锁的区别与使用

    目录 锁类型 可中断锁 公平锁/非公平锁 可重入锁 独享锁/共享锁 互斥锁/读写锁 乐观锁/悲观锁 分段锁 偏向锁/轻量级锁/重量级锁 自旋锁 Synchronized与Static Synchron ...

  10. 实现strStr

    Implement strStr(). Return the index of the first occurrence of needle in haystack, or -1 if needle ...