UOJ Logo

NOI.AC

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#199785#2989. 优美数wosile1001020ms10644kbC++111.9kb2023-12-21 09:42:382023-12-21 12:02:04

answer

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define eps 1e-8
#define inf 0x3f3f3f3f
#define INF 0x3f3f3f3f3f3f3f3f
__int128 p9[25],f[25],suf[25];
__int128 n;
char s[105];
map<__int128,__int128>mp[25][25];
__int128 dfs(int d,__int128 p,int z,int lim){
	// printf("dfs %d %d %d %d\n",d,(int)p,z,lim);
	if(d>=lim)return p>=f[lim];
	if(p>=f[lim])return z==1?suf[d]:p9[lim-d];
	if(p*p9[lim-d]<f[lim])return 0;
	if(z==0 && mp[lim][d].find(p)!=mp[lim][d].end())return mp[lim][d][p];
	__int128 sum=0;
	int top=z==1?(s[d]-'0'):9;
	for(int i=1;i<=top;i++)sum+=dfs(d+1,p*i,z&(i==top),lim);
	if(z==0)mp[lim][d][p]=sum;
	// printf("(%d,%d,%d,%d) ans=%d\n",d,(int)p,z,lim,(int)sum);
	return sum;
}
namespace IO{
	int read(){
		int x=0,c=getchar(),f=1;
		while(c<'0' || c>'9'){
			if(c=='-')f=-1;
			c=getchar();
		}
		while(c>='0' && c<='9'){
			x=(x<<3)+(x<<1)+c-'0';
			c=getchar();
		}
		return f==1?x:-x;
	}
	void write(__int128 x){
		if(x<0){
			putchar('-');
			write(-x);
			return;
		}
		if(x>9)write(x/10);
		putchar(x%10+48);
	}
	void write(__int128 x,char div){
		write(x);
		putchar(div);
	}
	void write(const char *s){
		int pos=0;
		while(s[pos])putchar(s[pos++]);
	}
}
using namespace IO;
int main(){
	f[0]=p9[0]=1;
	for(int i=1;i<=23;i++){
		f[i]=f[i-1]*i;
		p9[i]=p9[i-1]*9;
	}
	int T=read();
	while(T--){
		scanf("%s",s);
		__int128 ans=0;
		if(strlen(s)>=22){
			for(int i=1;i<=22;i++)ans+=dfs(0,1,0,i);
		}
		else{
			int len=strlen(s);
			n=0;
			for(int i=0;i<len;i++)n=n*10+s[i]-'0';
			suf[len]=1;
			for(int i=len-1;i>=0;i--)suf[i]=max((__int128)0,suf[i+1]+(s[i]-'1')*p9[len-1-i]);
			// for(int i=0;i<len;i++)write(suf[i],32);
			// write("\n");
			for(int i=1;i<len;i++)ans+=dfs(0,1,0,i);
			// write(ans,10);
			// write("*******\n");
			ans+=dfs(0,1,1,len);
		}
		write(ans,10);
	}
	return 0;
}
//quod erat demonstrandum

Details

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

Test #1:

score: 10
Accepted
time: 13ms
memory: 1268kb

input:

10000
47837
518455
655112
954345
600612
439802
420101
521472
537870
564701
532021
80484
18346
373734...

output:

28521
255105
331170
498198
305480
214091
203166
256272
266189
282501
262266
49149
10929
181859
39705...

result:

ok 10000 numbers

Test #2:

score: 10
Accepted
time: 15ms
memory: 1328kb

input:

10000
4058707
38032
2504347
7593417
4041877
8727030
1346770
7742834
7692976
554753
2279696
5140166
2...

output:

1576280
22467
940084
3142681
1576280
3683468
565983
3220642
3197318
276248
852824
2010866
931280
344...

result:

ok 10000 numbers

Test #3:

score: 10
Accepted
time: 127ms
memory: 9952kb

input:

10000
980475503196470826
409488057248738093
964119271402833647
578964283299264159
142509873610207494...

output:

2071241664423
1970523647942
2056479141675
1974294607826
1969798331582
1603540352679
1971880091330
19...

result:

ok 10000 numbers

Test #4:

score: 10
Accepted
time: 138ms
memory: 9948kb

input:

10000
810356108140771788
300489122283682456
762404595750439923
480865512993981847
550163957708337313...

output:

2015446574701
1969871629532
1993921074841
1971184218783
1973110540433
2052181582591
1987832109612
19...

result:

ok 10000 numbers

Test #5:

score: 10
Accepted
time: 115ms
memory: 10640kb

input:

3
8212662212468102540154365
19445655726298305109
97190067946627003

output:

2121793450243
2121511412240
1861031606514

result:

ok 3 number(s): "2121793450243 2121511412240 1861031606514"

Test #6:

score: 10
Accepted
time: 114ms
memory: 10640kb

input:

3
6483332648160372
3892040543298291125323
40247259935

output:

1031619953982
2121793450243
2613238047

result:

ok 3 number(s): "1031619953982 2121793450243 2613238047"

Test #7:

score: 10
Accepted
time: 112ms
memory: 10640kb

input:

200
282198
9455423614089
560151079866656
8058866664071192
18431809
622570433460150897110199
53873023...

output:

136630
122292625535
484191134993
1216741215387
5645680
2121793450243
58672426490
336568368
212179345...

result:

ok 200 numbers

Test #8:

score: 10
Accepted
time: 120ms
memory: 10640kb

input:

200
248212546060755377765
2951478667201
5087
9147186474579
918
7
65639
931533331045710461
8192108437...

output:

2121793167295
41565106450
3505
119237514903
729
7
39570
2052221635237
682457002854
6279
212169718855...

result:

ok 200 numbers

Test #9:

score: 10
Accepted
time: 132ms
memory: 10612kb

input:

10000
6112878206338759544
30104707437906067469
99979487336233526005
55863650164083552054
87604483409...

output:

2107155938386
2121511412241
2121744195978
2121511594073
2121552264516
2121511420735
2121511412240
21...

result:

ok 10000 numbers

Test #10:

score: 10
Accepted
time: 134ms
memory: 10644kb

input:

10000
64229512433483029478
3662181692760349362
12485423318924806225
34778296778156581266
31579270837...

output:

2121513598993
2106700807499
2121511412240
2121511412241
2106700763074
2121627144876
2106712293588
21...

result:

ok 10000 numbers

Extra Test:

score: 0
Extra Test Passed