HDU 3549 不要62

不要62

链接:http://acm.hdu.edu.cn/showproblem.php?pid=2089

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

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 100 0 0
 
Sample Output
80
 题解:数位dp+记忆化搜索,dp[dep][f][t]表示从高到低到了第dep位的符合的数,遍历完后dep = 0,所以边界是dp[0] = 1;
f表示是否顶位, 如 : 2333 --> digit = 3 3 3 2
 dep = 3, i =2, 则 dep = 2时,能取0~9; dep = 3, i = 3, 顶了上界, 则dep = 2时,只能取0~3;
t表示上一位是否为6
#include<bits/stdc++.h>
using namespace std;
const int maxn = 100005;
int digit[8], dp[8][2][2];
int dfs(int dep, int f, int t){
    if(!dep)return 1;
    if(dp[dep][f][t])return dp[dep][f][t];
    int tmp = 0;
    int i = f ? digit[dep] : 9;
    for(; i >= 0; i--){
        if(i == 4)continue;
        if(t && i == 2)continue;
        if(i == 6)tmp += dfs(dep-1, f&&(i == digit[dep]), 1);
        else tmp += dfs(dep-1, f&&(i == digit[dep]), 0);
    }     
    return dp[dep][f][t] = tmp;
}
int solve(int b){
    int cnt = 0;
    memset(dp, 0, sizeof(dp));
    while(b){
        digit[++cnt] = b%10;
        b /= 10; 
    }
    return dfs(cnt, 1, 0);
}

int main(){
    int l,r;
    while(scanf("%d%d",&l,&r) == 2){
        if(!l && !r)break;
        printf("%d\n",solve(r) - solve(l-1));
    }
}
View Code

 

 

Bomb

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)

Problem Description
The counter-terrorists found a time bomb in the dust. But this time the terrorists improve on the time bomb. The number sequence of the time bomb counts from 1 to N. If the current number sequence includes the sub-sequence "49", the power of the blast would add one point.
Now the counter-terrorist knows the number N. They want to know the final points of the power. Can you help them?
 
Input
The first line of input consists of an integer T (1 <= T <= 10000), indicating the number of test cases. For each test case, there will be an integer N (1 <= N <= 2^63-1) as the description.

The input terminates by end of file marker.
 
Output
For each test case, output an integer indicating the final points of the power.
 
Sample Input
3 1 50 500
 
Sample Output
0 1 15
Hint
From 1 to 500, the numbers that include the sub-sequence "49" are "49","149","249","349","449","490","491","492","493","494","495","496","497","498","499", so the answer is 15.
题意:问1——n中含49的数的个数
题解:求其中不含49的数,再用总数减, 不能直接求含49的,不满足数位DP按位来的特点
#include <bits/stdc++.h>
using namespace std;
#define ll long long
int digit[25],len;
ll dp[25][2][2];
ll dfs(int dep, int f, int t){
    if(!dep)return 1;
    if(dp[dep][f][t]) return dp[dep][f][t];
    ll tmp = 0;
    int i = f ? digit[dep] : 9;
    for( ; i >= 0; i--){       
        if(t && i == 9)continue;
        if(i == 4) tmp += dfs(dep-1, f&&(i == digit[dep]), 1);
        else tmp += dfs(dep-1, f&&(i == digit[dep]), 0);
    }
    return dp[dep][f][t] = tmp;
    
}
ll query(ll n){
    memset(dp, 0, sizeof(dp));
    len = 0;
    while(n){
        digit[++len] = n%10;
        n /= 10;
    }
    return dfs(len, 1, 0);
}
int main(){
    ll n;
    int T;
    scanf("%d", &T);
    while(T--){        
        scanf("%I64d",&n);
        printf("%I64d\n", n - (query(n) - 1));     
    }    
}
View Code

 

posted @ 2018-02-26 11:26  Ed_Sheeran  阅读(188)  评论(0编辑  收藏  举报