题目大意

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


输入:第一行包含两个整数X和Y。接下来两行包含整数K和B。
输出:只包含一个整数,表示满足条件的数的个数。
数据规模:1 ≤ X ≤ Y ≤ 2^31
−1,1 ≤ K ≤ 20,  2 ≤ B ≤ 10。

题解

《浅谈数位类统计问题》的论问题~~~~~人生第一道数位DP,哈哈~~~O(∩_∩)O~~代码纯属抄袭~~~~

代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
using namespace std;
#define MAXN 35
int f[MAXN][MAXN];
void init()
{
f[0][0]=1;
for(int i=1;i<32;i++)
{
f[i][0]=f[i-1][0];
for(int j=1;j<=i;j++)
f[i][j]=f[i-1][j]+f[i-1][j-1];
}
}
int change(int x,int b)
{
int p[MAXN];
int ans=0;
while(x)
{
p[ans++]=x%b;
x/=b;
}
reverse(p,p+ans);
for(int i=0;i<ans;i++)
if(p[i]>1)
{
for(int j=i;j<ans;j++)
p[j]=1;
break;
}
for(int i=0;i<ans;i++)
x=x|(p[ans-i-1]<<i);
return x;
}
int cal(int x,int k)
{
int tot=0,ans=0;
for(int i=31;i>0;i--)
{
if(x&(1<<i))
{
tot++;
if(tot>k) break;
x=x^(1<<i);
}
if((1<<(i-1))<=x)
ans+=f[i-1][k-tot];
}
if(tot+x==k) ans++;
return ans;
}
int main()
{
init();
int x,y,k,b;
while(cin>>x>>y>>k>>b)
{
int X=change(x,b);
int Y=change(y,b);
cout<<cal(Y,k)-cal(X-1,k)<<endl;
}
return 0;
}

Ural1057 - Amount of Degrees(数位DP)的更多相关文章

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

  2. URAL 1057. Amount of Degrees(数位DP)

    题目链接 我看错题了...都是泪啊,不存在3*4^2这种情况...系数必须为1... #include <cstdio> #include <cstring> #include ...

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

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

  4. URAL1057. Amount of Degrees(DP)

    1057 简单的数位DP  刚开始全以2进制来算的 后来发现要找最接近x,y值的那个基于b进制的0,1组合 #include <iostream> #include<cstdio&g ...

  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. ural1057 Amount of Degrees

    链接 这题有一点小坑点 就是AX^B  A只能为0或者1  ,剩下的就比较好做的了. #include <iostream> #include<cstdio> #include ...

  7. ural 1057Amount of Degrees ——数位DP

    link:http://acm.timus.ru/problem.aspx?space=1&num=1057 论文: 浅谈数位类统计问题  刘聪 #include <iostream&g ...

  8. ural1057 Amount of degrees 位数统计

    #include <iostream> #include <string> using namespace std; ][]; void init(){ f[][] =; ;i ...

  9. 数位dp总结

    由简单到稍微难点. 从网上搜了10到数位dp的题目,有几道还是很难想到的,前几道基本都是模板题,供入门用. 点开即可看题解. hdu3555 Bomb hdu3652 B-number hdu2089 ...

随机推荐

  1. python的生成器

    1.生成器 >>> def func1(): ... yield 0 ... yield 1 ... >>> a=func1() >>> a.ne ...

  2. STL的pair学习, map学习

    http://blog.csdn.net/calvin_zcx/article/details/6072286 http://www.linuxidc.com/Linux/2014-10/107621 ...

  3. Java学习--封装、继承、多态

    接下来几天会根据http://www.cnblogs.com/chenssy/category/525010.html中讲解的java内容做个学习笔记,在此感谢一下这位大仙!! 一.封装 对于封装而言 ...

  4. Windows常用CMD命令

    远程桌面:mstsc 记事本:notepad        写字板:write 计算器:calc IIS重启:iisreset 60秒倒计时关机命令:tsshutdn     15秒关机:rononc ...

  5. node.js 1Million concurrent connections!

    https://github.com/ashtuchkin/node-millenium http://blog.caustik.com/2012/08/19/node-js-w1m-concurre ...

  6. <六> jQuery 获得内容和属性

    获得内容 三个简单实用的用于 DOM 操作的 jQuery 方法: text() - 设置或返回所选元素的文本内容 html() - 设置或返回所选元素的内容(包括 HTML 标记) val() - ...

  7. Linux df 命令

    Linux df 命令 df(disk free)功能说明:显示磁盘的相关信息.语 法:df [-ahHiklmPT][--block-size=<区块大小>][-t <文件系统类型 ...

  8. js正则匹配查找

    var pattern1 = /好/g; console.log(pattern1.test("你好")); 字符串查找: var pattern1 = /\w/; console ...

  9. Twitter:蓄水池储水量问题

    早上买了两个饼夹肉,我吃了一个,辣椒粉好多,现在一直在实验室喝水. 一.倒数第n位 今年暑假去世纪佳缘面试,其中一题就是这个,只能遍历一遍链表求出倒数第n位. 答案是两个指针,第一个在头部设为A,第二 ...

  10. HTTP Keep-Alive详解

    HTTP是一个请求<->响应模式的典型范例,即客户端向服务器发送一个请求信息,服务器来响应这个信息.在老的HTTP版本中,每个请求都将被创建一个新的客户端->服务器的连接,在这个连接 ...