当前位置: 代码迷 >> 综合 >> The Counting Problem(POJ-2282)(模拟)
  详细解决方案

The Counting Problem(POJ-2282)(模拟)

热度:67   发布时间:2023-11-19 10:25:08.0

文章目录

  • 题目
  • 思路
  • 代码

题目

Vjudge
POJ
题目大意
各你两个数 a,ba,ba,b (0&lt;a,b&lt;1000000000 &lt; a, b &lt; 1000000000<a,b<100000000),统计 [a,b][a,b][a,b]中数码 0?90-90?9 分别出现的数量
ininin

1 10
44 497
346 542
1199 1748
1496 1403
1004 503
1714 190
1317 854
1976 494
1001 1960
0 0

outoutout

1 2 1 1 1 1 1 1 1 1
85 185 185 185 190 96 96 96 95 93
40 40 40 93 136 82 40 40 40 40
115 666 215 215 214 205 205 154 105 106
16 113 19 20 114 20 20 19 19 16
107 105 100 101 101 197 200 200 200 200
413 1133 503 503 503 502 502 417 402 412
196 512 186 104 87 93 97 97 142 196
398 1375 398 398 405 499 499 495 488 471
294 1256 296 296 296 296 287 286 286 247

思路

我们令 f(n)f(n)f(n)0?n0-n0?n 中数码 0?90-90?9 分别出现的数量,那么答案就是 f(b)?f(a?1)f(b)-f(a-1)f(b)?f(a?1)
那怎么做呢
比如一个数 4123
我们可以拆成 4000+100+20+3来统计
比如千位上的4出现次数就是123+1
比如千位上的3出现次数就是1000
如果允许出现前导0,那么0的答案减去1110即可
那么从左至右扫一遍就可以了

代码

#include<set>
#include<map>
#include<stack>
#include<cmath>
#include<queue>
#include<deque>
#include<cstdio>
#include<bitset>
#include<vector>
#include<climits>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
#define LL long long
int read(){
    int f=1,x=0;char c=getchar();while(c<'0'||'9'<c){
    if(c=='-')f=-1;c=getchar();}while('0'<=c&&c<='9') x=(x<<3)+(x<<1)+c-'0',c=getchar();return f*x;
}
#define MAXN 100
#define INF 0x3f3f3f3f
LL num[2][10];
char S[MAXN+5];
int Pow[10]={
    1},Pow9[10]={
    1};
int main(){
    int a[2];for(int i=1;i<=9;i++)Pow[i]=Pow[i-1]*10,Pow9[i]=Pow9[i-1]*9;while(1){
    a[0]=read(),a[1]=read();if(a[0]==0&&a[1]==0)break;if(a[0]>a[1])swap(a[0],a[1]);a[0]--;memset(num,0,sizeof(num));for(int t=0;t<=1;t++){
    memset(S,0,sizeof(S));sprintf(S+1,"%d",a[t]);int n=strlen(S+1);for(int i=1;i<n;i++)num[t][0]-=Pow[i];for(int i=1;i<=n;i++){
    int p=S[i]-'0';for(int j=0;j<p;j++)num[t][j]+=Pow[n-i];for(int j=0;j<=9;j++)num[t][j]+=(n-i)*p*Pow[n-i]/10;if(i+1<=n)num[t][p]+=atoi(S+i+1);num[t][p]++;}}for(int i=0;i<=9;i++)printf("%lld ",num[1][i]-num[0][i]);puts("");}return 0;
}
  相关解决方案