BZOJ 4521: [Cqoi2016]手機號碼

比較好搞(暴力)的數位DP

f[pos][a][b][c1][c2][c3][flag]

表示目前決策第pos位,a是pos+2位,b是pos+1位,c1表示是否有3個連續的,c2表示是否有4,c3表示是否有8,flag表示這一位能不能取比原數大的

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<queue>
#include<vector>
#include<algorithm>
#include<map>
#include<set>
#define rep(i,l,r) for(int i=l;i<=r;i++)
#define per(i,r,l) for(int i=r;i>=l;i--)
#define mmt(a,v) memset(a,v,sizeof(a))
#define tra(i,u) for(int i=head[u];i;i=e[i].next)
using namespace std;
typedef long long ll;
ll f[15][10][10][2][2][2][2];
bool vis[15][10][10][2][2][2][2];
int dpt[15];
ll dp(int pos,int a,int b,bool c1,bool c2,bool c3,bool flag){
	if(vis[pos][a][b][c1][c2][c3][flag])
	return f[pos][a][b][c1][c2][c3][flag];
	vis[pos][a][b][c1][c2][c3][flag]=1;
	ll &ans=f[pos][a][b][c1][c2][c3][flag];
	if(c2&&c3)return ans=0;
	if(!pos)return ans=c1;
	int l=(pos==11)?1:0,r=flag?9:dpt[pos];
	rep(i,l,r)
	ans+=dp(pos-1,b,i,c1||(a==b&&b==i),c2||(i==4),c3||(i==8),flag||(i<dpt[pos]));
	return ans;
}
ll calc(ll x){
	dpt[0]=0;
	while(x)dpt[++dpt[0]]=x%10,x/=10;
	mmt(f,0);mmt(vis,0);
	dp(11,0,0,0,0,0,0);
	return f[11][0][0][0][0][0][0];
}
int main(){
	//freopen("a.in","r",stdin);
	ll l,r;scanf("%lld %lld\n",&l,&r);
	if(l==(ll)1e10)printf("%lld\n",calc(r));
	else printf("%lld\n",calc(r)-calc(l-1));
	return 0;
}
	
	


發佈了293 篇原創文章 · 獲贊 3 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章