hdu 3652

求[1,n]种出现过13并且能被13整除的数字的个数,只比hdu 3555多了一个维度,即记录数字模13的余数

#include <cstdio>
#include <cstring>

using namespace std;
typedef long long LL;
LL n,bits[20],d[20][20][3];

LL dp(int pos,int mod,int st,int dif){
	if(pos == 0) return st == 2 && mod == 0;
	if(dif && d[pos][mod][st] != -1) return d[pos][mod][st];
	LL x = dif ? 9 : bits[pos];
	LL ans = 0;
	for(int i = 0;i<=x ;i++ ){
		if(st == 2 || (st == 1 && i == 3)) 
			ans += dp(pos-1,(mod*10+i)%13,2,dif || i<x);
		else if(i == 1) ans += dp(pos-1,(mod*10+i)%13,1,dif||i<x);
		else ans += dp(pos-1,(mod*10+i)%13,0,dif||i<x);
	} 
	if(dif) d[pos][mod][st] = ans;
	return ans;
}

LL cal(LL n){
	int cnt = 0;
	while(n){
		bits[++cnt] = n % 10;
		n /= 10;
	}
	return dp(cnt,0,0,0);
}

int main(){
	while(~scanf("%lld",&n)){
		memset(d,-1,sizeof(d));
		printf("%lld\n",cal(n));	
	}
	
	return 0;
} 

猜你喜欢

转载自blog.csdn.net/winhcc/article/details/88957157