(数位dp)HDU3652B-number

HDU3652 B-number

题意&思路:

找出1~n中含有13并能被13整除的数字个数。
纠结了好久,状态真的是差。
dp[len][p13][mod]三个参数分别表示数位长度;p13的0表示上一位不是1,1表示上一位是1,2表示存在13;mod表示这个数和13的余数。
剩下的就是数位dp的板子了,我太菜了啊。

代码:

#include<bits/stdc++.h>
#define pii pair<int,int>
#define ll long long
const int N=1e9+10;
const int maxn=0x3f3f3f3f;
const int minn=0xc0c0c0c0;
const int inf=99999999;
using namespace std;
int dp[20][3][20]={0},a[20];
int judge(int x,int y)
{
	if(x!=2 && y==1)
		return 1;
	else if(x==1 && y==3 || x==2)
		return 2;
	else 
		return 0;
}
int dfs(int len,int p13,int mod,bool limit)
{
	if(len==0)	return p13==2&&mod==0;
	if(!limit && dp[len][p13][mod])	return  dp[len][p13][mod];
	int i,temp=0,up=limit?a[len]:9;
	for(i=0;i<=up;i++)
		temp+=dfs(len-1,judge(p13,i),(mod*10+i)%13,limit && i==a[len]);
	if(!limit)	dp[len][p13][mod]=temp;
	return temp;
}
int solve(int x)
{
	int l;
	while(x)
	{
		a[++l]=x%10;
		x/=10;	
	}	
	return dfs(l,0,0,true);
}
int main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);cout.tie(0);
	int n;
	while(cin>>n)
		cout<<solve(n)<<endl;
	return 0;
}

发布了59 篇原创文章 · 获赞 0 · 访问量 810

猜你喜欢

转载自blog.csdn.net/Z7784562/article/details/103940199