CodeForces 258 B.Little Elephant and Elections(数位DP+dfs)

125 篇文章 0 订阅
22 篇文章 0 订阅

Description

1 ~m中选出 7 个不同的数字,使得第七个数字的4 7 的数量 大于前六个数字的4 7 的数量

Input

一个整数m(7m109)

Output

输出满足条件的方案数,结果模 109+7

Sample Input

7

Sample Output

0

Solution

数位 DP 找到不超过 m 的数里恰有i 4 7的数字个数 cnti ,之后枚举第七个数字中 4 7的数量 i dfs找六个数字使得其 4 7的数量不超过 i <script type="math/tex" id="MathJax-Element-22">i</script>的方案数

Code

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
#include<map>
#include<set>
#include<ctime>
using namespace std;
typedef long long ll;
typedef pair<int,int>P;
const int INF=0x3f3f3f3f,maxn=100001;
#define mod 1000000007
int m,n,a[15],dp[15][15],cnt[15],ans;
int dfs(int pos,int num,int fp)
{
    if(pos<0)return num==0;
    if(!fp&&~dp[pos][num])return dp[pos][num];
    int ans=0,fpmax=fp?a[pos]:9;
    for(int i=0;i<=fpmax;i++)
        ans+=dfs(pos-1,num-(i==4||i==7),fp&&i==fpmax);
    if(!fp)dp[pos][num]=ans;
    return ans;
}
void dfs1(int num,int now,int res,int tans)
{
    if(now>=num)return ;
    if(res==6)
    {
        ans+=tans;
        if(ans>=mod)ans-=mod;
        return ;
    }
    for(int i=0;i<=n;i++)
        if(cnt[i])
        {
            cnt[i]--;
            dfs1(num,now+i,res+1,(ll)tans*(cnt[i]+1)%mod);
            cnt[i]++;
        }
}
int main()
{
    scanf("%d",&m);
    n=0;
    while(m)a[n++]=m%10,m/=10;
    memset(dp,-1,sizeof(dp));
    for(int i=0;i<=n;i++)cnt[i]=dfs(n-1,i,1);
    //printf("cnt[%d]=%d\n",i,cnt[i]);
    cnt[0]--;
    ans=0;
    for(int i=1;i<=n;i++)dfs1(i,0,0,cnt[i]);
    printf("%d\n",ans);
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值