UVA 1640(DFS)
题意:给你a,b两个数 问你a b区间中0 9出现的次数
其实就是求1-n中0-9出现的次数 ans[n] 答案就是ans[b]-ans[a-1]
怎么求的话看代码吧
1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<cstring> 5 #include<cstdlib> 6 #include<string.h> 7 #include<set> 8 #include<vector> 9 #include<queue> 10 #include<stack> 11 #include<map> 12 #include<cmath> 13 typedef long long ll; 14 typedef unsigned long long LL; 15 using namespace std; 16 const double PI=acos(-1.0); 17 const double eps=0.0000000001; 18 const int N=500000+100; 19 const ll mod=1e9+7; 20 const int INF=0x3f3f3f3f; 21 int a[N]; 22 int b[N]; 23 int ans[N]; 24 void DFS(int n,int m){ 25 int x=n/10; 26 int y=n%10; 27 for(int i=0;i<=9;i++)ans[i]=ans[i]+x*m; 28 for(int i=1;i<=y;i++)ans[i]=ans[i]+m; 29 if(x==0)return; 30 DFS(x-1,m*10); 31 while(x){ 32 int t=x%10; 33 ans[t]=ans[t]+m*(y+1); 34 x=x/10; 35 } 36 37 } 38 int main(){ 39 int n,m; 40 while(scanf("%d%d",&n,&m)!=EOF){ 41 if(n==0&&m==0)break; 42 memset(a,0,sizeof(a)); 43 memset(b,0,sizeof(b)); 44 memset(ans,0,sizeof(ans)); 45 int t1=0; 46 int t2=0; 47 if(n>m)swap(n,m); 48 DFS(n-1,1); 49 for(int i=0;i<=9;i++)a[i]=ans[i]; 50 memset(ans,0,sizeof(ans)); 51 DFS(m,1); 52 for(int i=0;i<=9;i++)b[i]=ans[i]; 53 for(int i=0;i<=8;i++)cout<<b[i]-a[i]<<" "; 54 cout<<b[9]-a[9]<<endl; 55 56 57 } 58 }