"Digital DP" hdu3555 Bomb

Source: Internet
Author: User

Test instructions is looking for 0 to n how many of the numbers contain 49. Data range close to 10^20

DP State is 2-D dp[len][3]
Dp[len][0] represents the number of scenarios with a length of Len without 49
DP[LEN][1] represents the number of scenarios with a length of Len without 49 but with a number beginning with 9
DP[LEN][2] For a length of Len containing 49 of the scheme number

State transitions are as follows
Dp[i][0] = dp[i-1][0] * 10-dp[i-1][1]; Not include 49 if it does not contain 49 and can be filled in 0-9 but minus dp[i-1][1] because 4 will and 9 constitute 49
DP[I][1] = dp[i-1][0]; Not including starts with 9 This is just a 9 on the number that doesn't include 49.
DP[I][2] = dp[i-1][2] * ten + dp[i-1][1]; Include 49 already contains 49 of the number can be filled 0-9, or 9 the beginning of the fill 4

And then you start counting from high.

When counting to a bit, add dp[i-1][2] * Digit[i] is obviously right, because this one can fill 0-(digit[i]-1)
If this person is next to 49, then add dp[i-1][0] * Digit[i] is also clearly right.
If this one is not next to 49, but Digit[i] is bigger than 4, then when this one fills 4, you have to add dp[i-1][1]

--http://www.cnblogs.com/jackge/archive/2013/05/15/3080958.html

#include <cstdio>using namespace Std;typedef long long ll;ll f[21][3],n;void init () {f[0][0]=1;for (int i=1;i<= 20;++i)  {  f[i][0]=f[i-1][0]*10-f[i-1][1];  F[I][1]=F[I-1][0];  F[I][2]=F[I-1][2]*10+F[I-1][1];}  } int T;int Main () {init ();//freopen ("Hdu3555.in", "R", stdin); scanf ("%d", &t); int bit[23];for (; T;--T)  {  int m=0; ll ans=0; bool flag=0;  scanf ("%i64d", &n); ++n;  while (n)    {    bit[++m]=n%10;    n/=10;    }  bit[m+1]=bit[m+2]=0;  for (int i=m;i;--i)    {    if (bit[i+1]==9 && bit[i+2]==4)      flag=1;    ans+=f[i-1][2]* (LL) bit[i];    if (flag)      ans+= (LL) bit[i]*f[i-1][0];    else if (Bit[i] > 4)      ans+=f[i-1][1];    }  printf ("%i64d\n", ans);  } return 0;}

"Digital DP" hdu3555 Bomb

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.