P2602 [ZJOI2010] digital count
answer
DFS nausea digital DP
For this question, we can find a number of a number
I.e., respectively, count the number of interval [L, R] i arise internal digital (0 <= i <= 9)
DFS is the only record:
Current fill the first of several pos
How many times have appeared a total sum k
Target number k
Whether top border limit
Are all leading zeros qdl
dp[pos][sum]:
> No top sector without leading zeros,
Current pos fill in the first place, the target figure sum contribution times when the answers appear to produce a total of
> Since the sum will be taken up to the same number and pos, the array size so as to open and like pos
Record sum when there are two cases:
(1) k! = 0 filled directly to see whether the target figures like digital
(2) k = 0 <1> are all leading zeros in front, but the number is not filled 0
<2> 0 is filled to the last digit, that is 0000000, to be considered at this time 0 appear once
<3> to rest the situation is the record number 0 appears
Code
#include<iostream> #include<cstdio> #include<string> #include<cstring> #include<algorithm> #include<cmath> #include<cstdlib> #include<queue> using namespace std; typedef long long ll; inline ll read() { ll ans=0; char last=' ',ch=getchar(); while(ch<'0'||ch>'9') last=ch,ch=getchar(); while(ch>='0'&&ch<='9') ans=ans*10+ch-'0',ch=getchar(); if(last=='-') ans=-ans; return ans; } ll a,b; ll c[20],len; ll dp[20][20]; ll dfs(ll pos,ll sum,ll k,bool limit,bool qdl) { if(pos<=0) return sum; if(!limit&&!qdl&&dp[pos][sum]!=-1) return dp[pos][sum]; ll ans=0; ll up=limit?c[pos]:9; for(int i=0;i<=up;i++) ans+=dfs(pos-1,sum+(k==0?(!qdl&&i==0)||(qdl&&i==0&&pos==0):(i==k)),k,limit&&(i==up),qdl&&(i==0)); if(!limit&&!qdl) dp[pos][sum]=ans; return ans; } ll sum(ll x,ll k) { memset(c,0,sizeof(c));len=0; memset(dp,-1,sizeof(dp)); while(x) { c[++len]=x%10; x/=10; } return dfs(len,0,k,1,1); } int main() { a=read();b=read(); for(int i=0;i<=9;i++) printf("%lld ",sum(b,i)-sum(a-1,i)); return 0; }
Double experience (simpler than the first one)
P1239 counter
answer
That is, only one is enough enough of a
Code
#include<iostream> #include<cstdio> #include<string> #include<cstring> #include<algorithm> #include<cmath> #include<cstdlib> #include<queue> using namespace std; typedef long long ll; inline ll read() { ll ans=0; char last=' ',ch=getchar(); while(ch<'0'||ch>'9') last=ch,ch=getchar(); while(ch>='0'&&ch<='9') ans=ans*10+ch-'0',ch=getchar(); if(last=='-') ans=-ans; return ans; } const ll mod=1e9+7; ll a,b,T,ans=0; ll c[30],len; ll dp[30][30]; ll dfs(ll pos,ll sum,ll k,bool limit,bool qdl) { if(pos<=0) return sum; if(!limit&&!qdl&&dp[pos][sum]!=-1) return dp[pos][sum]; ll ans=0; ll up=limit?c[pos]:9; for(int i=0;i<=up;i++) ans+=dfs(pos-1,sum+(k==0?(!qdl&&i==0)||(qdl&&i==0&&pos==0):(i==k)),k,limit&&(i==up),qdl&&(i==0)); if(!limit&&!qdl) dp[pos][sum]=ans; return ans; } ll sum(ll x,ll k) { memset(c,0,sizeof(c));len=0; memset(dp,-1,sizeof(dp)); while(x) { c[++len]=x%10; x/=10; } return dfs(len,0,k,1,1); } int main() { a=read(); for(int i=0;i<=9;i++) printf("%lld\n",sum(a,i)); return 0; }
Three times the experience
P4999 annoying math homework
answer
Take a look at the question: I want AC spicy! ! !
The reality is 90pt
As Ha! ! ! ! ! !
Modulo of pot, take a few more
I was too naive
Code
#include<iostream> #include<cstdio> #include<string> #include<cstring> #include<algorithm> #include<cmath> #include<cstdlib> #include<queue> using namespace std; typedef long long ll; inline ll read() { ll ans=0; char last=' ',ch=getchar(); while(ch<'0'||ch>'9') last=ch,ch=getchar(); while(ch>='0'&&ch<='9') ans=ans*10+ch-'0',ch=getchar(); if(last=='-') ans=-ans; return ans; } const ll mod=1e9+7; ll a,b,T,ans=0; ll c[20],len; ll dp[20][20]; ll dfs(ll pos,ll sum,ll k,bool limit,bool qdl) { if(pos<=0) return sum; if(!limit&&!qdl&&dp[pos][sum]!=-1) return dp[pos][sum]; ll ans=0; ll up=limit?c[pos]:9; for(int i=0;i<=up;i++) ans+=dfs(pos-1,sum+(k==0?(!qdl&&i==0)||(qdl&&i==0&&pos==0):(i==k)),k,limit&&(i==up),qdl&&(i==0)); if(!limit&&!qdl) dp[pos][sum]=ans; return ans; } ll sum(ll x,ll k) { memset(c,0,sizeof(c));len=0; memset(dp,-1,sizeof(dp)); while(x) { c[++len]=x%10; x/=10; } return dfs(len,0,k,1,1); } int main() { T=read(); while(T--) { ans=0; a=read();b=read(); for(int i=1;i<=9;i++) { ll tmp=((sum(b,i)-sum(a-1,i))%mod+mod)%mod;
ans=((ans+i*tmp%mod+mod)%mod+mod)%mod; } printf("%lld\n",ans%mod); } return 0; }