Basic digital dp questions

Source: Internet
Author: User

[Cpp]
/********************
Language: c ++
Author: pirates
Problem: hdu2089
Style: Digital dp
*********************/
 
 
# Include <iostream>
# Include <stdio. h>
# Include <algorithm>
Using namespace std;
# Define Maxx 1000010
Int dp [10] [10];
// Dp [I] [0] indicates the number of unlucky ones
// Dp [I] [1] indicates the number with the highest bit of 2 and an unlucky number.
// Dp [I] [2] indicates the total number of unlucky digits. Neither 62 nor 4 can exist.
Void Init ()
{
Dp [0] [0] = 1;
For (int I = 1; I <= 6; I ++)
{
Dp [I] [0] = dp [I-1] [0] * 9-dp [I-1] [1]; // The highest digit except 4, select the other 9 count, but there may be 6 before 2, so we need to subtract the number of the second digit 2.
Dp [I] [1] = dp [I-1] [0]; // The highest bit is only 2
Dp [I] [2] = dp [I-1] [2] * 10 + dp [I-1] [0] + dp [I-1] [1]; //
}
}
Int Solve (int n)
{
Int I, j, flag = 0;
Int bit [10];
Int len = 0;
Int tem = n;
While (n)
{
Bit [++ len] = n % 10;
N/= 10;
}
Bit [len + 1] = 0;
Int ans = 0;
For (int I = len; I> = 0; I --)
{
Ans + = dp [I-1] [2] * bit [I]; // total number of unlucky numbers
If (flag)
Ans + = dp [I-1] [0] * bit [I];
Else
{
If (bit [I]> 4)
Ans + = dp [I-1] [0]; // high may appear 4
If (bit [I + 1] = 6 & bit [I]> 2)
Ans + = dp [I] [1]; // when the high position is 6 and the second position may be 2
If (bit [I]> 6)
Ans + = dp [I-1] [1];
If (bit [I] = 4 | (bit [I + 1] = 6 & bit [I] = 2 ))
Flag = 1;
}
}
Return tem-ans;
}
Int main ()
{
Int n, m, I, j;
Init ();
While (scanf ("% d", & n, & m ))
{
If (! N &&! M) break;
Printf ("% d \ n", Solve (m + 1)-Solve (n ));
}
Return 0;
}

/********************
Language: c ++
Author: pirates
Problem: hdu2089
Style: Digital dp
*********************/


# Include <iostream>
# Include <stdio. h>
# Include <algorithm>
Using namespace std;
# Define Maxx 1000010
Int dp [10] [10];
// Dp [I] [0] indicates the number of unlucky ones
// Dp [I] [1] indicates the number with the highest bit of 2 and an unlucky number.
// Dp [I] [2] indicates the total number of unlucky digits. Neither 62 nor 4 can exist.
Void Init ()
{
Dp [0] [0] = 1;
For (int I = 1; I <= 6; I ++)
{
Dp [I] [0] = dp [I-1] [0] * 9-dp [I-1] [1]; // The highest digit except 4, select the other 9 count, but there may be 6 before 2, so we need to subtract the number of the second digit 2.
Dp [I] [1] = dp [I-1] [0]; // The highest bit is only 2
Dp [I] [2] = dp [I-1] [2] * 10 + dp [I-1] [0] + dp [I-1] [1]; //
}
}
Int Solve (int n)
{
Int I, j, flag = 0;
Int bit [10];
Int len = 0;
Int tem = n;
While (n)
{
Bit [++ len] = n % 10;
N/= 10;
}
Bit [len + 1] = 0;
Int ans = 0;
For (int I = len; I> = 0; I --)
{
Ans + = dp [I-1] [2] * bit [I]; // total number of unlucky numbers
If (flag)
Ans + = dp [I-1] [0] * bit [I];
Else
{
If (bit [I]> 4)
Ans + = dp [I-1] [0]; // high may appear 4
If (bit [I + 1] = 6 & bit [I]> 2)
Ans + = dp [I] [1]; // when the high position is 6 and the second position may be 2
If (bit [I]> 6)
Ans + = dp [I-1] [1];
If (bit [I] = 4 | (bit [I + 1] = 6 & bit [I] = 2 ))
Flag = 1;
}
}
Return tem-ans;
}
Int main ()
{
Int n, m, I, j;
Init ();
While (scanf ("% d", & n, & m ))
{
If (! N &&! M) break;
Printf ("% d \ n", Solve (m + 1)-Solve (n ));
}
Return 0;
} [Cpp]
/********************
Language: c ++
Author: pirates
Problem: hdu3555
Style: Digital dp
*********************/
 
 
# Include <iostream>
# Include <stdio. h>
# Include <string. h>
# Include <algorithm>
Using namespace std;
 
