#include<bits/stdc++.h>
#define loop(i,j,s) for(int i=j;i<=s;i++)
#define loopback(i,j,s) for(int i=j;i>=s;i--)
#define pln( x ) cout << x << "\n"
#define ps( x ) cout << x << " "
#define entr cout << "\n"
#define pcnt(i) __builtin_popcount(i)
#define ll long long
#define pb push_back
#define mp make_pair
#define ff first
#define ss second
using namespace std;
const int INFTY=20000000;
const int MAX=500100;
const int MOD=10000000;
void coutTab(int* tab,int n){
loop(i,0,n){
cout<<tab[i]<<" ";
}
cout<<"\n";
}
//------------------------------------------
bool sign=0;
void coutNum(ll t){
if(t==1)
cout<<1;
else{
cout<<"(";
loop(i,1,t){
cout<<1;
if(i!=t) cout<<"+";
}
cout<<")";
}
}
ll divide(ll t,ll sum){
//cout<<t<<"\n";
if(t<=5){
coutNum(t);
return t;
}
vector<ll> dividers;
vector<ll> howmany;
vector<bool> plusone;
int tmp;
loop(d,2,t){
int j=0;
while(t%d==0){
t=t/d;
j++;
}
if(j>0){
dividers.pb(d);
howmany.pb(j);
plusone.pb(0);
}
}
int sumd=0;
loop(i,0,dividers.size()){
sumd=sumd+(dividers[i]*howmany[i]);
}
if(sum+sumd>100){
loopback(i,dividers.size()-1,0){
if(sum+sumd<=100) break;
if(sign) cout<<"*";
cout<<"("<<"";
sign=0;
tmp=divide(pow(dividers[i],howmany[i])-1,sumd+sum-dividers[i]*howmany[i]);
sumd=sumd-dividers[i]*howmany[i]+tmp+1;
plusone[i]=1;
cout<<"+1)";
sign=1;
//cout<<"*";
}
loop(i,0,dividers.size()-1){
loop(j,1,howmany[i]){
if(sign&&!plusone[i]) cout<<"*";
if(!plusone[i]){ coutNum(dividers[i])/*cout<<dividers[i]*/; sign=1;}
//if(sign&&!plusone[i]&&(i<dividers.size()-1||j<howmany[i])){ cout<<"*"; sign=1;}
}
}
}else{
loop(i,0,dividers.size()-1){
loop(j,1,howmany[i]){
if(sign&&!plusone[i]) cout<<"*";
if(!plusone[i]){ coutNum(dividers[i])/*cout<<dividers[i]*/; sign=1;}
//if(sign&&!plusone[i]&&(i<dividers.size()-1||j<howmany[i])){ cout<<"*"; sign=1;}
} //if(plusone[i]) ps("+1");
}
}
return sumd;
}
int main(){
//ios_base::sync_with_stdio(0);
int T;
ll t;
cin>>T;
while(T--){
cin>>t;
sign=0;
divide(t,0);
entr;
}
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | #include<bits/stdc++.h> #define loop(i,j,s) for(int i=j;i<=s;i++) #define loopback(i,j,s) for(int i=j;i>=s;i--) #define pln( x ) cout << x << "\n" #define ps( x ) cout << x << " " #define entr cout << "\n" #define pcnt(i) __builtin_popcount(i) #define ll long long #define pb push_back #define mp make_pair #define ff first #define ss second using namespace std; const int INFTY=20000000; const int MAX=500100; const int MOD=10000000; void coutTab(int* tab,int n){ loop(i,0,n){ cout<<tab[i]<<" "; } cout<<"\n"; } //------------------------------------------ bool sign=0; void coutNum(ll t){ if(t==1) cout<<1; else{ cout<<"("; loop(i,1,t){ cout<<1; if(i!=t) cout<<"+"; } cout<<")"; } } ll divide(ll t,ll sum){ //cout<<t<<"\n"; if(t<=5){ coutNum(t); return t; } vector<ll> dividers; vector<ll> howmany; vector<bool> plusone; int tmp; loop(d,2,t){ int j=0; while(t%d==0){ t=t/d; j++; } if(j>0){ dividers.pb(d); howmany.pb(j); plusone.pb(0); } } int sumd=0; loop(i,0,dividers.size()){ sumd=sumd+(dividers[i]*howmany[i]); } if(sum+sumd>100){ loopback(i,dividers.size()-1,0){ if(sum+sumd<=100) break; if(sign) cout<<"*"; cout<<"("<<""; sign=0; tmp=divide(pow(dividers[i],howmany[i])-1,sumd+sum-dividers[i]*howmany[i]); sumd=sumd-dividers[i]*howmany[i]+tmp+1; plusone[i]=1; cout<<"+1)"; sign=1; //cout<<"*"; } loop(i,0,dividers.size()-1){ loop(j,1,howmany[i]){ if(sign&&!plusone[i]) cout<<"*"; if(!plusone[i]){ coutNum(dividers[i])/*cout<<dividers[i]*/; sign=1;} //if(sign&&!plusone[i]&&(i<dividers.size()-1||j<howmany[i])){ cout<<"*"; sign=1;} } } }else{ loop(i,0,dividers.size()-1){ loop(j,1,howmany[i]){ if(sign&&!plusone[i]) cout<<"*"; if(!plusone[i]){ coutNum(dividers[i])/*cout<<dividers[i]*/; sign=1;} //if(sign&&!plusone[i]&&(i<dividers.size()-1||j<howmany[i])){ cout<<"*"; sign=1;} } //if(plusone[i]) ps("+1"); } } return sumd; } int main(){ //ios_base::sync_with_stdio(0); int T; ll t; cin>>T; while(T--){ cin>>t; sign=0; divide(t,0); entr; } } |
English