Faulty OdometerTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2328 Accepted Submission(s): 1601 Problem Description You are given a car odometer which displays the miles traveled as an integer. The odometer has a defect, however: it proceeds from the digit 2 to the digit 4 and from the digit 7 to the digit 9, always skipping over the digit 3 and 8. This defect shows up in all positions (the one's, the ten's, the hundred's, etc.). For example, if the odometer displays 15229 and the car travels one mile, odometer reading changes to 15240 (instead of 15230).
Input Each line of input contains a positive integer in the range 1..999999999 which represents an odometer reading. (Leading zeros will not appear in the input.) The end of input is indicated by a line containing a single 0. You may assume that no odometer reading will contain the digit 3 and 8.
Output Each line of input will produce exactly one line of output, which will contain: the odometer reading from the input, a colon, one blank space, and the actual number of miles traveled by the car.
Sample Input 15 2005 250 1500 999999 0
Sample Output 15: 12 2005: 1028 250: 160 1500: 768 999999: 262143
Source 2012 ACM/ICPC Asia Regional Tianjin Online
Recommend liuyiding |
模板题...
#include<iostream>
#include<math.h>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<vector>
#include<queue>
#include<map>
#include<set>
using namespace std;
#define B(x) (1<<(x))
typedef long long ll;
void cmax(int& a,int b){ if(b>a)a=b; }
void cmin(int& a,int b){ if(b<a)a=b; }
const int oo=0x3f3f3f3f;
const int MOD=1000000007;
const int maxn=5100;
const int maxm=21000;
ll dp[66][66][66];
int bit[66];
ll dfs(int pos,int p1,int is,int f){
//参数表明状态转移;
if(pos<1) return is;//数位dp模板
if(!f&&dp[pos][p1][is]!=-1) return dp[pos][p1][is];
int last= f ? bit[pos] : 9;
ll res=0;//从0到last遍历;
for(int i=0;i<=last;i++){
res+=dfs(pos-1,i,is||(i==3||i==8),f&&i==last);
}//状态转移res+=dfs(pos-1,i,is||(p1==4&&i==9),f&i==last);
if(!f) dp[pos][p1][is]=res;
return res;//处理
}
ll Cnt(ll x){
int len=0;
while(x){
bit[++len]=x%10;//分解数的每个位数;
x/=10;
}//先分解每个位置;
return dfs(len,0,0,1);//处理1-x有多少个含有666的数//深搜处理数x;
//赋予其初始状态;
}
int main(){
ll n,n1,T;
memset(dp,-1,sizeof dp);
while(scanf("%lld",&n1)&&n1)
printf("%lld: %lld\n",n1,n1-Cnt(n1));
return 0;
}