QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#573747 | #2519. Number with Bachelors | tx344 | AC ✓ | 292ms | 112988kb | C++14 | 4.4kb | 2024-09-18 19:52:33 | 2024-09-18 19:52:33 |
Judging History
answer
//
#include<bits/stdc++.h>
#define ll long long
#define Ull unsigned ll
#define PII pair<int,int>
#define PLL pair<ll,ll>
#define PPIII pair<PII,int>
#define PLPII pair<ll,PII>
#define fi first
#define se second
using namespace std;
int Q,num[121],nt;
ll f1[115][1<<10],f2[211][1<<16];
inline int id(char x){return x<='9'&&x>='0'?x-'0':x-'a'+10;}
ll DFS1(int i,int j,int S)
{
if(!i)return 1;
if(j>1&&~f1[i][S])return f1[i][S];
ll sum=0;
if(!j)sum+=DFS1(i-1,0,S);
for(int o=!j;o<=((j==1||((!j&&i==nt)))?num[i]:9);o++)if(!(S>>o&1))
{
sum+=DFS1(i-1,((j==1||((!j&&i==nt)))&&o==num[i])?1:2,S^(1<<o));
}
if(j>1)f1[i][S]=sum;
// if(i==nt)
// {
// puts("-------------------------");
// for(int i=nt;i;i--)printf("%d ",num[i]);puts("");
// printf("%lld\n",sum);
// puts("-------------------------");
// }
return sum;
}
ll DFS2(int i,int j,int S)
{
if(!i)return 1;
if(j>1&&~f2[i][S])return f2[i][S];
ll sum=0;
if(!j)sum+=DFS2(i-1,0,S);
for(int o=!j;o<=((j==1||((!j&&i==nt)))?num[i]:15);o++)if(!(S>>o&1))
{
sum+=DFS2(i-1,((j==1||((!j&&i==nt)))&&o==num[i])?1:2,S^(1<<o));
}
if(j>1)f2[i][S]=sum;
// if(i==nt)
// {
// puts("+++++++++++++++++++++++++");
// for(int i=nt;i;i--)printf("%d ",num[i]);puts("");
// printf("%lld\n",sum);
// puts("+++++++++++++++++++++++++");
// }
return sum;
}
ll Solve_d0()
{
return DFS1(nt,0,0);
}
ll Solve_f0()
{
return DFS2(nt,0,0);
}
void Solve_d1(Ull C)
{
if(C>8877691)return puts("-"),void();
memset(num,0,sizeof(num));
bool Flag=0;
for(int i=10;i;i--)
{
if(!Flag)nt=i;
int l=1,r=9;
while(l<=r)
{
int mid=l+r>>1;
num[i]=mid;
if(Solve_d0()<C)l=mid+1;
else r=mid-1;
}
if(r)Flag=1;
num[i]=r;
}
int sg=1;
while(sg<=nt&&num[sg]==9)num[sg++]=0;
if(sg<=nt)++num[sg];
else num[++nt]=1;
for(int i=nt;i;i--)printf("%d",num[i]);puts("");
}
void Solve_f1(Ull C)
{
if(C>53319412081141)return puts("-"),void();
memset(num,0,sizeof(num));
bool Flag=0;
for(int i=16;i;i--)
{
if(!Flag)nt=i;
int l=1,r=15;
while(l<=r)
{
int mid=l+r>>1;
num[i]=mid;
// for(int i=nt;i;i--)printf("%c",num[i]<10?num[i]+'0':num[i]-10+'a');puts("");
// printf("%lld %lld\n",Solve_f0(),C);
if(Solve_f0()<C)l=mid+1;
else r=mid-1;
}
if(r)Flag=1;
num[i]=r;
}
int sg=1;
while(sg<=nt&&num[sg]==15)num[sg++]=0;
if(sg<=nt)++num[sg];
else num[++nt]=1;
for(int i=nt;i;i--)printf("%c",num[i]<10?num[i]+'0':num[i]-10+'a');puts("");
}
void Print_f(ll x)
{
if(x>15)Print_f(x/16);
printf("%c",x%16<10?x%16+'0':x%16-10+'a');
}
int main()
{
#ifndef ONLINE_JUDGE
freopen(".in","r",stdin);
freopen(".out","w",stdout);
#endif
memset(f1,-1,sizeof(f1));
memset(f2,-1,sizeof(f2));
int iQ=0;
scanf("%d",&Q);
while(Q--)
{
char op1[51],x[211],y[211];
int op2;
Ull z=0;
scanf("%s%d%s",op1,&op2,x);
// if(++iQ==64)printf("%c %d %s:",op1[0],op2,x);
if(op2)
{
nt=strlen(x);
if(nt==1&&x[0]=='1'){puts("0");continue;}
for(int i=0;i<nt;i++)z=z*(op1[0]=='d'?10:16)+id(x[i]);
// printf("z=%llu\n",z);
if(op1[0]=='d')Solve_d1(z);
else Solve_f1(z);
}
else
{
scanf("%s",y);
nt=strlen(y);
for(int i=0;i<nt;i++)num[nt-i]=id(y[i]);
ll sum;
if(op1[0]=='d')sum=Solve_d0();
else sum=Solve_f0();
nt=strlen(x);
for(int i=0;i<nt;i++)num[nt-i]=id(x[i]);
if(nt>1||num[1])
{
int sg=1;
while(!num[sg])num[sg++]=op1[0]=='d'?9:15;
if(!--num[sg]&&sg==nt)--nt;
if(op1[0]=='d')sum-=Solve_d0();
else sum-=Solve_f0();
}
if(op1[0]=='d')printf("%lld\n",sum);
else Print_f(sum),puts("");
}
}
cerr<<1.0*clock()/CLOCKS_PER_SEC<<'\n';
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 36ms
memory: 112948kb
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: 292ms
memory: 112988kb
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