CodeForces 258B Little Elephant and Elections
Posted LJZ_C
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了CodeForces 258B Little Elephant and Elections相关的知识,希望对你有一定的参考价值。
标签(空格分隔): 数位DP 背包DP
算出有i个幸运数字的方案,然后枚举上限做背包DP
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
typedef long long ll;
ll read()
{
ll x=0,f=1;char ch=getchar();
while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();}
while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();}
return x*f;
}
const int mod=1e9+7;
int m,f[15][15],g[15],dit[15];
ll calc(int x)
{
ll res=1;
for(int i=0;i<6;i++)res=res*(x-i)%mod;
return res;
}
void MOD(int &x,int y)
{
x+=y;
if(x>=mod)x-=mod;
}
int dp(int step,int r,int lim)
{
if(step==0)return r==0;
if(!lim&&~f[step][r])return f[step][r];
int x=lim?dit[step]:9,res=0;
for(int i=0;i<=x;i++)
{
if(i==4||i==7)
{
if(r==0)continue;
MOD(res,dp(step-1,r-1,lim&&i==x));
continue;
}
res+=dp(step-1,r,lim&&i==x);
}
if(!lim)f[step][r]=res;
return res;
}
int dfs(int step,int r,ll mul)
{
if(step==0)return mul;
int res=0;
for(int i=0;i<=r;i++)if(g[i])
{
g[i]--;
MOD(res,dfs(step-1,r-i,mul*(g[i]+1)%mod));
g[i]++;
}
return res;
}
int main()
{
memset(f,-1,sizeof(f));
m=read();
int len=0;
while(m)dit[++len]=m%10,m/=10;
for(int i=0;i<=len;i++)g[i]=dp(len,i,1);
g[0]--;
int ans=0;
for(int i=1;i<=len;i++)MOD(ans,(ll)g[i]*dfs(6,i-1,1)%mod);
printf("%d\n",ans);
return 0;
}
以上是关于CodeForces 258B Little Elephant and Elections的主要内容,如果未能解决你的问题,请参考以下文章
Codeforces 258B Sort the Array
codeforces 460D:Little Victor and Set
Codeforces 220B - Little Elephant and Array 离线树状数组
[CodeForces-259C] Little Elephant and Bits