UVA 1640 The Counting Problem

https://vjudge.net/problem/UVA-1640

topic

To two numbers $ a $, $ b $, ask the $ [a, b] $ numbers written between the row number 0,1,2,3,4,5,6,7,8,9 each occurrence how many times. $ N \ leqslant 10 ^ 8 $, 500 sets of data

answer

Two cases, to consider every ......

For example, if a number is 203, 1 appears to consider how many times.

Consider a bit, then the situation is $ 20 \ boxed {1} \ sim \ phantom {00} \ boxed {1} $, $ 20-0 + 1 $ times

Consider ten, where it appears that the $ 1 \ boxed {1} 9 \ sim \ phantom {0} \ boxed {1} 0 $, $ 19-0 + 1 $ times

Consider one hundred, then the situation arise that $ \ boxed {1} 99 \ sim \ boxed {1} 00 $, $ 99-0 + 1 $ times

Other cases have to consider ...... This is just an example = =

AC Code

#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<cassert>
#include<algorithm>
#define REP(i,a,b) for(register int i=(a); i<(b); i++)
#define REPE(i,a,b) for(register int i=(a); i<=(b); i++)
#define PERE(i,a,b) for(register int i=(a); i>=(b); i--)
#ifdef sahdsg
#define DBG(...) printf(__VA_ARGS__)
#else
#define DBG(...) (void)0
#endif
#define ln log
using namespace std;
typedef long long LL;
int a,b;
int f0(int x) {
	int k;
	int ans=0;
	for(int i=1; i<=x; i*=10) {
		k = x/i/10*i;
		if(k*10+i-1<=x) k+=i-1;
		else k+=x%i;

		if(k>=i) ans+=k-i+1;
	}
	return ans;
}
int f(int x, int q) {
	int k;
	int ans=0;
	for(int i=1; i*q<=x; i*=10) {
		k = x/i/10*i;
		if(k*10+i*q>x) k-=i;

		if(k*10+i*q+i-1<=x) k+=i-1;
		else k+=x%i;

		ans+=k+1;
	}
	return ans;
}
int main() {
	
	while(~scanf("%d%d", &a, &b) && a) {
		if(a>b) swap(a,b);
		printf("%d", f0(b)-f0(a-1));
		REP(i,1,10) {
			printf(" %d", f(b,i)-f(a-1,i));
		}
		putchar('\n');
	}
	return 0;
}

 

Guess you like

Origin www.cnblogs.com/sahdsg/p/11869625.html