// Dp [I] [0] indicates the number of 49 records not displayed.
// The maximum number of dp [I] [1] bits is 9.
// Number of 49 entries in dp [I] [2]
Typedef _ int64 ll;
Ll dp [30] [3];
Void Init ()
{
Ll I;
Memset (dp, 0, sizeof (dp ));
Dp [0] [0] = 1;
For (I = 1; I <= 30; I ++ ){
Dp [I] [0] = (ll) dp [I-1] [0] * 10-dp [I-1] [1]; // no number of 49
Dp [I] [1] = (ll) dp [I-1] [0]; // The maximum number of 9
Dp [I] [2] = (ll) dp [I-1] [2] * 10 + dp [I-1] [1]; // Number of 49 occurrences
}
}
Ll Solve (ll n)
{
Ll I, j, len = 0;
Ll bit [25];
While (n)
{
Bit [++ len] = n % 10;
N/= 10;
}
Bit [len + 1] = 0;
Ll ans = 0;
Ll flag = 0;
For (I = len; I> 0; I --)
{
Ans + = (ll) dp [I-1] [2] * bit [I];
If (flag)
Ans + = (ll) dp [I-1] [0] * bit [I];
Else
{
If (bit [I]> 4)
Ans + = dp [I-1] [1];
If (bit [I + 1] = 4 & bit [I] = 9)
Flag = 1;
}
}
Return ans;
}
 
Int main ()
{
Init ();
Ll n, m, T;
Scanf ("% I64d", & T );
While (T --)
{
Scanf ("% I64d", & n );
Printf ("% I64d \ n", Solve (n + 1 ));
}
Return 0;
}

/********************
Language: c ++
Author: pirates
Problem: hdu3555
Style: Digital dp
*********************/


# Include <iostream>
# Include <stdio. h>
# Include <string. h>
# Include <algorithm>
Using namespace std;

// Dp [I] [0] indicates the number of 49 records not displayed.
// The maximum number of dp [I] [1] bits is 9.
// Number of 49 entries in dp [I] [2]
Typedef _ int64 ll;
Ll dp [30] [3];
Void Init ()
{
Ll I;
Memset (dp, 0, sizeof (dp ));
Dp [0] [0] = 1;
For (I = 1; I <= 30; I ++ ){
Dp [I] [0] = (ll) dp [I-1] [0] * 10-dp [I-1] [1]; // no number of 49
Dp [I] [1] = (ll) dp [I-1] [0]; // The maximum number of 9
Dp [I] [2] = (ll) dp [I-1] [2] * 10 + dp [I-1] [1]; // Number of 49 occurrences
}
}
Ll Solve (ll n)
{
Ll I, j, len = 0;
Ll bit [25];
While (n)
{
Bit [++ len] = n % 10;
N/= 10;
}
Bit [len + 1] = 0;
Ll ans = 0;
Ll flag = 0;
For (I = len; I> 0; I --)
{
Ans + = (ll) dp [I-1] [2] * bit [I];
If (flag)
Ans + = (ll) dp [I-1] [0] * bit [I];
Else
{
If (bit [I]> 4)
Ans + = dp [I-1] [1];
If (bit [I + 1] = 4 & bit [I] = 9)
Flag = 1;
}
}
Return ans;
}

Int main ()
{
Init ();
Ll n, m, T;
Scanf ("% I64d", & T );
While (T --)
{
Scanf ("% I64d", & n );
Printf ("% I64d \ n", Solve (n + 1 ));
}
Return 0;
}
[Cpp
/***************
Language: c ++
Author: pirates
Problem: adjacent two numbers difference 2
Style: Digital dp
****************/
 
# Include <iostream>
# Include <stdio. h>
# Include <algorithm>
# Include <string. h>
Using namespace std;
Int dp [15] [10];
Void Init ()
{
Int I, j, k;
Memset (dp, 0, sizeof (dp ));
For (I = 0; I <10; I ++)
Dp [1] [I] = 1;
For (I = 2; I <= 10; I ++)
For (j = 0; j <10; j ++)
For (k = 0; k <10; k ++)
If (abs (j-k)> = 2)
Dp [I] [j] + = dp [I-1] [k];
}
Int Solve (int n)
{
Int len = 0, I, j, bit [15];
While (n)
{
Bit [++ len] = n % 10;
N/= 10;
}
Bit [len + 1] = 0;
Int ans = 0;
For (I = 1; I <len; I ++) // collects statistics on each condition.
For (j = 1; j <bit [I]; j ++)
Ans + = dp [I] [j];
For (I = 1; I <bit [len]; I ++) // maximum bit
Ans + = dp [len] [I];
For (I = len-1; I --)//
For (j = 0; j <bit [I]; j ++ ){
If (abs (j-bit [I + 1])> = 2)
Ans + = dp [I] [j];
If (abs (bit [I + 1]-bit [I]) <2) break;
}
Return ans;
}
Int main ()
{
Init ();
Int I, j, n, m;
Scanf ("% d", & n, & m );
Printf ("% d \ n", Solve (m + 1)-Solve (n ));
Return 0;
}

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.