HDU(3555),数位DP

题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=3555

Bomb

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 15372    Accepted Submission(s): 5563

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.

Author

[email protected]

Source

2010 ACM-ICPC Multi-University Training Contest(12)——Host by WHU

数位DP,之前做了一个相反的题目,结果WA了,后来发现数据不对,是long long 型数据。

#include <stdio.h>
#include <string.h>

int bit[25];
long long dp[25][2];

long long dfs(int len,bool is4,bool ismax)
{
    if(len==0) return 1;     ///搜索成功
    if(!ismax&&dp[len][is4]>=0) return dp[len][is4];

    long long cnt = 0;
    int maxnum = ismax? bit[len]:9;
    for(int i=0; i<=maxnum; i++)
    {
        if((is4&&i==9)) continue;
        cnt +=dfs(len-1,i==4,ismax&&i==maxnum);
    }
    return ismax?cnt:dp[len][is4]=cnt;
}

long long f(long long n)
{
    int len = 0;
    while(n)
    {
        bit[++len] = n%10;
        n/=10;
    }
    return dfs(len,false,true);
}

int main()
{
    int t;
    scanf("%d",&t);
    while(t--)
    {
        long long n;
        scanf("%lld",&n);
        memset(dp,-1,sizeof(dp));
        printf("%lld\n",n-f(n)+1);
    }
    return 0;
}
时间: 2024-08-10 01:55:58

HDU(3555),数位DP的相关文章

HDU 3555 数位dp入门

开始想用dp[i][j]来记录第i位j开头含有49的数的个数 但是init后并不知道如何进行cal 想了想可以用不要62的思想 当作不要49来做 然后减一下 就好 看网上的代码 不要62和这道题用的dp方法和cal都与我用的有很大不同 做完入门水题就去学习一下那种很正规的方法~ #include<stdio.h> #include<string.h> #include<algorithm> #include<map> #include<math.h&g

HDU 3555 数位dp

Bomb Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)Total Submission(s): 15072    Accepted Submission(s): 5441 Problem Description The counter-terrorists found a time bomb in the dust. But this time the terrorists

hdu 3555 数位dp水题 记忆化搜索做法

#include<iostream> #include<cstring> #include<cstdio> using namespace std ; const int maxn = 20; __int64 dp[maxn][3] ;//dp[i][flag] ,flag = 2,表示已经有49,flag == 1,表示没有49,这一位是4, int bit[maxn] ;    //flag == 0, 什么都没有 __int64 dfs(int pos , int

hdu 4734 数位dp

http://acm.hdu.edu.cn/showproblem.php?pid=4734 Problem Description For a decimal number x with n digits (AnAn-1An-2 ... A2A1), we define its weight as F(x) = An * 2n-1 + An-1 * 2n-2 + ... + A2 * 2 + A1 * 1. Now you are given two numbers A and B, plea

hdu 4352 数位dp(最长上升子序列的长度为k的个数)

http://acm.hdu.edu.cn/showproblem.php?pid=4352 Problem Description #define xhxj (Xin Hang senior sister(学姐)) If you do not know xhxj, then carefully reading the entire description is very important. As the strongest fighting force in UESTC, xhxj grew

hdu 3709 数位dp(小思维)

http://acm.hdu.edu.cn/showproblem.php?pid=3709 Problem Description A balanced number is a non-negative integer that can be balanced if a pivot is placed at some digit. More specifically, imagine each digit as a box with weight indicated by the digit.

hdu 4352 数位dp + 状态压缩

XHXJ's LIS Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2265    Accepted Submission(s): 927 Problem Description #define xhxj (Xin Hang senior sister(学姐)) If you do not know xhxj, then careful

hdu 4507 数位dp(求和,求平方和)

http://acm.hdu.edu.cn/showproblem.php?pid=4507 Problem Description 单身! 依然单身! 吉哥依然单身! DS级码农吉哥依然单身! 所以,他生平最恨情人节,不管是214还是77,他都讨厌! 吉哥观察了214和77这两个数,发现: 2+1+4=7 7+7=7*2 77=7*11 最终,他发现原来这一切归根到底都是因为和7有关!所以,他现在甚至讨厌一切和7有关的数! 什么样的数和7有关呢? 如果一个整数符合下面3个条件之一,那么我们就说

2017中国大学生程序设计竞赛 - 网络选拔赛 HDU 6156 数位DP

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6156 题意:如题. 解法:数位DP,暴力枚举进制之后,就转化成了求L,R区间的回文数的个数,这个直接做一个数位DP就好了.dp[jz][start][cur][state]表示jz进制下以start位起始到cur位状态为state(1表示已经回文,0表示没有回文)时回文数的个数. #include <bits/stdc++.h> using namespace std; typedef long

hdu 5179 数位dp

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5179 beautiful number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 198    Accepted Submission(s): 116 Problem Description Let A=∑ni=1ai?10n?i(1