小白算法练习 hdu 不要62 POJ 2282 the Counting problem 数位dp

来源:互联网 发布:筑业家装软件 编辑:程序博客网 时间:2024/06/05 15:41

不要62

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 45529    Accepted Submission(s): 17111


Problem Description
杭州人称那些傻乎乎粘嗒嗒的人为62(音:laoer)。
杭州交通管理局经常会扩充一些的士车牌照,新近出来一个好消息,以后上牌照,不再含有不吉利的数字了,这样一来,就可以消除个别的士司机和乘客的心理障碍,更安全地服务大众。
不吉利的数字为所有含有4或62的号码。例如:
62315 73418 88914
都属于不吉利号码。但是,61152虽然含有6和2,但不是62连号,所以不属于不吉利数字之列。
你的任务是,对于每次给出的一个牌照区间号,推断出交管局今次又要实际上给多少辆新的士车上牌照了。
 

Input
输入的都是整数对n、m(0<n≤m<1000000),如果遇到都是0的整数对,则输入结束。
 

Output
对于每个整数对,输出一个不含有不吉利数字的统计个数,该数值占一行位置。
 

Sample Input
1 1000 0
 

Sample Output
80
 
代码:
#include<iostream>#include<cstring>#include<stdio.h>using namespace std;#define Max 10000typedef long long ll;long long dp[Max][10];int a[100];void getdp(){  //准备工作memset(dp,0,sizeof(dp));  dp[0][0]=1;for(int i=1;i<=9;i++)  {for(int j=0;j<=9;j++){for(int k=0;k<=9;k++){if(j!=4 && !(j==6&&k==2))dp[i][j]+=dp[i-1][k];}}}}ll solve(int x){ //分解工作int len=0;while(x>0){a[++len]=x%10;x/=10;}a[len+1]=0;ll ans=0;for(int i=len;i;i--){for(int j=0;j<a[i];j++){if(j!=4 && !( a[i+1]==6 && j==2 ) ){ans+=dp[i][j];}}if(a[i]==4 || (a[i]==2 && a[i+1]==6))break;}return ans;}int main(){getdp();ll left,right;while(cin>>left>>right){if(left==right && left==0) break;printf("%lld\n",solve(right+1)-solve(left));}} 

 

The Counting Problem
Time Limit: 3000MS Memory Limit: 65536KTotal Submissions: 4728 Accepted: 2422

Description

Given two integers a and b, we write the numbers between a and b, inclusive, in a list. Your task is to calculate the number of occurrences of each digit. For example, if a = 1024 and b = 1032, the list will be 
1024 1025 1026 1027 1028 1029 1030 1031 1032

there are ten 0's in the list, ten 1's, seven 2's, three 3's, and etc.

Input

The input consists of up to 500 lines. Each line contains two numbers a and b where 0 < a, b < 100000000. The input is terminated by a line `0 0', which is not considered as part of the input.

Output

For each pair of input, output a line containing ten numbers separated by single spaces. The first number is the number of occurrences of the digit 0, the second is the number of occurrences of the digit 1, etc.

Sample Input

1 1044 497346 5421199 17481496 14031004 5031714 1901317 8541976 4941001 19600 0

Sample Output

1 2 1 1 1 1 1 1 1 185 185 185 185 190 96 96 96 95 9340 40 40 93 136 82 40 40 40 40115 666 215 215 214 205 205 154 105 10616 113 19 20 114 20 20 19 19 16107 105 100 101 101 197 200 200 200 200413 1133 503 503 503 502 502 417 402 412196 512 186 104 87 93 97 97 142 196398 1375 398 398 405 499 499 495 488 471294 1256 296 296 296 296 287 286 286 247

代码

我想对下面的代码解释一下还是比较难理解的;
如果数字576想找4;第一位数是5那么0-99,100-199,200-299,300-399,400-499里面的数是一样的,
由于dp[2][4]存放的是两位数,所以当然是5*dp[2][4].然后你再考虑如果找的数是<5的,比如说上面的4,
400-499的第一位是4,要加上100个数..然后考虑第二位数。
但是如果576想找0;由于开头字不可以是0,所以只能dp[2][4]*(5-1),剩下的0-99,就递归计算..一次类推
#include<iostream>#include<cstring>using namespace std;typedef long long ll;long long dp[10][10];ll cm(int a,int b){ll ans=1;while(b){if(b&1){ans=ans*a;b--;}b/=2;a=a*a;}return ans;}void getdp(){memset(dp,0,sizeof(dp));  for(ll i=1;i<10;i++){for(ll j=0;j<10;j++){dp[i][j]=dp[i-1][j]*10+cm(10,i-1);}}}ll solve(ll x,int pos){  //不是零的情况 int a[15];memset(a,0,sizeof(a));ll ans=0;ll cnt=0;int len=0;while(x){a[++len]=x%10;x/=10;}a[len+1]=0;for(int i=len;i;i--){ans+=dp[i-1][pos]*a[i];ans+=cnt*cm(10,i-1)*a[i];if(a[i]>pos)ans+=cm(10,i-1);else if(a[i]==pos){cnt++;}else{//pass}}return ans;}ll solve_0(ll x,int pos=0){ //是零的情况 int a[15];ll ans=0;ll cnt=0;int len=0;while(x){a[++len]=x%10;x/=10;}a[0]=0;for(int i=len;i;i--){if(i==len){ans+=dp[i-1][0]*(a[i]-1);int tem=cm(10,i-1)-1;ans+=solve_0(tem,0); }elseans+=dp[i-1][pos]*a[i]; ans+=cnt*cm(10,i-1)*a[i];if(i!=len){if(a[i]>0)ans+=cm(10,i-1);if(a[i]==0)cnt++;else{//pass}}}return ans;}int main(){ll l,r;getdp();while(cin>>l>>r &&l!=EOF){if(l==r && r==0){break;}if(l>r){int temp=l;l=r;r=temp;}for(int i=0;i<=9;i++){if(i!=0)cout<<solve(r+1,i)-solve(l,i)<<" ";else{cout<<solve_0(r+1,0)-solve_0(l,0)<<" ";}}cout<<endl;}}