Timus 1057 数位dp
来源:互联网 发布:北京四达时代通讯网络 编辑:程序博客网 时间:2024/06/01 10:26
1057. Amount of Degrees
Time limit: 1.0 second
Memory limit: 64 MB
Memory limit: 64 MB
Create a code to determine the amount of integers, lying in the set [X;Y] and being a sum of exactlyK 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.
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−1). The next two lines contain integers K 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.
Sample
15 2022
3
找到状态后,很容易做了。
#include<iostream>#include<cstdio>#include<cstring>#include<cmath>#include<map>#include<queue>#include<stack>#include<vector>#include<ctype.h>#include<algorithm>#include<string>#define PI acos(-1.0)#define maxn 40#define maxm 15#define INF 1<<25typedef long long ll;using namespace std;int ff[maxn][maxn];int bit[maxn];int aa,bb;int lim,bt;int solve (int pos,int p,bool cmp){ if(pos==0) return p==lim; if(~ff[pos][p]&&!cmp) return ff[pos][p]; int u=cmp?bit[pos]:bt-1; u=min(u,1); int ans=0; for(int i=0; i<=u; i++) { int newp; if(i==1) newp=p+1; else newp=p; bool c=(cmp&&(i==bit[pos])); if(newp>lim) continue; ans+=solve(pos-1,newp,c); } return cmp?ans:ff[pos][p]=ans;}int main(){ scanf("%d%d%d%d",&aa,&bb,&lim,&bt); int pp=0; aa--; memset(ff,-1,sizeof(ff)); memset(bit,0,sizeof(bit)); while(aa) { bit[++pp]=aa%bt; aa/=bt; } int l1=solve(pp,0,1); pp=0; memset(ff,-1,sizeof(ff)); memset(bit,0,sizeof(bit)); while(bb) { bit[++pp]=bb%bt; bb/=bt; } int l2=solve(pp,0,1); printf("%d\n",l2-l1);}
- Timus 1057 数位dp
- Timus OJ 1057 数位dp
- Timus Online Judge 1057. Amount of Degrees(数位dp)
- URAL 1057 数位DP
- URAL 1057 数位DP
- URAL 1057 数位DP
- URAL 1057 (数位dp)
- Timus 1225. Flags dp
- Timus 1009 DP
- Timus 1010 DP
- Timus 1013 DP
- Timus 1012 DP
- URAL 1057 数位DP(递归)
- ural 1057(数位dp)
- 数位dp
- 数位DP
- 数位DP
- 数位dp
- HDU 2037 - 今年暑假不AC(贪心 + 动态)
- c/c++static
- C#中调用VC编写的dll库
- java读取properties文件方法和对比
- Android入门之Service和BroadcastReceiver
- Timus 1057 数位dp
- dom4j xml解析
- 大数相乘
- 新的开始 new start
- QML数据模型(Model)
- Epoch时间和可读时间的相互转换
- 谷歌眼镜设计针对性的应用
- 移动WEB应用实践:Bootstrap应用实践
- 黑马程序员---交通灯管理系统