QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#575048#2519. Number with BachelorsPatrickStarAC ✓168ms26180kbC++143.4kb2024-09-19 10:12:192024-09-19 10:12:24

Judging History

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

  • [2024-09-19 10:12:24]
  • 评测
  • 测评结果:AC
  • 用时:168ms
  • 内存:26180kb
  • [2024-09-19 10:12:19]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define ll unsigned long long
int n, dig[25];
ll dp1[25][2050];
ll dfs1(int i, int u, bool limit) {
	if (i==0) return 1;
	if (limit) {
		ll ans = 0;
		if (!u) ans+=dfs1(i-1, u, (dig[i]==0));
		else if (!(u&1)) ans+=dfs1(i-1, u|1, (dig[i]==0));
		for (int p=1;p<=dig[i];p++) {
			if (!((u>>p)&1)) ans+=dfs1(i-1, u|(1<<p), (p==dig[i]));
		}
		return ans;
	}
	else {
		if (dp1[i][u]!=-1) return dp1[i][u];
		ll ans = 0;
		if (!u) ans+=dfs1(i-1, u, 0);
		else if (!(u&1)) ans+=dfs1(i-1, u|1, 0);
		for (int p=1;p<=9;p++) {
			if (!((u>>p)&1)) ans+=dfs1(i-1, u|(1<<p), 0);
		}
		return dp1[i][u]=ans;
	}
}
__int128 dp2[20][70005];
__int128 dfs2(int i, int u, bool limit) {
	if (i==0) return 1;
	if (limit) {
		ll ans = 0;
		if (!u) ans+=dfs2(i-1, u, (dig[i]==0));
		else if (!(u&1)) ans+=dfs2(i-1, u|1, (dig[i]==0));
		for (int p=1;p<=dig[i];p++) {
			if (!((u>>p)&1)) ans+=dfs2(i-1, u|(1<<p), (p==dig[i]));
		}
		return ans;
	}
	else {
		if (dp2[i][u]!=-1) return dp2[i][u];
		ll ans = 0;
		if (!u) ans+=dfs2(i-1, u, 0);
		else if (!(u&1)) ans+=dfs2(i-1, u|1, 0);
		for (int p=1;p<=15;p++) {
			if (!((u>>p)&1)) ans+=dfs2(i-1, u|(1<<p), 0);
		}
		return dp2[i][u]=ans;
	}
}
__int128 read() {
	__int128 ans;
	char i;
	while (i=getchar(), !((i>='0'&&i<='9')||(i>='a'&&i<='f')));
	if (i>='0'&&i<='9') ans = i-'0';
	else ans = 10+i-'a';
	while (i=getchar(), (i>='0'&&i<='9')||(i>='a'&&i<='f')) {
		if (i>='0'&&i<='9') ans = ans*16+i-'0';
		else ans = ans*16+10+i-'a';
	}
	return ans;
}
void write(__int128 x) {
	if (x<16) {
		if (x<=9) putchar(x+'0');
		else putchar('a'+x-10);
		return ;
	}
	write(x/16);
	x%=16;
	if (x<=9) putchar(x+'0');
	else putchar('a'+x-10);
	return ;
}
int main() {
	memset(dp1, -1, sizeof(dp1));
	memset(dp2, -1, sizeof(dp2));
	scanf("%d", &n);
	for (int p=1;p<=n;p++) {
		char tp;
		cin>>tp;
		bool op;
		scanf("%d", &op);
		if (tp=='d') {
			if (!op) {
				ll l, r;
				scanf("%llu %llu", &l, &r);
				int tot = 0;
				while (r) {
					dig[++tot] = r%10;
					r/=10;
				}
				ll ans = dfs1(tot, 0, 1);
				if (l) {
					tot = 0;
					l--;
					while (l) {
						dig[++tot] = l%10;
						l/=10;
					}
					ans-=dfs1(tot, 0, 1);
				}
				printf("%llu\n", ans);
			}
			else {
				ll x;
				scanf("%llu", &x);
				ll l = 0, r = (1ll<<60);
				while (l<r) {
					ll mid = (l+r)/2;
					int tot = 0;
					ll t = mid;
					while (t) {
						dig[++tot] = t%10;
						t/=10;
					}
					if (dfs1(tot, 0, 1)>=x) r = mid;
					else l = mid+1;
				}
				if (r==(1ll<<60)) puts("-");
				else printf("%llu\n", l);
			}
		}
		else {
			if (!op) {
				__int128 l = read(), r = read();
				int tot = 0;
				while (r) {
					dig[++tot] = r%16;
					r/=16;
				}
				__int128 ans = dfs2(tot, 0, 1);
				if (l) {
					l--;
					tot = 0;
					while (l) {
						dig[++tot] = l%16;
						l/=16;
					}
					ans-=dfs2(tot, 0, 1);
				}
				write(ans);
				putchar('\n');
			}
			else {
				__int128 x = read();
				__int128 l = 0, r = (((__int128)(1))<<70);
				while (l<r) {
					__int128 mid = (l+r)/2;
					int tot = 0;
					__int128 t = mid;
					while (t) {
						dig[++tot] = t%16;
						t/=16;
					}
					if (dfs2(tot, 0, 1)>=x) r = mid;
					else l = mid+1;
				}
				if (r==(((__int128)(1))<<70)) puts("-");
				else {
					write(l);
					putchar('\n');
				}
			}
		}
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 41ms
memory: 26180kb

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: 168ms
memory: 26068kb

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