QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#504649#9107. Zayin and CountAfterlife#AC ✓52ms3636kbC++202.9kb2024-08-04 14:28:102024-08-04 14:28:11

Judging History

你现在查看的是最新测评结果

  • [2024-08-04 14:28:11]
  • 评测
  • 测评结果:AC
  • 用时:52ms
  • 内存:3636kb
  • [2024-08-04 14:28:10]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;

#define int long long

using ll=__int128;

const int N=71;

int T;

ll f[N][2][2];

int a[10],b[10];

string s;

ll dfs(int x,int lead,int up)
{
    if(x==s.size())
        return !lead;
    if(f[x][lead][up]!=-1)
        return f[x][lead][up];
    ll &ret=f[x][lead][up];
    ret=0;
    for(int i=0;i<=(up?s[x]-'0':9);i++)
        if(a[i]||(lead&&i==0))
        {
            int nlead=lead&&!i;
            int nup=up&&i==s[x]-'0';
            ret+=dfs(x+1,nlead,nup);
        }
    return ret;
}

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cin>>T;
    while(T--)
    {
        memset(f,-1,sizeof(f));
        // cout << (f[70][0][0] == -1) << endl;
        for(int i=0;i<10;i++)
            cin>>a[i];
        for(int i=0;i<10;i++)
            cin>>b[i];
        cin>>s;
        // int u=stoi(s);
        // int cc=0;
        // for(int i=0;i<=u;i++)
        // {
        //     auto s=to_string(i);
        //     bool ok=1;
        //     for(auto x:s)
        //         if(!a[x-'0'])
        //         {
        //             ok=0;
        //             break;
        //         }
        //     cc+=ok;
        // }
        // cerr<<cc<<endl;
        ll cnt=0;
        cnt=dfs(0,1,1);
        if(a[0])
            cnt++;
        // cerr<<(int)cnt<<endl;
        if(b[0])
        {
            if(cnt==1)
            {
                cout<<"0\n";
                continue;
            }
            cnt--;
        }
        int cb=accumulate(b,b+10,0);
        vector<ll> pw({1});
        ll S=0;
        for(int i=1;;i++)
        {
            ll ways=pw.back();
            int fd=0;
            for(int j=1;j<10;j++)
            {
                if(!b[j])
                    continue;
                if(S+ways>=cnt)
                {
                    string ans;
                    ans+=to_string(j);
                    pw.pop_back();
                    for(int k=i-1;k>=1;k--)
                    {
                        ll ways=pw.back();
                        pw.pop_back();
                        for(int j=0;j<10;j++)
                        {
                            if(!b[j])
                                continue;
                            
                            if(S+ways>=cnt)
                            {
                                ans+=to_string(j);
                                break;
                            }
                            else
                                S+=ways;
                        }
                    }
                    cout<<ans<<"\n";
                    fd=1;
                    break;
                }
                else
                    S+=ways;
            }
            if(fd)
                break;
            pw.push_back(pw.back()*cb);
        }
    }
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 52ms
memory: 3636kb

input:

10000
1 0 0 0 1 1 0 0 0 1
0 0 1 0 1 1 1 1 0 0
950595954440050004054505054050
1 0 0 0 1 1 1 1 0 0
1 1 1 0 1 0 0 0 1 1
45467007076660767550460064
1 1 1 1 0 0 0 1 0 0
1 1 0 1 1 0 1 0 0 1
23373171320213300170200722
0 0 0 0 1 1 1 0 1 0
0 0 1 0 0 1 0 1 1 1
558565664666565565558468668484
1 1 0 0 1 0 1 0 1 ...

output:

52755244567262766742575722
41990991999414091249949
101364364636933104003903
57558888789255872922852552
757222758857875785288225787822
761161760076076167101117776167
56666586555668686566656586856566686658
15611661611611111511116116661611616155
505885888775005550558080707878
3912911219633669993999199
...

result:

ok 10000 lines