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