Codeforces Round #157 (Div. 1) B-digit DP

Source: Internet
Author: User

There are several (7 or 4) enumerations, and the number of numbers of I (7 or 4) is found with a memory search of the digital DP
Violence search in the I select a few
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace Std;
const INT mod = 1e9 + 7;
int dp[20][20];//I digit j number (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;
}















































Codeforces Round #157 (Div. 1) B-digit 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.