POJ2282:The Counting Problem(数位DP)
2013-08-16 15:56
549 查看
Description
Giventwointegersaandb,wewritethenumbersbetweenaandb,inclusive,inalist.Yourtaskistocalculatethenumberofoccurrencesofeachdigit.Forexample,ifa=1024andb=1032,thelistwillbe
102410251026102710281029103010311032
thereareten0'sinthelist,ten1's,seven2's,three3's,andetc.
Input
Theinputconsistsofupto500lines.Eachlinecontainstwonumbersaandbwhere0<a,b<100000000.Theinputisterminatedbyaline`00',whichisnotconsideredaspartoftheinput.
Output
Foreachpairofinput,outputalinecontainingtennumbersseparatedbysinglespaces.Thefirstnumberisthenumberofoccurrencesofthedigit0,thesecondisthenumberofoccurrencesofthedigit1,etc.
SampleInput
SampleOutput
Giventwointegersaandb,wewritethenumbersbetweenaandb,inclusive,inalist.Yourtaskistocalculatethenumberofoccurrencesofeachdigit.Forexample,ifa=1024andb=1032,thelistwillbe
102410251026102710281029103010311032
thereareten0'sinthelist,ten1's,seven2's,three3's,andetc.
Input
Theinputconsistsofupto500lines.Eachlinecontainstwonumbersaandbwhere0<a,b<100000000.Theinputisterminatedbyaline`00',whichisnotconsideredaspartoftheinput.
Output
Foreachpairofinput,outputalinecontainingtennumbersseparatedbysinglespaces.Thefirstnumberisthenumberofoccurrencesofthedigit0,thesecondisthenumberofoccurrencesofthedigit1,etc.
SampleInput
110 44497 346542 11991748 14961403 1004503 1714190 1317854 1976494 10011960 00
SampleOutput
1211111111 851851851851909696969593 404040931368240404040 115666215215214205205154105106 1611319201142020191916 107105100101101197200200200200 4131133503503503502502417402412 19651218610487939797142196 3981375398398405499499495488471 2941256296296296296287286286247
题意:求出区间内0~9的个数
思路:dp[i][j],代表长度为i的数字里面共有几个j
#include<stdio.h>
#include<string.h>
#include<algorithm>
usingnamespacestd;
__int64dp[10][10];
__int64pow(__int64a,__int64b)
{
__int64ans=1,i;
for(i=1;i<=b;i++)
ans*=a;
returnans;
}
voidset()
{
memset(dp,0,sizeof(dp));
for(__int64i=1;i<=9;i++)
{
for(__int64j=0;j<=9;j++)
dp[i][j]=dp[i-1][j]*10+pow(10,i-1);
}
}
__int64solve(__int64n,__int64pos)
{
__int64s[15],len=0,i,tem;
while(n)
{
s[++len]=n%10;
n/=10;
}
s[0]=0;
__int64ans=0,cnt=0;
for(i=len;i>=1;i--)
{
if(pos==0&&i==len)//找出0的情况
{
ans+=dp[i-1][pos]*(s[i]-1);
tem=pow(10,i-1)-1;
ans+=solve(tem,0);
}
else//不为0,则后面的个数要进行s[i]次相加
ans+=dp[i-1][pos]*s[i];
ans+=cnt*pow(10,i-1)*s[i];//计算前面的与要求的数字相同的被运算了几次
if(pos)
{
if(s[i]>pos)
ans+=pow(10,i-1);//s[i]>tmp,则s[i]==tmp时,要将刺猬相加pow(10,i-1)次
if(s[i]==pos)
cnt++;
}
elseif(i!=len)
{
if(s[i]>pos)
ans+=pow(10,i-1);
if(s[i]==pos)
cnt++;
}
}
returnans+cnt;
}
intmain()
{
set();
__int64l,r,i;
while(~scanf("%I64d%I64d",&l,&r),l+r)
{
if(l>r)
swap(l,r);
printf("%I64d",solve(r,0)-solve(l-1,0));
for(i=1;i<=9;i++)
printf("%I64d",solve(r,i)-solve(l-1,i));
printf("\n");
}
return0;
}
相关文章推荐
- poj 2282 The Counting Problem (数位DP)
- POJ 2282 The Counting Problem (数位dp)
- poj 2282 The Counting Problem && poj 3286 How many 0's? (数位dp)
- The Counting Problem - POJ 2282 数位dp
- 小白算法练习 hdu 不要62 POJ 2282 the Counting problem 数位dp
- POJ-2282-The Counting Problem
- (Realx 数论1.28)POJ 2282 The Counting Problem(区间统计数字:统计a、b之间各个数字(0~9)出现的次数)
- poj2282 The Counting Problem 数位dp
- POJ 3286 How many 0's? / 2282 The Counting Problem 排列组合统计数字
- POJ题目2282 The Counting Problem(数学)
- POJ 2282 The Counting Problem & POJ 3286 How many 0's?(按位计算贡献)
- POJ 2282 The Counting Problem,组合数学
- POJ 2282 && HDU 1663 The Counting Problem(数论)
- POJ 2282 The Counting Problem
- POJ 2282 The Counting Problem
- TOJ 1183 The Counting Problem 数位dp
- [POJ] 2282 -> The Counting Problem
- poj2282 The Counting Problem
- poj 2282 The Counting Problem & 3286 How many 0's?
- POJ 2282-The Counting Problem(组合数学_区间计数)