bzoj1833 [ZJOI2010]count 数字计数
我是DP弱者!!!我是DP弱者!!!我是DP弱者!!!
调了好久,数位DP好恼火QAQ
1 #include<algorithm> 2 #include<iostream> 3 #include<cstdlib> 4 #include<cstring> 5 #include<cstdio> 6 #include<string> 7 #include<cmath> 8 #include<ctime> 9 #include<queue> 10 #include<stack> 11 #include<map> 12 #include<set> 13 #define rre(i,r,l) for(int i=(r);i>=(l);i--) 14 #define re(i,l,r) for(int i=(l);i<=(r);i++) 15 #define Clear(a,b) memset(a,b,sizeof(a)) 16 #define inout(x) printf("%d",(x)) 17 #define douin(x) scanf("%lf",&x) 18 #define strin(x) scanf("%s",(x)) 19 #define LLin(x) scanf("%lld",&x) 20 #define op operator 21 #define CSC main 22 typedef unsigned long long ULL; 23 typedef const int cint; 24 typedef long long LL; 25 using namespace std; 26 void inin(int &ret) 27 { 28 ret=0;int f=0;char ch=getchar(); 29 while(ch<'0'||ch>'9'){if(ch=='-')f=1;ch=getchar();} 30 while(ch>='0'&&ch<='9')ret*=10,ret+=ch-'0',ch=getchar(); 31 ret=f?-ret:ret; 32 } 33 LL ten[13]; 34 LL dp[13][10][10]; 35 void getpre() 36 { 37 ten[0]=1; 38 re(i,1,12)ten[i]=ten[i-1]*10; 39 re(i,0,9)dp[1][i][i]=1; 40 re(i,2,12)re(j,0,9)re(k,0,9) 41 { 42 dp[i][j][k]=(j==k)?ten[i-1]:0; 43 re(t,0,9)dp[i][j][k]+=dp[i-1][t][k]; 44 } 45 } 46 int getbit(LL x) 47 { 48 int ret=0; 49 while(x) 50 x/=10,ret++; 51 return ret; 52 } 53 LL ans[10]; 54 int num[15]; 55 void calc(LL temp,int bit,bool qian) 56 { 57 if(!bit)return; 58 if(qian) 59 re(i,0,num[bit]-1) 60 re(j,0,9) 61 ans[j]+=dp[bit][i][j]; 62 else re(i,1,num[bit]-1)re(j,0,9)ans[j]+=dp[bit][i][j]; 63 ans[num[bit]]+=temp%ten[bit-1]+1; 64 calc(temp,bit-1,1); 65 } 66 void calc2(LL temp,int bit,bool qian) 67 { 68 if(!bit)return; 69 if(qian) 70 re(i,0,num[bit]-1) 71 re(j,0,9) 72 ans[j]-=dp[bit][i][j]; 73 else re(i,1,num[bit]-1)re(j,0,9)ans[j]-=dp[bit][i][j]; 74 ans[num[bit]]-=temp%ten[bit-1]+1; 75 calc2(temp,bit-1,1); 76 } 77 int main() 78 { 79 getpre(); 80 LL l,r; 81 scanf("%lld%lld",&l,&r); 82 int len=0; 83 LL temp=r; 84 while(r)num[++len]=r%10,r/=10; 85 calc(temp,len,0); 86 re(i,1,len-1)re(j,0,9)re(k,1,9)ans[j]+=dp[i][k][j]; 87 len=0,temp=l-1;l--; 88 while(l)num[++len]=l%10,l/=10; 89 calc2(temp,len,0); 90 re(i,1,len-1)re(j,0,9)re(k,1,9)ans[j]-=dp[i][k][j]; 91 re(i,0,9)printf("%lld%c",ans[i],i==9?'\n':' '); 92 return 0; 93 }