hdu_3886_Final Kichiku “Lanlanshu”(数位DP)
2016-07-06 15:05
381 查看
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3886
题意:这题的题意有点晦涩难懂,大概意思就是给你一个区间,让你找一些满足递增递减条件的数,举个列:/-\,要匹配这个关系,把一个数字分成一列数位,满足先递增,然后相等,然后递减的关系:ie:123321,1221,123441,这些都满足/-\。
题解:设dp[i][j][k]表示考虑到第i位,上一个数为j,匹配关系到了k,然后DP下去就行了,注意处理左区间-1和前导零
#include<cstdio>
#include<cstring>
#include<cmath>
#define F(i,a,b) for(int i=a;i<=b;i++)
typedef long long LL;
char ops[111],a[111],b[111];
int dig[111],st,len,oplen;
LL N=(LL)1e10,dp[111][11][111];
int check(int pre,int now,int idx){
char c=ops[idx];
if(c=='/')return pre<now;
else if(c=='-')return pre==now;
return pre>now;
}
LL dfs(int pos,int idx=0,int pre=0,int z=0,bool inf=1){
if(!pos)return idx==oplen;
if(!inf&&~dp[pos][pre][idx])return dp[pos][pre][idx];
int end=inf?dig[pos]:9;LL ans=0;
F(i,0,end){
if(!z)ans+=dfs(pos-1,idx,i,z||i,inf&&i==end),ans%=N;
else if(idx<oplen&&check(pre,i,idx))
ans+=dfs(pos-1,idx+1,i,z||i,inf&&i==end),ans%=N;
else if(idx>0&&check(pre,i,idx-1))
ans+=dfs(pos-1,idx,i,z||i,inf&&i==end),ans%=N;
}
if(!inf)dp[pos][pre][idx]=ans;
return ans;
}
void f_ck(){
memset(dp,-1,sizeof(dp));
oplen=strlen(ops);
for(st=0,len=0;a[st]=='0';)st++;
int end=strlen(a);
for(int i=end-1;i>=st;i--)dig[++len]=a[i]-'0';
dig[1]--;//处理左区间-1
for(int i=1;dig[i]<0;)dig[i]=9,dig[i+1]--;
LL tmp=(len==0)?0:dfs(len);
for(st=0,len=0;b[st]=='0';)st++;
end=strlen(b);
for(int i=end-1;i>=st;i--)dig[++len]=b[i]-'0';
LL an=dfs(len)-tmp+N;
printf("%08lld\n",an%(LL)1e8);
}
int main(){
while(~scanf("%s%s%s",ops,a,b))f_ck();
return 0;
}
题意:这题的题意有点晦涩难懂,大概意思就是给你一个区间,让你找一些满足递增递减条件的数,举个列:/-\,要匹配这个关系,把一个数字分成一列数位,满足先递增,然后相等,然后递减的关系:ie:123321,1221,123441,这些都满足/-\。
题解:设dp[i][j][k]表示考虑到第i位,上一个数为j,匹配关系到了k,然后DP下去就行了,注意处理左区间-1和前导零
#include<cstdio>
#include<cstring>
#include<cmath>
#define F(i,a,b) for(int i=a;i<=b;i++)
typedef long long LL;
char ops[111],a[111],b[111];
int dig[111],st,len,oplen;
LL N=(LL)1e10,dp[111][11][111];
int check(int pre,int now,int idx){
char c=ops[idx];
if(c=='/')return pre<now;
else if(c=='-')return pre==now;
return pre>now;
}
LL dfs(int pos,int idx=0,int pre=0,int z=0,bool inf=1){
if(!pos)return idx==oplen;
if(!inf&&~dp[pos][pre][idx])return dp[pos][pre][idx];
int end=inf?dig[pos]:9;LL ans=0;
F(i,0,end){
if(!z)ans+=dfs(pos-1,idx,i,z||i,inf&&i==end),ans%=N;
else if(idx<oplen&&check(pre,i,idx))
ans+=dfs(pos-1,idx+1,i,z||i,inf&&i==end),ans%=N;
else if(idx>0&&check(pre,i,idx-1))
ans+=dfs(pos-1,idx,i,z||i,inf&&i==end),ans%=N;
}
if(!inf)dp[pos][pre][idx]=ans;
return ans;
}
void f_ck(){
memset(dp,-1,sizeof(dp));
oplen=strlen(ops);
for(st=0,len=0;a[st]=='0';)st++;
int end=strlen(a);
for(int i=end-1;i>=st;i--)dig[++len]=a[i]-'0';
dig[1]--;//处理左区间-1
for(int i=1;dig[i]<0;)dig[i]=9,dig[i+1]--;
LL tmp=(len==0)?0:dfs(len);
for(st=0,len=0;b[st]=='0';)st++;
end=strlen(b);
for(int i=end-1;i>=st;i--)dig[++len]=b[i]-'0';
LL an=dfs(len)-tmp+N;
printf("%08lld\n",an%(LL)1e8);
}
int main(){
while(~scanf("%s%s%s",ops,a,b))f_ck();
return 0;
}
相关文章推荐
- 详解Android应用中屏幕尺寸的获取及dp和px值的转换
- 基于Android中dp和px之间进行转换的实现代码
- Android中dip、dp、sp、pt和px的区别详解
- LFC1.0.0 版本发布
- Android dpi,dip,dp的概念以及屏幕适配
- Android px、dp、sp之间相互转换
- HP data protector软件学习1--基本角色与基本工作流程
- HP data protector软件学习2--软件组成与界面介绍
- android中像素单位dp、px、pt、sp的比较
- Android对px和dip进行尺寸转换的方法
- 【HDU 5366】The mook jong 详解
- 【HDU 2136】Largest prime factor 详细图解
- 【HDU 1568】Fibonacci 数学公式 详解
- Android根据分辨率进行单位转换-(dp,sp转像素px)
- android 尺寸 dp,sp,px,dip,pt详解
- DP问题各种模型的状态转移方程
- HDU 1568
- HDU1290
- POJ-1695-Magazine Delivery-dp
- nyoj-1216-整理图书-dp