D. Divisible by Twenty-Five(数位dp)

D. Divisible by Twenty-Five

分析

直接看代码吧,感觉比较容易理解

代码

#include<iostream>
#include<queue>
#include<cstring>
#include<vector>
#include<stdio.h>
#include<map>
#include<algorithm>
#include<deque>
#include<stack>
#include<set>
#include <random>
#include<bitset>
// #include <unordered_map>
#include<math.h>
#include<string.h>
#define IOS ios::sync_with_stdio(false),cin.tie(0);
using namespace std;
 
#define pb push_back
#define coutl cout<<"------------"<<endl;
#define fi first
#define se second

#define ire(x) scanf("%d",&x)
#define iire(a,b) scanf("%d %d",&a,&b)
#define lre(x) scanf("%lld",&x)
#define llre(a,b) scanf("%lld %lld",&a,&b)
#define dre(x) scanf("%lf",&x)
#define ddre(a,b) scanf("%lf %lf",&a,&b)
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define endl "\n"
#define PI acos(-1.0)
#define int long long
// #define double long double
// typedef __int128 ll;
typedef long long ll;
typedef unsigned long long ull;

typedef pair<int, int> PII;
typedef pair<double, int> PDI;
typedef pair<ll, ll> PLL;
typedef pair<double, double> PDD;
typedef pair<double, pair<int, double> > PDID;
typedef pair<char, char> PCC;
typedef pair<char, pair<int, int> > PCII;
typedef pair<int, pair<int, int> > PIII;
typedef pair<int, pair<int, pair<int, int> > > PIIII;
typedef pair<ll, pair<int, int> > PLII;

const int maxn = 5e5 + 10;
const int N = 5e5 + 7;
const int M = 5e5 + 7;
const int mod = 1e9+7;
const int inv = mod - mod/2;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const double pi = acos(-1);
const double eps = 1e-5;
  
ll gcd(ll a,ll b) {return b==0 ? a : gcd(b,a%b);}
ll lcm(ll a,ll b) {return a*b / gcd(a,b);}
ll qmi(ll a,ll b,ll p) {ll ans = 1; while(b) { if(b & 1) ans = ans * a % p; a = a * a % p; b >>= 1; } return ans;}
int lowbit(int x) {return x & (-x);}

/*
一眼数位dp
	
当前到第几位,上一位是什么,当前位是什么,是否有前导零,x填什么
*/

string s;
int dp[10][11][11][2][10];

int dfs(int i,int la,int now,bool is_pre,int x)
{
	if(i == s.size())
	{
		if(!is_pre && ((la == 2 && now == 5) || (la == 7 && now == 5) || (la == 5 && now == 0) || (la == 0 && now == 0))) return 1;
		else return 0;
	}
	
	if(dp[i][la][now][is_pre][x] != -1) return dp[i][la][now][is_pre][x];
	
	int ans = 0;
	
	if(s[i] != '_' && s[i] != 'X')
	{
		if(i == 0 && s[i] == '0') ans += dfs(i+1,now,s[i]-'0',true,x);	//有前导零
		else ans += dfs(i+1,now,s[i]-'0',is_pre,x);
	}
	else
	{
		if(s[i] == '_')
		{
			int up = 9;
			int dn = (i == 0) ? 1 : 0;
			
			for(int num = dn ; num <= up ; num ++) ans += dfs(i+1,now,num,is_pre,x);
		}	
		else if(s[i] == 'X')
		{
		    if(i == 0 && x == 0) ans += dfs(i+1,now,x,true,x);	//有前导零
		    else ans += dfs(i+1,now,x,is_pre,x);
		}
	}
	
	dp[i][la][now][is_pre][x] = ans;
	
	return dp[i][la][now][is_pre][x];
}

void solve()
{
	cin>>s;
	
	if(s == "0" || s == "_" || s == "X")
	{
	    cout<<1<<'\n';
	    return;
	}
	
	int f = 0;
	for(int i=0;i<s.size();i++)
		if(s[i] == 'X')
		{
			f = 1;
			break;
		}
	
	ll ans = 0;
	if(f)
	{
		for(int i=0;i<=9;i++)	//枚举x填啥
		{
			memset(dp,-1,sizeof dp);
			ans += dfs(0,10,10,0,i);
		}
	}
	else
	{
	   // cout<<"---\n";
		memset(dp,-1,sizeof dp);
		ans = dfs(0,10,10,0,0);
	}
	
	cout<<ans<<'\n';
}

signed main()
{
 	IOS;

	int t;
	t = 1;
//  	cin>>t;
	while(t--)
	{
		solve();
	}
	
	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值