lightoj 1205 数位DP

AC代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
using namespace std;

long long dp[30][30][2];
int digit[30];
int num[30];

long long DFS( int pos, int beginpos, bool limit, bool pre_all_0 ){
	
	if( pos <= 0 ){ 
		return 1;
	}
	if( !limit && dp[pos][beginpos][pre_all_0==true] != EOF ){
		return dp[pos][beginpos][pre_all_0==true];
	}
	
	int end = limit ? digit[pos] : 9;
	long long sum = 0;
	for( int i = 0; i <= end; i++ ){

		if( pre_all_0 ){
		
			num[pos] = i;
			sum += DFS( pos - 1, beginpos - !i, limit && i == end, pre_all_0 && !i );
			
		}else{
		
			int temp = ( beginpos + 1 ) / 2;
			bool contion = beginpos % 2 == 1 ? pos < temp : pos <= temp;

			if( contion ){
				if( num[beginpos+1-pos] == i ){
					sum += DFS( pos - 1, beginpos, limit && i == end, pre_all_0 );
				}
			}else{
				num[pos] = i;
				sum += DFS( pos - 1, beginpos, limit && i == end, pre_all_0 );
			}
		}
	}

	if( !limit ){
		dp[pos][beginpos][pre_all_0==true] = sum;
	}
	return sum;
}

long long solve( long long N ){
	int len = 0;
	while( N ){
		digit[++len] = N % 10;
		N /= 10;
	}
	return DFS( len, len, true, true );
}

int main(){
	int T, Case = 1;
	long long A, B;
	
	cin >> T;
	while( T-- ){
		memset( dp, -1, sizeof( dp ) );
		cin >> A >> B;
		if( A > B ){
			swap( A, B );
		}
		cout << "Case " << Case++ << ": "<< solve( B ) - solve( A - 1 ) << endl;
	}
	return 0;
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值