题解:P16945 「LAOI-18」数字表示
普通用户提示:
在https://www.luogu.com.cn/discuss/1435357 中,GPT:
@[514727] 预处理调用 qpow(2,36) 等奇指数时,最后一次循环更新仍执行 a=a*a。该平方可能超出 signed long long(例如最后把 2^32 平方为 2^64),虽结果已算完但 C++ 有符号溢出仍是未定义行为:https://www.luogu.com.cn/article/hw44mazn
这错误真是太惊人了,为了防止程序出现未定义行为导致大家都变成可爱的猫娘,那就改一下吧。
大模拟。
我们发现除了
我们先考虑三次运算的情况,此时后缀表达式只有
a b op1 c op2 d op3a b op1 c d op2 op3a b c op1 op2 d op3a b c op1 d op2 op3a b c d op1 op2 op3
分别对应
我们再仔细分析一下第一种情况各种运算符能取什么,首先第一个运算符一定是乘方,去掉重复情况后,后两个运算符可能为 +*,*+,-*,*-,+/,/+,-/,/- 或 +^,-^,*^,/^,且后一组情况的第二个乘方运算的指数只能是
以上情况不多,直接枚举运算符和数字即可,建议先处理出所有
再来看两次运算的情况,以同样的方式分析可以得出必要的表达式只有
注意
一种运算符的情况就只能是
总共只有大约
3k+ 的代码:
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
inline ll qpow(unsigned long long a,ll b){
ll res=1;
for (;b;b>>=1,a=a*a) if (b&1) res=res*a;
return res;
}
inline int len(ll 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<10000){
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;
}