YBT高效进阶_5.3.1 B数计数

YBT高效进阶_5.3.1 B数计数

思路

用填数法一位一位看
用3维表示状态:当前位,余数,含有13的状态

CODE

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int f[11][13][3],power[14],n;
int digitDP(int dep,int mods,int digit,bool limit)
{
    
    
	if(!limit&&f[dep][mods][digit]>-1)return f[dep][mods][digit];
	if(!dep&&!mods&&!digit)return 1;
	if(!dep)return 0;
	int i,t,ans=0,mx=limit?(n/power[dep-1]%10):9;
	for(i=0;i<=mx;i++)
	{
    
    
		t=(mods+13-i*power[dep-1]%13)%13;
		if(digit==2)
		{
    
    
			ans+=digitDP(dep-1,t,2,limit&(i==mx));
			if(i==1)ans+=digitDP(dep-1,t,1,limit&(i==mx));
		}
		if(digit==1&&i==3)
			ans+=digitDP(dep-1,t,0,limit&(i==mx))+digitDP(dep-1,t,1,limit&(i==mx));
		if(digit==0)
		{
    
    
			if(i!=3)ans+=digitDP(dep-1,t,0,limit&(i==mx));
			if(i!=1&&i!=3)ans+=digitDP(dep-1,t,1,limit&(i==mx));
		}
	}
	if(limit)return ans;
	return f[dep][mods][digit]=ans;
}
int solve(int x)
{
    
    
	int sum;
	if(!x)return 0;
	for(sum=0;x;x/=10,++sum);
	return digitDP(sum,0,2,1);
}
int main()
{
    
    
	int i;
	for(power[0]=1,i=1;i<=13;power[i]=power[i-1]*10,++i);
	for(memset(f,-1,sizeof(f)),f[0][0][0]=1;~scanf("%d",&n);printf("%d\n",solve(n)));
	return 0;
}

猜你喜欢

转载自blog.csdn.net/weixin_46975572/article/details/117850512