$BZOJ1799\ Luogu4127$ 月之谜 数位统计$DP$

AcWing

 

Description


Sol

看了很久也没有完全理解直接$DP$的做法,然后发现了记搜的做法,觉得好棒!

这里是超棒的数位$DP$的记搜做法总结   看完仿佛就觉得自己入门了,但是就像文中写的,还是要多做题才能真正弄明白鸭,还要加油(ง •_•)ง

然后这里是同一个人的本题题解

 

Code

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #define il inline
 5 #define Rg register
 6 #define go(i,a,b) for(i=a;i<=b;i++)
 7 #define yes(i,a,b) for(i=a;i>=b;i++)
 8 #define ll long long
 9 #define mem(a,b) memset(a,-1,sizeof(a));
10 using namespace std;
11 il ll read()
12 {
13     ll x=0,y=1;char c=getchar();
14     while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();}
15     while(c>='0'&&c<='9'){x=(x<<1)+(x<<3)+c-'0';c=getchar();}
16     return x*y;
17 }
18 int len,a[20];
19 ll l,r,mod,f[20][200][200];
20 il ll dfs(int pos,int sum,int ys,int limit)
21 {
22     if(pos>len)return (ys==0 && sum==mod);
23     if(!limit && f[pos][sum][ys]!=-1)return f[pos][sum][ys];
24     ll as=0;int maxs=limit?a[len-pos+1]:9,i;
25     go(i,0,maxs)as+=dfs(pos+1,sum+i,(1LL*ys*10+i)%mod,i==maxs&&limit);
26     if(!limit)f[pos][sum][ys]=as;return as;
27 }
28 il ll sol(ll x)
29 {
30     len=0;
31     while(x)a[++len]=x%10,x/=10;
32     ll as=0;
33     go(mod,1,9*len){mem(f,0);as+=dfs(1,0,0,1);}
34     return as;
35 }
36 int main()
37 {
38     l=read(),r=read();
39     printf("%lld\n",sol(r)-sol(l-1));
40     return 0;
41 }
View Code

 

 

 

posted @ 2019-07-27 22:46  DTTTTTTT  阅读(169)  评论(0编辑  收藏  举报