您的位置:首页 > 其它

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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  hdu dp