guxuan edited 3 年,11 月前
using namespace std;
typedef long long ll;
ll n,f[N],g[N],chen[N],yilai[N];
char op[N];
void solve(){
for(ll x=1;x<=5000;++x){
chen[x]=x;
for(ll i=2;ii<=x;++i){
ll now=chen[i]+chen[x/i]+2;
if(x%i==0&&now<chen[x]) chen[x]=now,g[x]=i;
}
}
for(ll i=1;i<=5000;++i){
if(chen[i]=1;–j){
ll now=f[j]+chen[i-j]+2;
if(now<f[i]) f[i]=now,yilai[i]=j,op[i]=’+’;
}
for(ll j=2;jj<=i;++j){
ll now=chen[j]+chen[i/j]+2;
if(i%j==0&&now<f[i]) f[i]=now,yilai[i]=j,op[i]=’’;
}
}
}
void output(ll x){
if(op[x]==’’){
output(yilai[x]);
cout<<”x”;
output(x/yilai[x]);
}else if(op[x]==’+’){
output(yilai[x]);
cout<<”+”;
output(x-yilai[x]);
}else for(ll i=1;i<=x;++i) cout<<”|”;
}
int main(){
solve();
while(cin>>n){
if(f[n]>1) cout<<f[n]<<” toothpicks: “;
else cout<<”1 toothpick : “;
output(n);
cout<<”=”<<n<<endl;
}
return 0;
}
//Case 6004: wrong output line
//WRONG OUTPUT
#include<bits/stdc++.h>
#define N 5009
#define INF 1e15
using namespace std;
typedef long long ll;
ll n,f[N],g[N],chen[N];
char op[N];
void solve(ll x){
// f[x]=chen[x]=x;
// for(ll i=1;i<=x;++i){
// ll now=chen[i]+chen[x-i]+2;
// if(now<f[x]) f[x]=now,g[x]=i,op[x]='+';
// }
chen[x]=x;
for(ll i=2;i*i<=x;++i){
ll now=chen[i]+chen[x/i]+2;
if(x%i==0&&now<chen[x]) chen[x]=now,g[x]=i;
}
}
void output(ll x){
if(chen[x]==x){for(ll i=1;i<=x;++i) cout<<"|";}
else{output(x/g[x]);cout<<"*";output(g[x]);}
}
void oup(ll x){
ll s=0,num=chen[x];
for(ll i=1;i<=x;++i)
if(i+chen[x-i]+2<num) num=i+chen[x-i]+2,s=i;
if(num==1) cout<<"1 toothpick : ";
else cout<<num<<" toothpicks: ";
output(x-s);
if(s!=0) cout<<"+";
for(ll i=1;i<=s;++i) cout<<"|";
}
int main(){
for(ll i=1;i<=5000;++i) solve(i);
while(cin>>n){
oup(n);
cout<<"="<<n<<endl;
}
return 0;
}