hdu_3886_Final Kichiku “Lanlanshu”(数位DP)

37 篇文章 0 订阅

题目连接: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;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值