UOJ Logo

NOI.AC

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#164758#2909. numberpaul20081002686ms381532kbC++1.9kb2022-11-05 17:44:402022-11-05 17:44:41

answer

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;

const int BASE=1e5;

long long one[100005],three[100005],nine[100005];
int fa[50][100205][20],ans[50][100205],wei[100205];

long long getans(long long x,long long y)
{
	long long num=x/BASE,need=y/BASE;
	x=x-num*BASE,y=y-need*BASE;
	while(num<need || (num==need && x<=y))
	{
		int w=wei[num];
		if(num==need) //高位相同
		{
			for(int i=19;i>=0;i--)
				if(fa[w][x][i] && fa[w][x][i]<=y)
					x=fa[w][x][i];
			x=fa[w][x][0];
			break;
		}
		else
			x=ans[w][x]-BASE;
		num++;
	}

	return x+need*BASE;
}

int main()
{
	for(int i=1;i<=BASE+20;i++)
		wei[i]=wei[i/10]+i%10;

	for(int j=0;j<=45;j++)
	{
		for(int i=0;i<BASE;i++)
			fa[j][i][0]=i+j+wei[i];

		for(int i=BASE-1;i>=0;i--)
			for(int k=1;k<=19;k++)
				fa[j][i][k]=fa[j][fa[j][i][k-1]][k-1];
	}

	for(int j=0;j<=45;j++)
		for(int i=BASE-1;i>=0;i--)
		{
			if(ans[j][fa[j][i][0]])
				ans[j][i]=ans[j][fa[j][i][0]];
			else
				ans[j][i]=fa[j][i][0];
		}

	long long x=1,lenone=0,lenthree=0,lennine=0;
	while(x<=1e10)
	{
		one[++lenone]=x;
		x=x/BASE*BASE+ans[wei[x/BASE]][x%BASE];
	}
	one[++lenone]=x;

	x=3;
	while(x<=1e10)
	{
		three[++lenthree]=x;
		x=x/BASE*BASE+ans[wei[x/BASE]][x%BASE];
	}
	three[++lenthree]=x;

	x=9;
	while(x<=1e10)
	{
		nine[++lennine]=x;
		x=x/BASE*BASE+ans[wei[x/BASE]][x%BASE];
	}
	nine[++lennine]=x;

	int T;
	cin >> T;
	while(T--)
	{
		long long x,y;
		scanf("%lld %lld",&x,&y);
		if(y-x<=10000000)
		{
			printf("%lld\n",getans(x,y));
			continue;
		}

		if(x%3!=0)
			x=one[upper_bound(one+1,one+lenone+1,y)-one-1];
		else if(x%9!=0)
			x=three[upper_bound(three+1,three+lenthree+1,y)-three-1];
		else
			x=nine[upper_bound(nine+1,nine+lennine+1,y)-nine-1];

		printf("%lld\n",getans(x,y));
	}
}

详细

小提示:点击横条可展开更详细的信息

Subtask #1:

score: 20
Accepted

Test #1:

score: 20
Accepted
time: 499ms
memory: 381528kb

input:

500000
1 1
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
1 15
1 16
1 17
1 18
1 19
1 20
1 ...

output:

2
4
4
8
8
8
8
16
16
16
16
16
16
16
16
23
23
23
23
23
23
23
28
28
28
28
28
38
38
38
38
38
38
38
38
38...

result:

ok 500000 lines

Test #2:

score: 0
Accepted
time: 492ms
memory: 381532kb

input:

500000
92 99927
119 99453
481 99268
29 99908
267 99547
835 99500
955 99099
734 99774
306 99883
729 9...

output:

99941
99454
99274
99941
99555
99520
99112
99775
99900
99657
99978
100010
99545
99245
99775
99907
997...

result:

ok 500000 lines

Subtask #2:

score: 25
Accepted

Test #3:

score: 25
Accepted
time: 577ms
memory: 381532kb

input:

500000
1 1
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
1 15
1 16
1 17
1 18
1 19
1 20
1 ...

output:

2
4
4
8
8
8
8
16
16
16
16
16
16
16
16
23
23
23
23
23
23
23
28
28
28
28
28
38
38
38
38
38
38
38
38
38...

result:

ok 500000 lines

Subtask #3:

score: 25
Accepted

Test #4:

score: 25
Accepted
time: 337ms
memory: 381524kb

input:

50
4587480273 4587480273
428862505 500400481
6920415626 7358620174
7787875953 7787884613
4542304779 ...

output:

4587480321
500400482
7358620210
7787884620
4542307848
4676070172
909798356
3555627285
9508855574
511...

result:

ok 50 lines

Subtask #4:

score: 30
Accepted

Test #5:

score: 30
Accepted
time: 781ms
memory: 381532kb

input:

500000
1 1
1 2
1 3
1 4
1 5
1 6
1 7
1 8
1 9
1 10
1 11
1 12
1 13
1 14
1 15
1 16
1 17
1 18
1 19
1 20
1 ...

output:

2
4
4
8
8
8
8
16
16
16
16
16
16
16
16
23
23
23
23
23
23
23
28
28
28
28
28
38
38
38
38
38
38
38
38
38...

result:

ok 500000 lines