QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#574905#2519. Number with BachelorslifanAC ✓1587ms27116kbC++142.6kb2024-09-19 08:30:472024-09-19 08:30:48

Judging History

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

  • [2024-09-19 08:30:48]
  • 评测
  • 测评结果:AC
  • 用时:1587ms
  • 内存:27116kb
  • [2024-09-19 08:30:47]
  • 提交

answer

// 
#include<bits/stdc++.h>
using namespace std;
const int N=25;
using uLL=unsigned long long;
using i128=__int128;
int B;
uLL qk;
int b[N],cnt;
uLL f[2][N][65536];
bool vis[2][N][65536];
uLL dfs(int tp,int now,int S,int first,int up) {
	if (!now) return 1;
	if (up&&!first&&vis[tp][now][S]) return f[tp][now][S];
	uLL res=0;
	for (int i=0;i<B;i++) {
		if ((S>>i)&1) continue;
		if (first&&!i) continue;
		if (!up) {
			if (i<b[now]) res+=dfs(tp,now-1,S|(1<<i),0,1);
			if (i==b[now]) res+=dfs(tp,now-1,S|(1<<i),0,0);
		} else res+=dfs(tp,now-1,S|(1<<i),0,1);
	}
	if (up&&!first) vis[tp][now][S]=1,f[tp][now][S]=res;
//	cout<<now<<' '<<S<<' '<<first<<' '<<up<<' '<<res<<"\n";
	return res;
}
inline uLL calc(uLL x) {
	cnt=0;
	while (x) {
		b[++cnt]=x%B,x/=B;
	}
	uLL res=dfs(B==10,cnt,0,1,0);
	for (int i=cnt-1;i>=0;i--) res+=dfs(B==10,i,0,1,1);
	return res;
}
inline bool check(uLL x) {
	return calc(x)>=qk;
}
uLL bs(uLL l,uLL r) {
	if (l==r) return l;
	if (l+1==r) {
		if (check(l)) return l;
		return r;
	}
	uLL mid=(((i128)l)+r)/2;
	if (check(mid)) return bs(l,mid);
	return bs(mid+1,r);
}
int main() {
	ios::sync_with_stdio(0);
	cin.tie(0),cout.tie(0);
	int tac;
	cin>>tac;
	for (int tt=1;tt<=tac;tt++) {
		char tp;
		int op;
		string l,r,x;
		cin>>tp>>op;
		qk=0;
		if (tp=='h') B=16;
		else B=10;
//		if (tt==92) {
//			cout<<tp<<'_'<<op<<'_';
//		}
		if (op) {
			cin>>x;
//			if (tt==92) {
//				cout<<x<<"\n";
//			}
			for (auto o:x) {
				if (isdigit(o)) qk=qk*B+o-'0';
				else qk=qk*B+o-'a'+10;
			}
			uLL maxn=0xffffffffffffffff;
			uLL res=bs(0,maxn);
			if (!res) {
				cout<<"0\n";
				continue;
			}
			if (res==maxn) {
				cout<<"-\n";
			} else {
				int bb[N]={},ccnt=0;
				while (res) bb[++ccnt]=res%B,res/=B;
				for (int i=ccnt;i;i--) {
					if (bb[i]>=10) cout<<(char)('a'+bb[i]-10);
					else cout<<(char)('0'+bb[i]);
				}
				cout<<"\n";
			}
		} else {
			cin>>l>>r;
//			if (tt==92) {
//				cout<<l<<'_'<<r<<"\n";
//			}
			uLL ql=0,qr=0;
			for (auto o:l) {
				if (isdigit(o)) ql=ql*B+o-'0';
				else ql=ql*B+o-'a'+10;
			}
			for (auto o:r) {
				if (isdigit(o)) qr=qr*B+o-'0';
				else qr=qr*B+o-'a'+10;
			}
			uLL res;
			if (ql) res=calc(qr)-calc(ql-1);
			else res=calc(qr);
//			cout<<ql<<' '<<qr<<"\n";
			if (!res) {
				cout<<"0\n";
				continue;
			}
			int bb[N]={},ccnt=0;
//			cout<<res<<"\n";
			while (res) bb[++ccnt]=res%B,res/=B;
			for (int i=ccnt;i;i--) {
				if (bb[i]>=10) cout<<(char)('a'+bb[i]-10);
				else cout<<(char)('0'+bb[i]);
			}
			cout<<"\n";
		}
	}
    return 0;
}

/*

2
h 1 147a
d 0 25 71

*/

詳細信息

Test #1:

score: 100
Accepted
time: 38ms
memory: 23268kb

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: 1587ms
memory: 27116kb

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