1585: 【例 1】Amount of Degrees

时间限制: 1000 ms         内存限制: 524288 KB

题目描述

原题来自:NEERC 2000 Central Subregional,题面详见 Ural 1057

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

17=2^4+2^0

18=2^4+2^1

20=2^4+2^2

输入格式

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

输出格式

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

样例

样例输入

15 20
2
2

样例输出

3

数据范围与提示

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

sol:把一个数想象成一个有B进制表示但只有(0,1)组成的数,统计的时候把n拆成B进制,用组合数计算一下,如果那位数字>1的话就退出,如果=1的话就对于那位填(0,1)讨论一下

#include <bits/stdc++.h>
using namespace std;
inline int read()
{
int s=;
bool f=;
char ch=' ';
while(!isdigit(ch))
{
f|=(ch=='-');
ch=getchar();
}
while(isdigit(ch))
{
s=(s<<)+(s<<)+(ch^);
ch=getchar();
}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(int x)
{
if(x<)
{
putchar('-');
x=-x;
}
if(x<)
{
putchar(x+'');
return;
}
write(x/);
putchar((x%)+'');
return;
}
inline void writeln(int x)
{
write(x);
putchar('\n');
return;
}
#define W(x) write(x),putchar(' ')
#define Wl(x) writeln(x)
int l,r,k,B;
int C[][];
inline void Init()
{
int i,j;
for(i=;i<=;i++)
{
C[i][]=;
for(j=;j<=i;j++)
{
C[i][j]=C[i-][j]+C[i-][j-];
}
}
return;
}
/*
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
*/
inline int Solve(int n)
{
int i,Len=,ans=,Now_k=k;
int a[];
while(n)
{
a[++Len]=n%B;
n/=B;
}
for(i=Len;i>=;i--)
{
if(a[i]==)
{
ans+=C[i-][Now_k];
Now_k--;
}
else if(a[i]>)
{
ans+=C[i][Now_k];
break;
}
if(Now_k<)return ans;
}
if(Now_k==) ans++;
return ans;
}
int main()
{
int i;
Init();
R(l); R(r); R(k); R(B);
Wl(Solve(r)-Solve(l-));
return ;
}
/*
input
15 20
2
2
output
3
*/

一本通1585【例 1】Amount of Degrees的更多相关文章

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

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

  2. [TimusOJ1057]Amount of Degrees

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

  3. 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 ...

  4. [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 ...

  5. Ural Amount of Degrees(数位dp)

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

  6. [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 ...

  7. 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 ...

  8. Ural1057. Amount of Degrees 题解 数位DP

    题目链接: (请自行百度进Ural然后查看题号为1057的那道题目囧~) 题目大意: Create a code to determine the amount of integers, lying ...

  9. 2018.09.07 Amount of degrees(数位dp)

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

随机推荐

  1. Objective-C ARC下IBOutlet属性是用weak还是strong来修饰

    1.苹果官方说明: From a practical perspective, in iOS and OS X outlets should be defined as declared proper ...

  2. MySQL 基础十一 事件

    1.查看事件 2.创建事件 3.执行事件,并查看执行结果是否正确 一 查看事件 -- 1.查看所有事件(显示执行频率(按年.月.日).创建日期.最后执行事件等)SELECT * FROM mysql. ...

  3. TCP/IP协议---ICMP协议及ping、traceroute

    ICMP Internet控制报文协议通常被认为是IP层的组成部分,一般被IP层或更高层(TCP.UDP)使用.ICMP报文是在IP数据报内部被传输的.如图: ICMP报文的格式如下: 报文的前4个字 ...

  4. vue 动态创建组件(运行时创建组件)

    function mountCmp (cmp, props, parent) { if (cmp.default) { cmp = cmp.default } cmp = Vue.extend(cmp ...

  5. [Spark][Python]Wordcount 例子

    [training@localhost ~]$ hdfs dfs -cat cats.txt The cat on the matThe aardvark sat on the sofa[traini ...

  6. Java HTML to PDF 支持SVG

    尝试一 (现用框架的基础上改动,影响最小化) 最早使用的框架 Xhtmlrenderer,需要把HTML转换成XHTML,引入第二个框架Tidy,Tidy与2010年停止更新,github上的项目也停 ...

  7. 仓储层接口IBaseRepository解析

    //代码调用由业务层调用,调用方式详见源代码的业务层,升级直接替换TT模板即可,无需覆盖系统using System; using System.Collections.Generic; using ...

  8. linux系统最小化安装后的初始化脚本

    作为运维人员,经常会初始化系统,系统在安装过程中基本都会选择最小化安装,这样安装好的系统里会缺少很多环境. 下面分享一个系统安装后的初始化脚本: #!/bin/bash #系统时最小化安装的,这里要安 ...

  9. 5 questions

    1.软件开发中有哪几种过程模型? 2.详细设计有哪几种描述方法? 3.什么是需求分析? 4.软件设计的基本原理包括哪些内容? 5.简述文档在软件工程中的作用? 逸翔.

  10. git使用(2)

    1.远程仓库 a SSHKEY 第1步:创建SSH Key.在用户主目录下,看看有没有.ssh目录,如果有,再看看这个目录下有没有id_rsa和id_rsa.pub这两个文件,如果已经有了,可直接跳到 ...