2018.09.07 Amount of degrees(数位dp)
描述
求给定区间[X,Y]中满足下列条件的整数个数:这个数恰好等于K个互不相等的B的整数次幂之和。
例如,设X=15,Y=20,K=2,B=2,则有且仅有下列三个数满足题意:
17 = 24+20,
18 = 24+21,
20 = 24+22。
输入
第一行包含两个整数X和Y。接下来两行包含整数K和B。
输出
只包含一个整数,表示满足条件的数的个数。
样例输入
15 20
2
2
样例输出
3
提示
数据规模:1 ≤ X ≤ Y ≤ 2^31−1,1 ≤ K ≤ 20, 2 ≤ B ≤ 10
数位dp经典题。
感觉快被自己蠢哭了。
这么简单的题居然调了那么久,结果是少打了一个 ~ 2333。。。
代码:
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll l,r,k,b,mul[35],len,num[35],f[35][25];
inline ll dfs(ll pos,ll cnt,ll lim){
if(pos==len+1)return cnt==k;
if((!lim)&&~f[pos][cnt])return f[pos][cnt];
ll tmp=0,up=lim?num[pos]:1;
for(ll i=0;i<=up;++i)if(cnt+i<=k)tmp+=dfs(pos+1,cnt+i,(lim&&(i==up)));
if(!lim)f[pos][cnt]=tmp;
return tmp;
}
inline ll solve(ll x){
if(!x)return 0;
for(len=0;mul[len]<=x;++len);
--len,memset(f,-1,sizeof(f));
for(ll i=len;~i;--i){
if(x>=mul[i])x-=mul[i],num[i]=1;
else num[i]=0;
}
reverse(num,num+len+1);
return dfs(0,0,1);
}
int main(){
cin>>l>>r>>k>>b,mul[0]=1;
for(ll i=1;mul[i-1]<=r;++i)mul[i]=mul[i-1]*b;
cout<<solve(r)-solve(l-1);
return 0;
}
2018.09.07 Amount of degrees(数位dp)的更多相关文章
- Ural1057 - Amount of Degrees(数位DP)
题目大意 求给定区间[X,Y]中满足下列条件的整数个数:这个数恰好等于K个互不相等的B的整数次幂之和.例如,设X=15,Y=20,K=2,B=2,则有且仅有下列三个数满足题意: 输入:第一行包含两个整 ...
- [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 ...
- URAL 1057. Amount of Degrees(数位DP)
题目链接 我看错题了...都是泪啊,不存在3*4^2这种情况...系数必须为1... #include <cstdio> #include <cstring> #include ...
- 2018.09.07 loj#10166 数字游戏(数位dp)
传送门 数位dp板子题. f[i][mod]" role="presentation" style="position: relative;"> ...
- [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 ...
- 2018上海大都会邀请赛J(数位DP)
#include<bits/stdc++.h>using namespace std;int num[20];//按位储存数字int mod;long long dp[20][110][1 ...
- 2018.09.07 bzoj1096: [ZJOI2007]仓库建设(斜率优化dp)
传送门 斜率优化dp经典题. 令f[i]表示i这个地方修建仓库的最优值,那么答案就是f[n]. 用dis[i]表示i到1的距离,sump[i]表示1~i所有工厂的p之和,sum[i]表示1~i所有工厂 ...
- 2018.09.07 bzoj1911: [Apio2010]特别行动队(斜率优化dp)
传送门 斜率优化dp经典题. 题目中说的很清楚,设f[i]表示前i个数分配出的最大值. 那么有: f[i]=max(f[j]+A∗(sum[i]−sum[j])2+B∗(sum[i]−sum[j])+ ...
- 2018.09.07 codeforces311B. Cats Transport(斜率优化dp)
传送门 斜率优化dp好题. 对于第i只猫,显然如果管理员想从出发开始刚好接到它,需要在t[i]=h[i]−dist(1,i)" role="presentation" s ...
随机推荐
- 使用MATPLOTLIB 制图(散点图,热力图)
import numpy as np import pandas as pd import matplotlib.pyplot as plt data = pd.read_csv('D:\\myfil ...
- java msgbox
JAVA import javax.swing.JOptionPane; JOptionPane.showMessageDialog( null,"sample dialog !" ...
- 机器学习入门-集成算法(bagging, boosting, stacking)
目的:为了让训练效果更好 bagging:是一种并行的算法,训练多个分类器,取最终结果的平均值 f(x) = 1/M∑fm(x) boosting: 是一种串行的算法,根据前一次的结果,进行加权来提高 ...
- ActiveMQ无法启动
解决办法:activemq无法启动,端口被占用 用netstat -an无法查出61616被哪个进程占用(实践证明,netstat -ano|findstr '61616'什么也没有找到) 经过排查和 ...
- 前端-javascript-ECMAScript5.0
-前端常用开发工具:sublime.visual Studio Code.HBuilder.Webstorm. 使用的PCharm跟WebStorm是JetBrains公司推出的编辑工具,开发阶段建议 ...
- Git----分支管理之解决冲突03
人生不如意之事十之八九,合并分支往往也不是一帆风顺. 准备新的feature1分支,继续我们的新分支开发: $ git checkout -b feature1Switched to a new br ...
- springMVC学习记录2-使用注解配置
前面说了一下使用xml配置springmvc,下面再说说注解配置.项目如下: 业务很简单,主页和输入用户名和密码进行登陆的页面. 看一下springmvc的配置文件: <?xml version ...
- Redis 发布与订阅 消息
基于Redis消息队列-实现短信服务化 1.Redis实现消息队列原理 常用的消息队列有RabbitMQ,ActiveMQ,个人觉得这种消息队列太大太重,本文介绍下基于Redis的轻量级消息队列服务. ...
- Cache Server
[Cache Server] Whenever a source Asset like a .psd or an .fbx file is modified, Unity detects the ch ...
- 调试PHP如何让浏览器提示错误
php.ini中的display_errors的值改为On:或者php代码页顶部加上ini_set("display_errors", "On"); error ...