2017广东工业大学程序设计竞赛决赛-G题-等凹数字

这题在求回文串的基础上还要等凹。

可以视为LightOJ 1205的升级版,不知道如何写回文串的数位DP可以打开链接LightOJ 1205


因此这题只需要加多2维,最高位的数字以及前一位(用于判断升降) (此思想来自于点击打开链接

在回文串的前半部分不能大于pre,后半段不能小于pre。

然后最后判断是否全为一个数字就可以了。

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <algorithm>  
#include <iostream>  
#include <cstdlib>  
#include <cstring>  
#include <string>
#include <cstdio>  
#include <climits>
#include <cmath> 
#include <vector>
#include <set>
#include <queue>
#include <stack>
#include <map>
#include <sstream>
#define INF 0x3f3f3f3f
#define LL long long
#define fora(i,a,n) for(int i=a;i<=n;i++)
#define fors(i,n,a) for(int i=n;i>=a;i--)
#define sci(x) scanf("%d",&x);
#define scl(x) scanf("%lld",&x);
#define MAXN 100024
const double eps = 1e-8;
using namespace std;
LL t, l, r, k, z = 1;
LL dp[22][22][10][10][2], digits[22], num[22];
LL dfs(LL start, LL dex,LL pre,LL s_n, LL flag, LL p,LL same) {
	LL sum = 0;
	if (dex == 0) return flag&&!same;
	if (dp[start][dex][pre][s_n][flag] && !p) 
		return dp[start][dex][pre][s_n][flag];
	LL u = p ? digits[dex] : 9;
	fora(i, 0, u) {
		num[dex] = i;
		if (start == dex&&i == 0) //前导0 
			sum += dfs(start - 1, dex - 1,9,0, flag, p&&i == u,1);
		else if (flag && (start / 2 + 1) > dex) {//回文串后半部分  
			if (i < pre) continue;//不能小于前一个数字,否则不符合题目等凹   
			sum += dfs(start, dex - 1,i,num[start], num[start - dex + 1] == i, p&&i == u,same&&i==s_n);
		}
		else {// 回文串前半部分
			if (i > pre) continue;//不能大于前一个数字,否则不符合题目等凹
			sum += dfs(start, dex - 1,i,num[start], flag, p&&i == u,same&&i==num[start]);
		}
	}
	if (!p) dp[start][dex][pre][s_n][flag] = sum;
	return  sum;
}
LL digit(LL n) {
	k = 0;
	while (n) {
		digits[++k] = n % 10;
		n /= 10;
	}
	return dfs(k, k,9,0, 1, 1,1);
}
int main() {
#ifdef DID
	freopen("in.txt", "r", stdin);
	//freopen("out.txt","w",stdout);
#endif
	scl(t);
	memset(dp, 0, sizeof(dp));
	while (t--) {
		scl(l); scl(r);
		if (l>r) swap(l, r);
		LL sum1 = digit(r);
		LL sum2 = digit(l - 1);
		printf("%lld\n", sum1 - sum2);
	}
	return 0;
}



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值