跳到正文

题解

题解:P16945 「LAOI-18」数字表示

bcdmwSjy约 9 分钟
原文

这是 2026年6月19日 存档的第 2 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2026年6月19日
收录
2026年6月19日
更新
2026年11月16日
篇幅
约 9 分钟
获赞
4
题目
P16945

版本

  1. v3当前
  2. v2
  3. v1

大模拟。

我们发现除了 10910^9,剩下的数字的长度都不超过 99,要想使表达式变短,那表达式的长度就必须小于等于 88,此时最多只能进行三次运算。并且容易证明,如果只有加减乘除,那么长度就不可能变短,所以必须要有一次乘方运算。

我们先考虑三次运算的情况,此时后缀表达式只有 55 种不同的情况:

  1. a b op1 c op2 d op3
  2. a b op1 c d op2 op3
  3. a b c op1 op2 d op3
  4. a b c op1 d op2 op3
  5. a b c d op1 op2 op3

分别对应 ((a⊕b)⊕c)⊕d,(a⊕b)⊕(c⊕d),(a⊕(b⊕c))⊕d,a⊕((b⊕c)⊕d),a⊕(b⊕(c⊕d))((a\oplus b)\oplus c)\oplus d,(a\oplus b)\oplus(c\oplus d),(a\oplus(b\oplus c))\oplus d,a\oplus((b\oplus c)\oplus d),a\oplus(b\oplus(c\oplus d)),其中 ⊕\oplus 可为加、减、乘、除、乘方中的任意一个运算符。并且我们不会让一个直接的数字去减或除以一个乘方后的结果,并且加法和乘法还有交换和结合律,可以发现后三种情况都是无用的,且第二种情况中只能是 ab⊕cda^b\oplus c^d(⊕\oplus 为加、减、乘、除中的一个)。

我们再仔细分析一下第一种情况各种运算符能取什么,首先第一个运算符一定是乘方,去掉重复情况后,后两个运算符可能为 +*,*+,-*,*-,+/,/+,-/,/- 或 +^,-^,*^,/^,且后一组情况的第二个乘方运算的指数只能是 22。

以上情况不多,直接枚举运算符和数字即可,建议先处理出所有 aba^b 形式的表达式(a,b≥2,ab≤109a,b\ge2,a^b\le10^9)。

再来看两次运算的情况,以同样的方式分析可以得出必要的表达式只有 (a⊕b)⊕c(a\oplus b)\oplus c 一种。同样枚举即可。

注意 aba^b 形式的表达式要多处理一点,因为可能出现 ab−cda^b-c^d 或 ab÷ca^b\div c 的情况,如 686321335=230−99686321335=2^{30}-9^9,处理到 a,b≤99,ab≤1012a,b\le99,a^b\le10^{12} 就一定没问题了。

一种运算符的情况就只能是 aba^b 了,已经考虑过了。

总共只有不到 6×1056\times10^5 种表达式会使数字变短,全都存起来,每次询问直接输出即可。

3k+ 的代码:

C++
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;

inline ll qpow(ll a,ll b){
	ll res=1;
	for (;b;b>>=1,a=a*a) if (b&1) res=res*a;
	return res;
}
inline int len(int x){
	return x>=10000?(x>=1000000?(x>=10000000?(x>=100000000?9:8):7):(x>=100000?6:5)):(x>=100?(x>=1000?4:3):(x>=10?2:1));
}
inline ll calc(ll x,ll y,int t){
	if (t==0) return x+y;
	else if (t==1){
		ll r=0;
		return __builtin_mul_overflow(x,y,&r)?-1:r;
	}
	else if (t==2) return x-y;
	else return x%y==0?x/y:-1;
}
inline string o2s(const string &x,const string &y,int t){
	if (t==0) return x+" "+y+" +";
	else if (t==1) return x+" "+y+" *";
	else if (t==2) return x+" "+y+" -";
	else if (t==3) return x+" "+y+" /";
	else return x+" "+y+" ^";
}
inline string o2s(const string &x,int y,int t){return o2s(x,to_string(y),t);}
inline string o2s(int x,int y,int t){return o2s(to_string(x),y,t);}

unordered_map<ll,int> minl;
unordered_map<ll,string> mine;
inline int getmin(int x){
	if (not minl.contains(x)) return len(x);
	else return minl[x];
}
inline string getmine(int x){
	if (not mine.contains(x)) return to_string(x);
	else return mine[x];
}
inline void updmin(ll x,int y,const string &e){
	if (x>1000000000 or y>=len(x)) return;
	if (not minl.contains(x)) minl[x]=y,mine[x]=e;
	else if (minl[x]>y) minl[x]=y,mine[x]=e;
}

const pair<int,int> ops[8]={{0,1},{1,0},{2,1},{1,2},{0,3},{3,0},{2,3},{3,2}};

int main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	cout.tie(nullptr);
	vector<array<ll,3>> v;
	for (int i=2;i<=31622;i++){
		int lim=9/log10(i);
		for (int j=2;j<=lim;j++){
			ll t=qpow(i,j),l=len(t);
			if (l>=4) v.push_back({i,j,t});
		}
		if (i<100){
			int lim2=12/log10(i);
			for (int j=lim+1;j<=lim2;j++){
				ll t=qpow(i,j),l=len(t);
				if (l>=4) v.push_back({i,j,t});
			}
		}
	}
	sort(v.begin(),v.end(),[](array<ll,3> u,array<ll,3> v){return len(u[0])+len(u[1])<len(v[0])+len(v[1]);});
	for (auto [a,b,r]:v){
		string e=o2s(a,b,4);
		int lab=len(a)+len(b);
		updmin(r,lab+1,e);
		if (lab<=3){
			for (int c=1;c<100;c++){
				int labc=lab+len(c);
				if (labc<=4){
					for (auto [op1,op2]:ops){
						ll r1=calc(r,c,op1);
						if (r1<0) continue;
						for (int d=1;d<100;d++){
							int labcd=labc+len(d);
							if (labcd<=5){
								ll r2=calc(r1,d,op2);
								if (r2>0 and r2<1000000000) updmin(r2,labcd+3,o2s(o2s(e,c,op1),d,op2));
							}else break;
						}
					}
					for (int op=0;op<4;op++){
						ll r1=calc(r,c,op);
						if (r1<0) continue;
						ll r2=calc(r1,r1,1);
						if (r2>0 and r2<1000000000) updmin(r2,labc+4,o2s(o2s(e,c,op),2,4));
					}
				}else break;
			}
			for (auto [c,d,r2]:v){
				int labcd=lab+len(c)+len(d);
				if (labcd<=5){
					for (int op=0;op<4;op++){
						ll r3=calc(r,r2,op);
						if (r3>0 and r2<1000000000) updmin(r3,labcd+3,o2s(e,o2s(c,d,4),op));
					}
				}else break;
			}
		}
		if (lab<=5){
			for (int c=1;c<10000;c++){
				int labc=lab+len(c);
				if (labc<=6){
					for (int op=0;op<4;op++){
						ll r2=calc(r,c,op);
						if (r2>0 and r2<1000000000) updmin(r2,labc+2,o2s(e,c,op));
					}
				}else break;
			}
		}
	}
	int T;
	cin>>T;
	while (T--){
		int x;
		cin>>x;
		cout<<getmin(x)<<"\n"<<getmine(x)<<"\n";
	}
	return 0;
}