FZOJ2113+月赛

题意:

求a到b的数中,共有多少个1

先可以预处理出一位,两位。。等这些数的1的个数,即 sum。

然后对于某个特定的 n 位数,假设第一位是1 然后求出有多少种可能的情况(即有多少种可能的数),,然后再枚举第二位,第三位。。。。

View Code
  1 /*
  2 找规律
  3 */
  4 #include<stdio.h>
  5 #include<string.h>
  6 #include<stdlib.h>
  7 #include<algorithm>
  8 #include<iostream>
  9 #include<queue>
 10 #include<vector>
 11 #include<map>
 12 #include<math.h>
 13 typedef long long ll;
 14 //typedef __int64 ll;
 15 
 16 ll sum[ 24 ];
 17 
 18 ll fpow( ll a,ll b ){
 19     ll sum = 1;
 20     for( ll i=1;i<=b;i++ )
 21         sum *= a;
 22     return sum;
 23 }
 24 
 25 void init(){
 26     sum[ 1 ] = 1;
 27     for( int i=2;i<=19;i++ ){
 28         ll tt = 0;
 29         for( int j=1;j<=i;j++ ){
 30             if( j==1 ) tt+=fpow(10,i-1);
 31             else tt+=9*fpow(10,i-2);
 32         }
 33         sum[i]=tt;
 34         //printf("sum[ %d ]= %lld \n",i,sum[i]);
 35     }
 36 }
 37 
 38 ll ansa,ansb,ans;
 39 
 40 ll get_sum( char num[] ){
 41     int len = strlen( num );
 42     ll res = 0;
 43     if( len==1 ) return 1;
 44     for( int i=1;i<len;i++ )
 45         res += sum[ i ];
 46     //printf("res:%lld\n",res);
 47     for( int i=0;i<len;i++ ){//枚举首位
 48         ll tt;
 49         ll tmp;
 50         if( i==0 ){
 51             tmp = 1;
 52             tt = 0;
 53             if( num[ i ]=='1' ){
 54                 for( int j=len-1;j>=1;j-- ){
 55                     tt += (num[j]-'0')*tmp;
 56                     tmp*=10;
 57                 }
 58                 res += (tt+1);
 59             }
 60             else {
 61                 tmp = 1;
 62                 tt = 0;
 63                 for( int j=len-1;j>=1;j-- ){
 64                     tt += 9*tmp;
 65                     tmp*=10;
 66                 }
 67                 res += (tt+1);
 68             }
 69             //printf("i==0:tt+1:%lld\n",tt+1);
 70         }//特判第一位是否为1
 71         else {
 72             if( num[i]>'1' ){
 73                 ll tt1,tt2;
 74                 tt1=tt2=0;
 75                 tmp = 1;
 76                 for( int j=len-1;j>=0;j-- ){
 77                     if( j==i ) continue;//tt1 += tmp;
 78                     else if( j>i ) ;
 79                     else if( j==0 ) tt1 += tmp;
 80                     else ;
 81                     tmp *= 10;
 82                 }
 83                 //the min
 84                 tmp = 1;
 85                 for( int j=len-1;j>=0;j-- ){
 86                     if( j==i ) continue;//tt2 += tmp;
 87                     else if( j>i ) tt2 += 9*tmp;
 88                     else tt2 += (num[j]-'0')*tmp;
 89                     tmp *= 10;
 90                 }
 91                 //the max
 92                 res += (tt2-tt1+1);
 93                 //printf("num[i]>1:tt2-tt1+1:%lld\n",tt2-tt1+1);
 94             }
 95             else if( num[i]=='1' ){
 96                 ll tt1,tt2;
 97                 tt1 = tt2 = 0;
 98                 tmp = 1;
 99                 for( int j=len-1;j>=0;j-- ){
100                     if( j==i ) continue;//tt1 += tmp;
101                     else if( j>i ) ;
102                     else if( j==0 ) tt1 += tmp;
103                     else ;
104                     tmp *= 10;
105                 }
106                 //the min
107                 tmp = 1;
108                 for( ll j=len-1;j>=0;j-- ){
109                     if( j==i ) continue;//tt2 += tmp;
110                     else tt2 += (num[j]-'0')*tmp;
111                     tmp *= 10;
112                 }
113                 res += (tt2-tt1+1);
114                 //printf("num[i]==1:tt2-tt1+1:%lld\n",tt2-tt1+1);
115             }
116             else if( num[i]=='0' ){
117                 int f = -1;
118                 for( int j=i-1;j>=0;j-- ){
119                     if( num[j]=='1'&&j==0 ) continue;
120                     if( num[j]>='1' ){
121                         f = j;
122                         break;
123                     }
124                 }
125                 if( f==-1 ) continue;
126                 //printf("f:%d\n",f);
127                 ll tt1,tt2;
128                 tt1 = tt2 = 0;
129                 tmp = 1;
130                 for( int j=len-1;j>=0;j-- ){
131                     if( j==i ) continue;//tt1 += tmp;
132                     else if( j>i ) ;
133                     else if( j==0 ) tt1 += tmp;
134                     else ;
135                     tmp *= 10;
136                 }
137                 tmp = 1;
138                 for( int j=len-1;j>=0;j-- ){
139                     if( j==i ) continue;//tt2 += tmp;
140                     else if( j>f ) tt2 += 9*tmp;
141                     else if( j==f ) tt2 += (num[j]-'1')*tmp;
142                     else tt2 += (num[j]-'0')*tmp;
143                     tmp *= 10;
144                 }
145                 res += (tt2-tt1+1);
146                 //printf("num[i]==0:tt2-tt1+1:%lld\n",tt2-tt1+1);
147             }        
148         }
149     }
150     /*
151     if( num[ len-1 ]=='0' ){
152         int f = -1;
153         for( int j=len-2;j>=0;j-- ){
154             if( num[j]=='1'&&j==0 ) continue;
155             if( num[j]>='1' ){
156                 f = j;
157                 break;
158             }
159         }
160         if( f!=-1 ){
161             ll tt1,tt2,tmp;
162             tt1 =tt2 =0;
163             tmp = 1;
164             for( int j=len-2;j>=0;j-- ){
165                 if( j==len-1 ) tt1 += tmp;
166                 else if( j==0 ) tt1 += tmp;
167                 else ;
168                 tmp *= 10;
169             }
170             tmp = 1;
171             for( int j=len-2;j>=0;j-- ){
172                 if( j==len-1 ) tt2 += tmp;
173                 else if( j>f ) tt2 += tmp*9;
174                 else if( j==f )    tt2 += tmp*(num[j]-'1');
175                 else tt2 += tmp*(num[j]-'0');
176                 tmp *= 10;
177             }
178             res += (tt2-tt1+1);
179             //printf("i==len-1:tt2-tt1+1:%lld\n",tt2-tt1+1);
180         }
181     }
182     else {
183         ll tt1,tt2,tmp;
184         tt1 =tt2 =0;
185         tmp = 1;
186         for( int j=len-2;j>=0;j-- ){
187             if( j==len-1 ) tt1 += tmp;
188             else if( j==0 ) tt1 += tmp;
189             else ;
190             tmp *= 10;
191         }
192         tmp = 1;
193         for( int j=len-2;j>=0;j-- ){
194             if( j==len-1 ) tt2 += tmp;
195             else tt2 += tmp*(num[j]-'0');
196             tmp *= 10;
197         }
198         res += (tt2-tt1+1);
199         //printf("i==len-1:tt2-tt1+1:%lld\n",tt2-tt1+1);
200     }
201 */
202     return res ;
203 }
204     
205 int main(){
206     init();
207     char a[ 24 ],b[ 24 ];
208     while( scanf("%s%s",a,b)!=EOF ){
209         int lena,lenb;
210         lena = strlen( a );
211         lenb = strlen( b );
212         if( lena>lenb ){
213             char tmp[24];
214             strcpy( tmp,a );
215             strcpy( a,b );
216             strcpy( b,tmp );
217         }
218         else if( lena==lenb&&strcmp(a,b)>0 ){
219             char tmp[24];
220             strcpy( tmp,a );
221             strcpy( a,b );
222             strcpy( b,tmp );
223         }
224         //printf("a:%s b:%s\n",a,b);
225         ansa = get_sum( a );
226         ansb = get_sum( b );
227         //printf("%lld %lld \n",ansa,ansb);
228         if( strcmp( a,"1")==0 ){
229             printf("%lld\n",ansb);
230             continue;
231         }
232         //if( strcmp( b,"1" )==0 ){
233             //printf("1\n");
234             //continue;
235         //}
236         ll cnt = 0;
237         for( ll i=0;a[i]!='\0';i++ )
238             if( a[i]=='1' )    
239                 cnt++;
240         ans = ansb - ansa + cnt;
241         printf("%lld\n",ans);
242     }
243     return 0;
244 }

写得我是一把心酸泪啊。。。。。。。

keep moving...
原文地址:https://www.cnblogs.com/xxx0624/p/3012256.html