CodeForces

来源:互联网 发布:mysql5.7数据库安装 编辑:程序博客网 时间:2024/06/05 14:15

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).

Example
Input
7
Output
0
Input
8
Output
1440

【题解】

 个人认为这是一道比较难的数位dp题,因为题目不好理解,题意就是说:

规定4,7为lucky数。给一个数m,求1~m中的七个数字的排列中,满足下列条件的排列有多少个:

第七个数中含有的lucky数的数量大于前六个含有的lucky数的总和。(比如说1234567,该数字有一个4,一个7,则他的lucky值为2)。

分析:

  因为最多是个10位数,所以最多的幸运数个数也就是10,所以预处理出来一个sum[i],代表幸运值为i的数有多少个,然后暴力找结果就就好。

 【AC代码】

#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<string.h>using namespace std;typedef __int64 ll;const int inf = 1e9+7;const int N = 20;ll dp[20][20][20];int num[100];ll answer[50];int len;ll dfs(int pos,int num1,int k,bool limit){    if(pos<0) return num1 == k;    if(!limit && dp[pos][num1][k]!=-1)        return dp[pos][num1][k];    int ii = limit?num[pos]:9;    ll ans=0;    for(int i=0;i<=ii;++i)    {        ans+=dfs(pos-1,num1+(i==4||i==7),k ,limit&&i == ii);        ans%=inf;    }    if(!limit) dp[pos][num1][k] = ans;    return ans;}void solve(ll m){    len=0;    memset(dp,-1,sizeof dp);    while(m){        num[len++] = m%10;        m/=10;    }    for(int i=0;i<=len;++i)        answer[i] = dfs(len-1,0,i,1);    answer[0]--;}ll solve(int n,int pos){    if(pos<=0) return 1;    ll ans=0;    for(int i=0;i<=n;++i)    {        if(answer[i]<=0) continue;        answer[i]--;        ans = (ans+(1+answer[i])*solve(n-i,pos-1))%inf;        answer[i]++;    }    return ans;}int main(){    ll a;    while(~scanf("%I64d",&a)){        solve(a);        ll ans = 0;        for(int i=0;i<=len;++i)            ans = (ans+answer[i]*solve(i-1,6))%inf;        printf("%I64d\n",ans);    }    return 0;}




原创粉丝点击