HDU 3555 Bomb (digital DP)

Source: Internet
Author: User

Problem Solving Ideas:

The number of numbers containing 49 in 0 to N

Dp[i][0] indicates that the length is I and does not contain 49.

DP[I][1] indicates that the length is I and does not contain 49 and the highest position is 9

DP[I][2] Indicates a length of I and contains 49;

Processing from high to low

#include <iostream> #include <cstring> #include <cstdlib> #include <cstdio> #include <cmath > #include <vector> #include <queue> #include <stack> #include <map> #include <set># Define LL long long using namespace std;  LL dp[25][3];//preprocessing dp array void init () {dp[0][0] = 1;dp[0][1] = dp[0][2] = 0;for (int i=1;i<25;i++) {dp[i][0] = dp[i-1][0] * 10 -Dp[i-1][1];DP [i][1] = dp[i-1][0];DP [i][2] = dp[i-1][2] * ten + dp[i-1][1];}} int bit[25]; ll solve (ll N) {memset (bit,0,sizeof (bit)); LL ans = 0;int len = 0;while (n) {Bit[++len] = n% 10;n/= 10;} BOOL flag = false;for (int i=len;i>=1;i--) {ans + = bit[i] * dp[i-1][2];//current bit can be placed 0 ~ bit[i]-1, total is bit[i] number if (flag) ans + = d P[I-1][0] * bit[i];//If 49 is present, the current bit can be placed Bit[i] number else if (Bit[i] > 4) ans + = dp[i-1][1];//If there is no previous 49, then the current must be 4if (bit[i+1] = = 4 && Bit[i] = = 9) flag = true;} if (flag) ans++;//if the last two bits are 49, plus one, i.e. n itself is the number that satisfies the condition of the return ans; int main () {int t;scanf ("%d", &t), Init (), while (t--) {LL n;scanf ("%i64d", &N);p rintf ("%i64d\n", Solve (n));}  return 0;}


HDU 3555 Bomb (digital DP)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.