SGU_390_Tickets(另类数位DP)

Tickets

Time Limit : 1000/500ms (Java/Other)   Memory Limit : 524288/262144K (Java/Other)
Total Submission(s) : 79   Accepted Submission(s) : 16
Problem Description


Conductor is quite a boring profession, as all you have to do is just to sell tickets to the passengers. So no wonder that once upon a time in a faraway galaxy one conductor decided to diversify this occupation. Now this conductor sells several tickets at a time to each passenger. More precisely, he successively gives tickets to the passenger until the sum of the digits on all the tickets given becomes not less than some integer number  k. Then this process repeats for the next passenger. Initially conductor has a tape of tickets numbered successively from  l to  r, inclusive. This way of tickets distribution is quite good, because passengers are glad to get several tickets when they pay only for one. But there is one disadvantage. Since each passenger gets several tickets, it is possible that conductor won't be able to serve all passengers. Your task is to help conductor in this difficult situation. You should calculate how many passengers is the conductor able to serve.

Input
Input file contains three integer numbers  lr and  k (1 ≤  l ≤  r ≤ 10 18, 1 ≤  k ≤ 1000).

Output
Output should contain exactly one number — the answer to the problem.

Example(s)
sample input

sample output

40 218 57

29



题意:
给你一个区间,让你分段,分段完后一段连续的数的数位和要大于K,问这样的段有多少
题解:
朴素的数位DP肯定不能过,这题在这篇国家集训队论文上出现过,作者讲的比较清楚( 算法合集之《浅谈数位类统计问题》),
我们设dp[i][sum][rem]表示考虑到第i为,i之前的数位和为sum,当前还有多少rem没有用来分段,这里我用一个pair型来记录满足条件的段数cnt,和在这个dp状态下的rem,然后采用记忆化搜索,就能大大降低时间复杂度,为什么?因为当考虑到第i位时,前面的和为sum,只要不是边界情况下后面的每一个数位都可以取0-9,所以如果之前算出了当前的这个状态,那么就可以直接返回这个状态的值,这就是记忆化搜索的优势。
我看完论文后,对rem的概念还是比较模糊,然后自己模拟了一遍,最后懂了这个的意思,就拿样列来说,40—218内找满足条件的分段数,我们最开始sum和rem都为0,从40开始搜,40的数位和sum为4,然后dfs到最后一位,发现sum+rem小于57,所以到40这个数的时候不能分为一段,然后返回cnt=0,rem=sum+rem=4,就代表40这个数还没用于分段,然后回溯回去搜41,此时的sum为5,rem为4,然后发现sum+rem还是小于57,就返回cnt=0,rem=sum+rem=9,代码40和41这两个数都没用于分段,最后一直搜到47的时候sum=11,rem=49,然后发现sum+rem>=57成立,返回cnt=1,rem=0,表示到这个数可以分为一段,然后rem为0表示分完一段后,没用的数,没有了,所以对应的数位和rem为0。
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<algorithm>
 4 #define F(i,a,b) for(int i=a;i<=b;i++)
 5 using namespace std;
 6 typedef long long LL;
 7 typedef pair<LL,LL>P;
 8 int l[30],r[30],ln,rn,vis[30][200][1005];
 9 LL a,b,m;
10 P dp[30][200][1005];
11 P dfs(int pos=rn,int sum=0,int rem=0,bool up=1,bool dn=1){
12     if(!pos)if(sum+rem>=m)return P(1,0);else return P(0,sum+rem);
13     if(vis[pos][sum][rem]&&!up&&!dn)return dp[pos][sum][rem];
14     int st=dn?l[pos]:0,end=up?r[pos]:9;P ans=P(0,rem);
15     F(i,st,end){
16         P tp=dfs(pos-1,sum+i,ans.second,up&&i==end,dn&&i==st);
17         ans.first+=tp.first,ans.second=tp.second;
18     }
19     if(!up&&!dn)dp[pos][sum][rem]=ans,vis[pos][sum][rem]=1;
20     return ans;
21 }
22 
23 int main(){
24     while(~scanf("%I64d%I64d%I64d",&a,&b,&m)){
25         memset(vis,0,sizeof(vis));
26         for(rn=0;b;b/=10)r[++rn]=b%10;
27         for(ln=0;ln<rn;a/=10)l[++ln]=a%10;
28         printf("%I64d\n",dfs().first);
29     }
30     return 0;
31 }
View Code

 





 

 

转载于:https://www.cnblogs.com/bin-gege/p/5696089.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值