Discription

Create a code to determine the amount of integers, lying in the set [XY] and being a sum of exactly K different integer degrees of B.
Example. Let X=15, Y=20, K=2, B=2. By this example 3 numbers are the sum of exactly two integer degrees of number 2:
17 = 2 4+2 0
18 = 2 4+2 1
20 = 2 4+2 2.

Input

The first line of input contains integers X and Y, separated with a space (1 ≤  X ≤  Y ≤ 2 31−1). The next two lines contain integersK and B (1 ≤  K ≤ 20; 2 ≤  B ≤ 10).

Output

Output should contain a single integer — the amount of integers, lying between X and Y, being a sum of exactly K different integer degrees of B.

Example

input output
15 20
2
2
3

Solution

中文大意:给定你一个区间[X,Y],问区间内可以表示为k个b的次方和的数有多少

可以套用进制模型,k个b的次方的和,转化为b进制后即含有k个1的b进制数(当然,其余为0,因为正好是幂的和,所以只可能包含了0或1)

先考虑简单的二进制情况,按照套路,我们可以对数的个数预处理一下

设f[i][j]表示i长度的二进制数正好含有j个1的个数

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

处理b进制中大于1的情况:把求出的b进制串从高到低枚举,第一个大于1的位视为1,之后也全部视为1

之后把b进制视为2进制求解即可(满足区间减法,相减一下就行,当然因为算的时候都没有考虑这个限制数本身,所以用Ans(Y+1)-Ans(x)才能求出正解)

#include<stdio.h>
int f[][],n,m,_k,_b,len,zt[];
void bin_P() {
for(int i=; i<=; i++) {
f[i][]=,f[i][]=i;
for(int j=; j<=i; j++)
f[i][j]=f[i-][j-]+f[i-][j]; } }
int getans(int x) {
for(len=; x; x/=_b)
zt[++len]=x%_b;
int res=,q=_k;
for(int i=len; i; i--)
if(zt[i]) {
if(zt[i]>)
return res+f[i-][q-]+f[i-][q];
else res+=f[i-][q],q--;
if(q<)return res; }
return res; }
int main() {
bin_P();
scanf("%d%d%d%d",&n,&m,&_k,&_b);
printf("%d\n",getans(m+)-getans(n));
return ; }

[ural1057][Amount of Degrees] (数位dp+进制模型)的更多相关文章

  1. Ural1057 - Amount of Degrees(数位DP)

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

  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. Balanced Numbers (数位dp+三进制)

    SPOJ - BALNUM 题意: Balanced Numbers:数位上的偶数出现奇数次,数位上的奇数出现偶数次(比如2334, 2出现1次,4出现1次,3出现两次,所以2334是 Balance ...

  7. SPOJ - BALNUM Balanced Numbers(数位dp+三进制状压)

    Balanced Numbers Balanced numbers have been used by mathematicians for centuries. A positive integer ...

  8. ural1057 Amount of Degrees

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

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

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

随机推荐

  1. spring-boot-configuration-processor的作用

    spring默认使用yml中的配置,但有时候要用传统的xml或properties配置,就需要使用spring-boot-configuration-processor了 先引入pom依赖 <d ...

  2. foxmail地址簿导入thunderbird的乱码问题 (转载)

    转自:http://blog.csdn.net/gexueyuan/article/details/9032595 由于foxmail的地址簿格式和thunderbird的格式不一样,另外也存在编码问 ...

  3. uml图六种箭头的含义(转载)

    在看一些技术博客的时候,经常会见到博客里画上很多uml图.因为经常会被这几种表达关系的箭头搞混,这里我就把常见的6种箭头表达的含义理一下. 泛化 概念:泛化是一种一般与特殊.一般与具体之间关系的描述, ...

  4. Python机器学习算法 — 支持向量机(SVM)

    SVM--简介 <α∗j<C,可得:          构造决策函数:  5.求最优解         要求解的最优化问题如下:          考虑使用序列最小最优化算法(SMO,se ...

  5. bzoj 3401: [Usaco2009 Mar]Look Up 仰望【单调栈】

    用单调递减的栈从后往前扫一遍即可 #include<iostream> #include<cstdio> using namespace std; const int N=10 ...

  6. noip2002矩阵覆盖(搜索)

    矩阵覆盖 题目描述 在平面上有 n 个点(n <= 50),每个点用一对整数坐标表示.例如:当 n=4 时,4个点的坐标分另为:p1(1,1),p2(2,2),p3(3,6),P4(0,7),见 ...

  7. linux C编程 Makefile的使用

    Makefile的作用就是"自动化编译" 一.Makefile基本规则 下面给出几个简单实例: 第一步:分别用vim创建prog.c code.c code.h三个文件 prog. ...

  8. DP BestCoder Round #50 (div.2) 1003 The mook jong

    题目传送门 /* DP:这题赤裸裸的dp,dp[i][1/0]表示第i块板放木桩和不放木桩的方案数.状态转移方程: dp[i][1] = dp[i-3][1] + dp[i-3][0] + 1; dp ...

  9. ACM_闹钟人生(水题)

    闹钟人生 Time Limit: 2000/1000ms (Java/Others) Problem Description: 已知一个时钟一开始指向0点,顺时针走了n个小时,求它最终所指向的数字(时 ...

  10. 全面学习ORACLE Scheduler特性(12)使用Windows和Window Groups

    七.使用Windows 此Windows非彼Windows,通常说的Windows是指盖首富的操作系统,而此处所说的Windows,是指SCHEDULER特性中的一个子项.在SCHEDULER中,WI ...