HDOJ-3555 by bit DP ..

Source: Internet
Author: User

The method is very simple. The statistics are quite painful... WA wants to vomit... it's just a reference to other people's code...

Note that the first digit of the current number is 49 ....

Program:


[Cpp]
# Include <iostream>
# Include <cmath>
# Include <stack>
# Include <queue>
# Include <set>
# Include <algorithm>
# Include <stdio. h>
# Include <string. h>
# Define ll unsigned long
# Define OOS 1000000007
Using namespace std;
Ll dp [23] [3];
Void pre ()
{
Int I, j, k;
Memset (dp, 0, sizeof (dp ));
Dp [0] [0] = 1;
For (I = 1; I <= 22; I ++)
{
Dp [I] [0] = dp [I-1] [0] * 10-dp [I-1] [1]; // Number of numbers with the maximum length of I and not including 49
Dp [I] [1] = dp [I-1] [0]; // Number of numbers with a maximum length of 9 and No 49
Dp [I] [2] = 10 * dp [I-1] [2] + dp [I-1] [1]; // Number of I contained 49
}
Return;
}
Ll getans (ll n)
{
Ll p, ans, l, s [22];
Bool f = false;
L = 0;
S [0] = 0;
While (n)
{
S [++ l] = n % 10;
N/= 10;
}
Ans = 0;
For (p = l; p> = 1; p --)
{
Ans + = dp [P-1] [2] * s [p];
If (f)
Ans + = dp [P-1] [0] * s [p];
Else
If (s [p]> 4) ans + = dp [P-1] [1];
If (s [p + 1] = 4 & s [p] = 9) f = true;
}
Return ans;
}
Int main ()
{
Int T;
Ll n;
Scanf ("% d", & T );
Pre ();
While (T --)
{
Cin> n;
N ++;
Cout <getans (n) <endl;
}
Return 0;
}

# Include <iostream>
# Include <cmath>
# Include <stack>
# Include <queue>
# Include <set>
# Include <algorithm>
# Include <stdio. h>
# Include <string. h>
# Define ll unsigned long
# Define OOS 1000000007
Using namespace std;
Ll dp [23] [3];
Void pre ()
{
Int I, j, k;
Memset (dp, 0, sizeof (dp ));
Dp [0] [0] = 1;
For (I = 1; I <= 22; I ++)
{
Dp [I] [0] = dp [I-1] [0] * 10-dp [I-1] [1]; // Number of numbers with the maximum length of I and not including 49
Dp [I] [1] = dp [I-1] [0]; // Number of numbers with a maximum length of 9 and No 49
Dp [I] [2] = 10 * dp [I-1] [2] + dp [I-1] [1]; // Number of I contained 49
}
Return;
}
Ll getans (ll n)
{
Ll p, ans, l, s [22];
Bool f = false;
L = 0;
S [0] = 0;
While (n)
{
S [++ l] = n % 10;
N/= 10;
}
Ans = 0;
For (p = l; p> = 1; p --)
{
Ans + = dp [P-1] [2] * s [p];
If (f)
Ans + = dp [P-1] [0] * s [p];
Else
If (s [p]> 4) ans + = dp [P-1] [1];
If (s [p + 1] = 4 & s [p] = 9) f = true;
}
Return ans;
}
Int main ()
{
Int T;
Ll n;
Scanf ("% d", & T );
Pre ();
While (T --)
{
Cin> n;
N ++;
Cout <getans (n) <endl;
}
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.