QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#574310#2519. Number with Bachelorslittle_pinkpigAC ✓236ms47480kbC++142.8kb2024-09-18 21:29:572024-09-18 21:29:57

Judging History

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

  • [2024-09-18 21:29:57]
  • 评测
  • 测评结果:AC
  • 用时:236ms
  • 内存:47480kb
  • [2024-09-18 21:29:57]
  • 提交

answer

#include<bits/stdc++.h>
#define MAXL (23)
#define MAXS (65539)
#define ll long long
#define ull unsigned long long
using namespace std;
const int mod=20130427;
void File()
{
    freopen(".in","r",stdin);
    freopen(".out","w",stdout);
}
template<typename type>
void read(type &x)
{
    bool f=0;char ch=0;x=0;
    while(ch<'0'||ch>'9'){f|=!(ch^'-');ch=getchar();}
    while(ch>='0'&&ch<='9'){x=(x<<3)+(x<<1)+(ch^48);ch=getchar();}
    x=f?-x:x;
}
template<typename type,typename... Args>
void read(type &x,Args &... args)
{
    read(x);
    read(args...);
}
bool vis[MAXL][MAXS][2][2];
char typ;
int test,qtyp,len;
int bas[2]={10,16},dig[MAXL];
ull L,R,pos,ans;
ull f[MAXL][MAXS][2][2];
ull dfs(int dep,int sta,bool lead,bool opt,bool up)
{
    if(!dep) return lead?0:1;
    if(!up&&vis[dep][sta][lead?1:0][opt?1:0]) return f[dep][sta][lead?1:0][opt?1:0];
    ull res=0;
    int lim=up?dig[dep]:(bas[opt?1:0]-1);
    for(int i=0;i<=lim;i++) if(!((sta>>i)&1)) res+=dfs(dep-1,(lead&&i==0)?0:sta|(1<<i),lead&&i==0,opt,up&&i==lim);
    if(!up) vis[dep][sta][lead?1:0][opt?1:0]=1,f[dep][sta][lead?1:0][opt?1:0]=res;
    return res;
}
ull fc(ull x)
{
    len=0;
    int t=typ=='d'?0:1;
    while(x)
    {
        dig[++len]=x%bas[t];
        x/=bas[t];
    }
    return dfs(len,0,1,t,1);
}
inline bool isdig(char ch){return (ch>='0'&&ch<='9')||(ch>='a'&&ch<='f');}
inline int getdig(char ch){return (ch>='0'&&ch<='9')?(ch-'0'):(ch-'a'+10);}
void input(ull &x)
{
    if(typ=='d') read(x);
    else
    {
        x=0;char ch=0;
        while(!isdig(ch)) ch=getchar();
        while(isdig(ch))
        {
            x=(x<<4)+getdig(ch);
            ch=getchar();
        }
    }
}
void output(ull x)
{
    // printf("OUTPUT=%llu\n",x);
    if(!x){printf("0\n");return;}
    if(typ=='d') printf("%llu\n",x);
    else
    {
        vector<int> dig;
        while(x) dig.push_back(x&15),x>>=4;
        reverse(dig.begin(),dig.end());
        for(int t:dig) putchar(t<10?(t+'0'):(t-10+'a'));
        printf("\n");
    }
}
inline void solve()
{
    cin>>typ;
    read(qtyp);
    if(!qtyp)
    {
        input(L),input(R);
        // printf("[%llu,%llu]\n",L,R);
        ans=fc(R)-(L?fc(L-1):(-1));
        output(ans);
    }
    else
    {
        input(pos);
        if(pos<9)
        {
            output(pos-1);
            return;
        }
        ull l=0,r=9876543210,res=-1;
        while(l<=r)
        {
            ull mid=l+r>>1;
            if(fc(mid)+1>=pos)
            {
                res=mid;
                r=mid-1;
            }
            else l=mid+1;
        }
        if(~res) output(res);
        else printf("-\n");
    }
}
int main()
{
#ifndef ONLINE_JUDGE
    File();
#endif
    read(test);
    for(int i=1;i<=test;i++) solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 8ms
memory: 24252kb

input:

6
d 0 10 20
h 0 10 1f
d 1 10
h 1 f
d 1 1000000000
h 1 ffffffffffffffff

output:

10
f
9
e
-
-

result:

ok 6 lines

Test #2:

score: 0
Accepted
time: 236ms
memory: 47480kb

input:

50000
h 1 147a
d 0 25 71
d 1 3587
d 0 26922 51887
d 1 711
d 0 3 5
h 0 7bf2defab442a0b1 f299a4cf1d4d9bed
d 0 6961 91018
d 1 4
d 1 876
h 1 12cc5d3370f99120
d 1 161315
h 0 25f 6959
d 0 467 516
d 1 298
h 1 70260cdc2da73281
h 1 928e17d65d764ca2
h 1 c8ec8a7b67605e51
d 1 91697
d 0 4941925161850941148 89850...

output:

1b36
43
6587
7710
953
3
8daab378500
26054
3
1356
-
946307
4681
40
387
-
-
-
491850
0
1
29
-
4605298
1
1
-
15b4
175f
9b944134000
124b7
6279
9
6257
-
39be22a900
5c636b59300
146ce
2a55
-
0
-
7
d
6
2041
-
1c94afe7300
0
5
9149
16540973
1389
125
0
-
3bc31189480
424
66800
7
-
-
1e6
0
0
48b6
9
-
2b0
5019
14...

result:

ok 50000 lines