Java教程

TZOJ3465: How many seats(数位DP)

本文主要是介绍TZOJ3465: How many seats(数位DP),对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

题目描述

As we all know the winter vacation is coming soon, so train tickets are becoming hot again!   (You can see the long waiting line in front of the tickets office). Fortunately our lucky xiaoY has got a ticket back home, but Mr HH who gave xiaoY the ticket placed a hard problem at the same time. Because Mr HH was working on the CRH(和谐号), he needed to know how many seats the CRH could be sold. CRH is designed in a strange way.

1st:        The carriage of index i has the sum of i's all digital seats.

2nd:       From the beginning of the train every 5 carriage is forbidden to take passenger.

3rd:       Give you the index of head-----L, and index of tail-----R, calculate the number of seats could be sold!

Mr HH was tired of this hard math problem, so he turned this problem to xiaoY, Can you help xiaoY solve this problem to back home!  

翻译

众所周知,寒假快到了,火车票又火了!(可以看到售票处前排着长长的队伍)。幸运的是,我们幸运的xiaoY 拿到了回家的票,但是给xiaoY 票的HH 先生同时也提出了一个难题。因为HH先生在做CRH(和谐号),他需要知道这个CRH可以卖多少个座位。CRH 的设计方式很奇怪。

第一:索引 i 的车厢是 i 的所有数字座位的总和。

第二:从列车始发,每5节车厢禁止载客。

第三:给你头-----L的指数,和尾-----R的指数,计算可以卖的座位数!

HH先生厌倦了这道难的数学题,于是把这道题交给了小Y,你能帮小Y解决这个问题回家吗!  

输入

N    ---- Case number (N<=100)

N pair of number followed

L, R ---- head and tail of train. (0=< L<=R<=100000000)

翻译

N    ---- 案例编号 ( N <=100)

N对数字跟随

L、R----火车头尾。(0=< L <=R<=100000000)

输出

Sum ----- the total seats of CRH could be sold.

翻译

Sum ----- 可售出CRH的总座位数。

样例输入

3
3 5
8 13
1 10

样例输出

12
24
40

提示

3 + 4 + 5 = 12

8 + 9 + (1+0) + (1+1) + (1+3) = 24(第 5数字 12 被忽略)

1 + 2 + 3 + 4 + 6 + 7 + 8 + 9 = 40(第5数字5和第10数字10被忽略)

分析

求两个数之间所有数的数位和,第一时间想到想到数位DP,先打上一个数位DP的板子,但这道题有限制条件,其实也比较好想,每五个数不能算,也就是尾数其实是固定的,以为数位DP是从前往后累加,所以最后一个数如果是限制数,就返回0,否则返回sum,算是一道数位DP入门级别大水题。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
ll dp[20][101];
int a[20];
ll lim1,lim2;

ll dfs(int pos,int flag,int lead,int last,int sum){
	if(pos==0){
		if(last!=lim1&&last!=lim2){
			return sum;
		}else{
			return 0;
		}	
	}
	if(!lead&&!flag&&dp[pos][sum]!=-1){
		return dp[pos][sum];
	} 
	int x=flag?a[pos]:9;
	ll ans=0;
	for(int i=0;i<=x;++i){
		ans+=dfs(pos-1,flag&&(i==x),lead&&(i==0),i,sum+i);
	}
	if(!lead&&!flag){
		dp[pos][sum]=ans;
	}
	return ans;
}

ll calc(ll x){
	memset(a,0,sizeof(a));
	int pos=0;
	while(x>0){
		a[++pos]=x%10;
		x/=10;
	}
	return dfs(pos,1,1,0,0);
} 

int main(){
	ios::sync_with_stdio(false);
	int t;
	cin>>t;
	while(t--){
		memset(dp,-1,sizeof(dp));
		ll x,y;
		cin>>x>>y;
		lim1=(x+4)%10;
		lim2=(x+9)%10;
		cout<<calc(y)-calc(x-1)<<"\n";
	}
} 

  

Sum ----- 可售出CRH的总座位数。

这篇关于TZOJ3465: How many seats(数位DP)的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!