Codeforces Round #157 (Div. 1) B digital dp

Source: Internet
Author: User

Codeforces Round #157 (Div. 1) B digital dp
// There are several enumerated values (7 or 4). The number of I (7 or 4) is searched with the memory of the digital dp.
// Select the number of brute-force searches in the I field.
# Include
# Include
# Include
Using namespace std;
Const int mod = 1e9 + 7;
Int dp [20] [20]; // Number of j in the I-th digit (7 or 4)
Int bit [20];
Int temp [20];
Int luck [20];
Int dfs (int pos, int flag, int max_flag, int lim)
{
If (pos = 0)
Return flag = max_flag;
If (! Lim & dp [pos] [flag]! =-1)
Return dp [pos] [flag];
Int num = lim? Bit [pos]: 9;
Int ans = 0;
For (int I = 0; I <= num; I ++)
{
Int flag_x = flag;
If (I = 7 | I = 4)
Flag_x ++;
Ans + = dfs (pos-1, flag_x, max_flag, lim & (I = num ));
}
If (! Lim) dp [pos] [flag] = ans;
Return ans;
}
// _ Int64 ans_ans = 0;
_ Int64 dfs_s (int res, _ int64 ans, int pos, int num, int max_flag)
{
If (num> = max_flag)
Return 0;
If (res = 0)
Return (ans * (_ int64) temp [num + 1]) % mod;
_ Int64 sum = 0;
For (int I = 0; I <= pos; I ++)
{
If (luck [I]> 0)
{
_ Int64 ans_x = (ans * (_ int64) luck [I]) % mod );
Luck [I]-= 1;
Sum + = dfs_s (res-1, ans_x, pos, num + I, max_flag );
Luck [I] + = 1;
}
}
Return sum % mod;
}
_ Int64 solve (int n)
{
If (n = 7) return 0;
Int t = n;
Int pos = 0;
While (t)
{
Bit [++ pos] = t % 10;
T/= 10;
}
Memset (luck, 0, sizeof (luck ));
Memset (temp, 0, sizeof (temp ));
For (int I = pos; I> = 0; I --)
{
Memset (dp,-1, sizeof (dp ));
Luck [I] = dfs (pos, 0, I, 1 );
Temp [I] = temp [I + 1] + luck [I];
}
Luck [0]-= 1;
Return dfs_s (6, 1, pos, 0, luck [pos]? Pos: pos-1 );
}
Int main ()
{
// Freopen ("input.txt", "r", stdin );
Int n;
While (~ Scanf ("% d", & n ))
{
Printf ("% I64d \ n", solve (n) % mod );
}
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.