Submission
Status:
PPPPPPPPPP
Subtask/Task Score:
100/100
Score: 100
User: qweqwe
Problemset: เลขฐานเบญจภาคได้ดุล
Language: cpp
Time: 0.003 second
Submitted On: 2025-10-21 10:35:17
#include <bits/stdc++.h>
#define speed cin.tie(0)->sync_with_stdio(0)
#define ll long long
#define pii pair<int,int>
using namespace std;
vector<int> expo={1,5,25,125,625,3125,15625}; // -5000<=x<=5000
int main(){
speed;
int n;cin >> n;
for (int i=0;i<n;i++){
int x;cin >> x;
if (x==0){
cout << 0 << "\n";continue;
}
int idx=0;
bool neg=0;
if (x<0) neg=1;
x=abs(x);
for (int i=0;i<7;i++){
if (x<=expo[i]){
idx=i;break;
}
}
vector<int> base(idx+1,0);
while (idx>=0){
if (idx==0 && abs(x)<=2){
base[idx]=x;break;
}
if (2*x>=expo[idx+1]){
x-=expo[idx+1];
base[idx+1]=1;
}else if (2*x<=-expo[idx+1]){
x+=expo[idx+1];
base[idx+1]=-1;
}
if ((3*expo[idx])/2<x){
x-=2*expo[idx];
base[idx]=2;
}else if (-(3*expo[idx])/2>x){
x+=2*expo[idx];
base[idx]=-2;
}
idx--;
}
if (base[0]==0)base[0]=x;
bool print=0;
vector<int> temp;
for (int i=base.size()-1;i>=0;i--){
if (base[i]!=0) print=1;
if (print){
if (neg) temp.push_back(-base[i]);
else temp.push_back(base[i]);
}
}
reverse(temp.begin(),temp.end());
for (int i:temp){
cout << i << " ";
}cout << "\n";
}
return 0;
}
/*
22
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
31 46 134 -8 -13 -134
*/