【HDU】3555 Bomb 数位DP

传送门:【HDU】3555 Bomb


题目分析:数位DP水题。


代码如下:


#include <cstdio>
#include <cstring>
#include <algorithm>
//#include <cmath>
#include <queue>
using namespace std ;
  
typedef long long LL ;
  
#define rep( i , a , b ) for ( int i = a ; i < b ; ++ i )
#define For( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define rev( i , a , b ) for ( int i = a ; i >= b ; -- i )
#define travel( e , H , u ) for ( Edge* e = H[u] ; e ; e = e -> next )
#define clr( a , x ) memset ( a , x , sizeof a )
#define cpy( a , x ) memcpy ( a , x , sizeof a )

const int MAXN = 65 ;

LL dp[MAXN][10][2] ;
LL ten[MAXN] ;
int vis[MAXN][10][2] , Time ;
int num[MAXN] ;
LL n ;

LL dfs ( int cur , int j , int flag , int num[] ) {
	if ( cur == -1 ) return 0 ;
	if ( vis[cur][j][flag] == Time ) return dp[cur][j][flag] ;
	vis[cur][j][flag] = Time ;
	dp[cur][j][flag] = 0 ;
	if ( flag == 0 ) {
		rep ( i , 0 , num[cur] ) {
			if ( j == 4 && i == 9 ) dp[cur][j][flag] += ten[cur] ;
			else dp[cur][j][flag] += dfs ( cur - 1 , i , 1 , num ) ;
		}
		if ( j == 4 && num[cur] == 9 ) dp[cur][j][flag] += n % ten[cur] + 1 ;
		else dp[cur][j][flag] += dfs ( cur - 1 , num[cur] , 0 , num ) ;
	} else {
		rep ( i , 0 , 10 ) {
			if ( j == 4 && i == 9 ) dp[cur][j][flag] += ten[cur] ;
			else dp[cur][j][flag] += dfs ( cur - 1 , i , 1 , num ) ;
		}
	}
	return dp[cur][j][flag] ;
}

void solve () {
	LL tmp ;
	int n1 = 0 ;
	scanf ( "%I64d" , &n ) ;
	tmp = n ;
	while ( tmp ) {
		num[n1 ++] = tmp % 10 ;
		tmp /= 10 ;
	}
	++ Time ;
	LL count = dfs ( n1 - 1 , 0 , 0 , num ) ;
	printf ( "%I64d\n" , count ) ;
}

int main () {
	//freopen ( "in.txt" , "r" , stdin ) ;
	//freopen ( "out.txt" , "w" , stdout ) ;
	int T ;
	clr ( vis , 0 ) ;
	Time = 0 ;
	ten[0] = 1 ;
	rep ( i , 1 , MAXN ) ten[i] = ten[i - 1] * 10 ;
	scanf ( "%d" , &T ) ;
	while ( T -- ) solve () ;
	return 0 ;
}


你可能感兴趣的:(HDU)