CodeForces - 258B King's Path(数位dp)

本文介绍了一种复杂的数位DP问题,旨在找出所有可能的数字排列,使得特定政党对应的数字包含的幸运数字数量超过其他政党的总数。通过预处理幸运数字出现次数及使用动态规划解决这一挑战。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
}




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值