题意:

  求给定区间[X,Y]中满足下列条件的整数个数:这个数恰好等于K个互不相等的,B的整数次幂之和。例如,设X=15,Y=20,K=2,B=2,则有且仅有下列三个数满足了要求:  17 = 24+20, 18 = 24+21, 20 = 24+22。(以B为底数,幂次数不允许相同)

  参考论文--》》论文中的题。

思路:

  论文倒是容易看明白,但是这个转成B进制的思想一直转不过来。其实转成B进制后变成 a1*Bn+a2*Bn-1...an*B0。其中ai是系数。范围是[0,B-1]。但是看了论文知道,里面画的那棵01树(树上的01就是代表系数a),只有从根走到叶子,经过的1的个数为K才是满足要求的。那么如果a大于0怎么办?那么从树上该点开始的整棵子树就可以全部进行考虑了。而如果刚好考虑的位为1的呢?那么取该位为0的那棵子树就行了。

  两种实现

 //#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <algorithm>
#include <vector>
#include <iostream>
#define pii pair<int,int>
#define INF 0x7f3f3f3f
#define LL long long
using namespace std;
const double PI = acos(-1.0);
const int N=; //注意大小 int f[N][N]; void pre_cal() //预处理组合数
{
f[][]=;
for(int i=; i<N; i++) //位数
{
f[i][]=f[i][i]=;
for(int j=; j<i; j++) //多少个1
{
f[i][j]=f[i-][j]+f[i-][j-];
}
}
} int bit[N];
int cal(int n,int k,int b)
{
memset(bit, , sizeof(bit));
int len=, cnt=, ans=;
while(n) //转成b进制
{
bit[++len]=n%b;
n/=b;
}
for(int i=len; i>; i--)
{
if(bit[i]>)
{
ans+=f[i][k-cnt]; //取整棵子树
break;
}
else if( bit[i]== )
{
ans+=f[i-][k-cnt]; //统计左边的
if(++cnt>k) break; //已超
}
}
if(cnt==k) ans++;
return ans;
} int main()
{
//freopen("input.txt","r",stdin);
pre_cal();
int x, y, k, b;
while(~scanf("%d%d%d%d",&x,&y,&k,&b))
printf("%d\n", cal(y,k,b)-cal(x-,k,b));
return ;
}

AC代码

 //#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <map>
#include <algorithm>
#include <vector>
#include <iostream>
#define pii pair<int,int>
#define INF 0x7f3f3f3f
#define LL long long
using namespace std;
const double PI = acos(-1.0);
const int N=; int f[N][N];
void pre_cal() //预处理组合数
{
f[][]=;
for(int i=; i<N; i++) //位数
{
f[i][]=f[i][i]=;
for(int j=; j<i; j++) //多少个1
{
f[i][j]=f[i-][j]+f[i-][j-];
}
}
}
int bit[N];
int cal(int n,int k,int b)
{
memset(bit, , sizeof(bit));
int len=, cnt=, ans=, flag=;
while(n) //转成b进制
{
bit[++len]=n%b;
n/=b;
if(bit[len]>) flag=;
} if(flag==)
{
//找到第一位大于1的,改为1,然后后面可以全部改成1了
for(int i=len; i>; i--)
if(bit[i]>)
{
for(int j=i; j>; j--) bit[j]=;
break;
}
} for(int i=len; i>; i--)
{
if( bit[i] )
{
ans+=f[i-][k-cnt]; //统计左边的
if(++cnt>k) break; //已超
}
}
if(cnt==k) ans++;
return ans;
} int main()
{
//freopen("input.txt","r",stdin);
pre_cal();
int x, y, k, b;
while(~scanf("%d%d%d%d",&x,&y,&k,&b))
printf("%d\n", cal(y,k,b)-cal(x-,k,b));
return ;
}

AC代码

URAL 1057 Amount of Degrees (数位DP,入门)的更多相关文章

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

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

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

  3. Ural 1057 Amount of Degrees

    Description 问[L,R]中有多少能表示k个b次幂之和. Sol 数位DP. 当2进制时. 建出一个二叉树, \(f[i][j]\) 表示长度为 \(i\) 有 \(j\) 个1的个数. 递 ...

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

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

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

  6. ural 1057 Amount of degrees 【数位dp】

    题意:求(x--y)区间转化为 c 进制 1 的个数为 k 的数的出现次数. 分析:发现其满足区间减法,所以能够求直接求0---x 的转化为 c 进制中 1 的个数为k的数的出现次数. 首先用一个数组 ...

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

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

  9. xbz分组题B 吉利数字 数位dp入门

    B吉利数字时限:1s [题目描述]算卦大湿biboyouyun最近得出一个神奇的结论,如果一个数字,它的各个数位相加能够被10整除,则称它为吉利数.现在叫你计算某个区间内有多少个吉利数字. [输入]第 ...

随机推荐

  1. iView之清空选择框

    Form表单布局的vue组件,已经增加了校验选择框,判断为空的情况下不调用接口. 后来发现,选择了选择框后,清空,再点查询,还是会调接口,看日志发现传了原来清空的值过来,实际上没有清空. 这里增加on ...

  2. 利用oracle session context 向oracle传值

    有时候,我们在执行数据库请求时,需要向数据库传一些应用程序的上下文信息,比如当前应用的用户.举个场景,我们要通过触发器记录对某些关键表的修改日志,日志包括修改的表,字段,字段的值,修改的时间,当然非常 ...

  3. JAVA基础学习-集合三-Map、HashMap,TreeMap与常用API

    森林森 一份耕耘,一份收获 博客园 首页 新随笔 联系 管理 订阅 随笔- 397  文章- 0  评论- 78  JAVA基础学习day16--集合三-Map.HashMap,TreeMap与常用A ...

  4. EntityFramework数据库配置(code frist)

    什么也不说先贴代码 <?xml version="1.0" encoding="utf-8"?> <configuration> < ...

  5. 权限验证MVC

    http://www.jb51.net/article/20147.htm  引用 <authentication mode="Forms"><!--权限受到阻碍 ...

  6. 蓝桥杯T37(nim博弈)

    题目链接:http://lx.lanqiao.cn/problem.page?gpid=T37 题意:中文题诶- 思路:nim博弈 个人感觉这题最难的地方是将题目转换为博弈模型,如果能将之转换为博弈模 ...

  7. bzoj 2535: [Noi2010]Plane 航空管制2【拓扑排序+堆】

    有个容易混的概念就是第一问的答案不是k[i]字典序最小即可,是要求k[i]大的尽量靠后,因为这里前面选的时候是对后面有影响的(比如两条链a->b c->d,ka=4,kb=2,kc=3,k ...

  8. 能量项链 洛谷P1063

    1154 能量项链 2006年NOIP全国联赛提高组 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题解 题目描述 Description 在Mars星球上,每个M ...

  9. 剑指Offer的学习笔记(C#篇)-- 数组中重复的数字

    题目描述 在一个长度为n的数组里的所有数字都在0到n-1的范围内. 数组中某些数字是重复的,但不知道有几个数字是重复的.也不知道每个数字重复几次.请找出数组中任意一个重复的数字. 例如,如果输入长度为 ...

  10. python——函数重点总结

    参数的分类 形参:位置参数.默认参数.*args.命名关键字参数.**kwargs 实参:位置参数.关键字参数 命名关键字参数:定义在*后面的位置参数和默认参数叫作命名关键字参数:用来限制实参必须以关 ...