淘金

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <map>
#include <ctime>
#include <queue>
#define rint register int
#define ll long long
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
const int mod=1e9+7;
const int MOD=76543;

ll n; int K;
int t[15],he;

ll num[15006],cnt[15006]; int con;

struct HAHSHSHS
{
    int first[MOD];
    int an[42006];ll ke[42006];
    int nt[42006],e,tt,i;
    void clear()
    {
        mem(first,-1);
    }
    int ifif(ll x)
    {
        tt=x%MOD;
        for(i=first[tt];i!=-1;i=nt[i])
            if(ke[i]==x)
                return an[i];
        return -1;
    }
    void add(ll x,int ans)
    {
        tt=x%MOD;
        an[e]=ans;
        ke[e]=x;
        nt[e]=first[tt];
        first[tt]=e++;
    }
}H;

struct son
{
    int x,y; ll v;
    son(){}
    son(int _x,int _y)
    {
        x=_x; y=_y;
        v=1LL*cnt[x]*cnt[y];
    }
    bool friend operator < (son a,son b)
    {
        return a.v<b.v;
    }
};

priority_queue<son> q;

bool flag[12006][12006];

bool cmp(ll a,ll b)
{
    return a>b;
}

void dfs(int order,int las,ll now)
{
    if(!now||now>n) return ;
    if(H.ifif(now)==-1)
        ++con,num[con]=now,H.add(now,con);
    if(!order) return ;
    int i;
    for(i=las;i<=9;++i)
        dfs(order-1,i,now*i);
}

ll f[15][2][20006];
ll dp(int order,int limit,int lead,int id)
{
    if(id<0) return 0;
    if(!order)
    {
        if(lead) return 0;
        return id==1;
    }
    if( limit==10 && f[order][lead][id]!=-1 )
        return f[order][lead][id];
    ll an=0; int i;
    if(lead)
        an+=dp(order-1,10,1,id);
    for(i=1;i<limit;++i)
        if(num[id]%i==0)
            an+=dp(order-1,10,0,H.ifif(num[id]/i));
    if(limit&&limit!=10&&num[id]%limit==0)
        an+=dp(order-1,t[order-1],0,H.ifif(num[id]/limit));
    if(limit==10)
        f[order][lead][id]=an;
    return an;
}

ll work()
{
    rint i,j; ll an=0;
    
    ll x=n;
    while(x)
    {
        t[++he]=x%10;
        x/=10;
    }
    dfs(he,1,1);
    
    mem(f,-1);
    for(i=1;i<=con;++i)
        cnt[i]=dp(he,t[he],1,i);
    sort(cnt+1,cnt+1+con,cmp);
    
    q.push(son(1,1));
    son tt;
    while(K&&!q.empty())
    {
        tt=q.top(); q.pop();
        if(flag[tt.x][tt.y]) continue;
        flag[tt.x][tt.y]=1;
        an=(an+tt.v)%mod;
        q.push(son(tt.x+1,tt.y));
        q.push(son(tt.x,tt.y+1));
        --K;
    }
    return an%mod;
}

int main(){
    
    //freopen("in.in","r",stdin);
    //freopen("out.out","w",stdout);
    
    H.clear();
    
    scanf("%lld%d",&n,&K);
    cout<<work();
}
淘金

 

posted @ 2017-11-03 10:18  A_LEAF  阅读(231)  评论(0编辑  收藏  举报