Ural - 1057. Amount of Degrees
2015-07-07 12:01
441 查看
1057. Amount of Degrees
Create a code to determine the amount of integers, lying in the set [X;Y] 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 = 24+20,
18 = 24+21,
20 = 24+22.
Input
The first line of input contains integers X and Y, separated with a space (1≤X≤Y≤231−11 ≤ X ≤ Y ≤ 2^31−1). The next two lines contain integers K and B (1≤K≤20;2≤B≤101 ≤ 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.
Sample
input output
15 20
2
2
3
思路:dp[cur][s][K]表示处理到当前状态,前面有s个不是1,一共需要K个不是1的,有多少
[code]#include<iostream> #include<cstdio> #include<string> #include<cstring> #include<vector> #include<cmath> #include<queue> #include<stack> #include<map> #include<set> #include<algorithm> using namespace std; typedef long long LL; const int maxn=100; int X,Y; int K,B; int dig[maxn]; int dp[maxn][20][25]; int dfs(int cur,int e,int s) { if(cur<0)return s==K; if(s>K)return 0; if(!e&&dp[cur][s][K]!=-1) return dp[cur][s][K]; int end=(e?min(1,dig[cur]):1); int ans=0; for(int i=0;i<=end;i++) ans+=dfs(cur-1,e&&i==dig[cur],s+(i!=0));//这里一定注意要是i==dig[cur],因为前面的end是变化过的 if(!e)dp[cur][s][K]=ans; return ans; } int solve(int x) { int len=0; memset(dp,-1,sizeof(dp)); while(x) { dig[len++]=x%B; x/=B; } return dfs(len-1,1,0); } int main() { while(scanf("%d%d",&X,&Y)!=EOF) { scanf("%d%d",&K,&B); printf("%d\n",solve(Y)-solve(X-1)); } return 0; }
相关文章推荐
- [Cocos2d-x]Cocos2d-x 3.2 学习笔记
- SVN命令使用详解
- jquery获取多个checkbox的值异步提交给php
- Mysql 5.6.10的事件开启和自启动
- 实现窗口中的文档自动向上滚动,方便阅读
- 77. sqlserver 锁表解决方式
- WinCE自启动Shell的问题
- kafka脚本
- 在MySQL中同时查找两张表中的数据的示例
- sip IMS VOLTE课程培训
- Java执行外部程序(Apache Commons Exec)
- 从中序表达式到逆序表达式(逆波兰式)(四则运算表达式求值)
- 电视上的各种接口
- 使用MONGODB 集群的OPLOG 日志进行数据恢复
- 开源游戏引擎哪家强?
- Unity中资源动态加载的两种方式之AssetsBundle
- mac上如何编写离线blog
- 双端队列
- Closures闭包
- GRE写作必备句型