1. 程式人生 > >CF258B Little Elephant and Elections 數位dp

CF258B Little Elephant and Elections 數位dp

There have recently been elections in the zoo. Overall there were 7 main political parties: one of them is the Little Elephant Political Party, 6 other parties have less catchy names.

Political parties find their number in the ballot highly important. Overall there are m possible numbers: 1, 2, ..., m

. Each of these 7 parties is going to be assigned in some way to exactly one number, at that, two distinct parties cannot receive the same number.

The Little Elephant Political Party members believe in the lucky digits 4 and 7. They want to evaluate their chances in the elections. For that, they need to find out, how many correct assignments are there, such that the number of lucky digits in the Little Elephant Political Party ballot number is strictly larger than the total number of lucky digits in the ballot numbers of 6 other parties.

Help the Little Elephant Political Party, calculate this number. As the answer can be rather large, print the remainder from dividing it by 1000000007 (109 + 7).

Input

A single line contains a single positive integer m (7 ≤ m ≤ 109) — the number of possible numbers in the ballot.

Output

In a single line print a single integer — the answer to the problem modulo 1000000007 (109 + 7).

Examples

Input

Copy

7

Output

Copy

0

Input

Copy

8

Output

Copy

1440

題意:

從 1~m中選擇7個數,滿足一個數中4&&7的個數>其餘所選擇的數字中4&&7個數之和;

數位dp;

先求出每一種位數時僅為4&&7組成的數,

然後對於其餘6個party進行dfs搜尋;

(Div1.B好難  orz)

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstdlib>
#include<cstring>
#include<string>
#include<cmath>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<bitset>
#include<ctime>
#include<deque>
#include<stack>
#include<functional>
#include<sstream>
//#include<cctype>
//#pragma GCC optimize("O3")
using namespace std;
#define maxn 100005
#define inf 0x3f3f3f3f
#define INF 999999999999999
#define rdint(x) scanf("%d",&x)
#define rdllt(x) scanf("%lld",&x)
#define rdult(x) scanf("%lu",&x)
#define rdlf(x) scanf("%lf",&x)
#define rdstr(x) scanf("%s",x)
typedef long long  ll;
typedef unsigned long long ull;
typedef unsigned int U;
#define ms(x) memset((x),0,sizeof(x))
const long long int mod = 1e9 + 7;
#define Mod 100003
#define sq(x) (x)*(x)
#define eps 1e-3
typedef pair<int, int> pii;
#define pi acos(-1.0)
const int N = 1005;
#define REP(i,n) for(int i=0;i<(n);i++)
inline ll rd() {
    ll x = 0;
    char c = getchar();
    bool f = false;
    while (!isdigit(c)) {
        if (c == '-') f = true;
        c = getchar();
    }
    while (isdigit(c)) {
        x = (x << 1) + (x << 3) + (c ^ 48);
        c = getchar();
    }
    return f ? -x : x;
}

ll gcd(ll a, ll b) {
    return b == 0 ? a : gcd(b, a%b);
}
ll sqr(ll x) { return x * x; }

/*ll ans;
ll exgcd(ll a, ll b, ll &x, ll &y) {
    if (!b) {
        x = 1; y = 0; return a;
    }
    ans = exgcd(b, a%b, x, y);
    ll t = x; x = y; y = t - a / b * y;
    return ans;
}
*/

int dp[20][20][20];
int sum[maxn];
int a[maxn];
int ans;

ll dfs(int pos, int cur, int sum, int limit) {
    if (pos == 0)return cur == sum;
    if (limit == 0 && dp[pos][cur][sum] != -1)return dp[pos][cur][sum];
    ll res = 0;
    int up = limit ? a[pos] : 9;
    for (int i = 0; i <= up; i++) {
        res += dfs(pos - 1, cur + (i == 4 || i == 7), sum, limit && (i == up));
    }
    if (limit == 0)dp[pos][cur][sum] = res;
    return res;
}

ll dfs2(int party, ll res) {
    ll ans = 0;
    if (party == 7)return 1;
    for (int i = 0; i <= res; i++) {
        if (sum[i]) {
            sum[i]--;
            ans = (ans + ((sum[i]+1) * dfs2(party + 1, res - i))) % mod;
            sum[i]++;
        }
    }
    return ans;
}

ll sol(ll x) {
    int pos = 0;
    while (x) {
        a[++pos] = x % 10; x /= 10;
    }
    memset(dp, -1, sizeof(dp));
    for (int i = 0; i <= pos; i++)sum[i] = dfs(pos, 0, i, 1);
    sum[0]--;
    ll ans = 0;
    for (int i = 0; i <= pos; i++) {
        ans = (ans + sum[i] * dfs2(1, i - 1)) % mod;
    }
    return ans;
}

int main()
{
    //ios::sync_with_stdio(false);
    ll n; rdllt(n);
    cout << sol(n) << endl;
    return 0;
}