C趣味解

已知 D 在 [1, 100],由于数字的组合是有限的,当计算到 100×D 时,必然会出现之前已经出现过的数字(或其关联特征)。因此,我们可以大胆直接枚举 1×D 到 100×D 的结果:

void solve()
{
    int n,d;
    string h;
    cin>>n>>d>>h;
    map<char,int>mp;
    
    for(int i=0;i<h.length();i++)
        mp[h[i]]++;
    if(mp['0'])
    {
        cout<<0<<endl;
        return;
    }
    for(int i = d;i<100*d;i+=d)
    {
        h  = to_string(i);
        int f = 1;
        map<char,int> mp2;
        for(auto e:h)
        {
            mp2[e]++;
        }
        for(auto [e,v]:mp2)
        {
            if(mp[e]>=mp2[e])
                continue;
            else
            {
                f=0;
                break;
            }
        }
        if(f)
        {
            cout<<i<<endl;
            return;
        }
    }
    cout<<-1<<endl;
}

但是实际上是25次就能通过代码,不知道是数据问题,还是说另有性质