#include <bits/stdc++.h>//1 20001000
using namespace std;
#define FOR(i,p,q) for(int i=(p); i<=(q); ++i)
#define ROF(i,p,q) for(int i=(p); i>=(q); --i)
#define REP(i,q) for(int i=0; i<(q); ++i)
#define pb push_back
#define as assign
#define rz resize
#define Co const
#define all(X) X.begin(), X.end()
#define rall(X) X.rbegin(), X.rend()
#define sz(X) (int)(X.size())
#define ckmax(a,b) a=max(a,b)
#define ckmin(a,b) a=min(a,b)
#define V vector
typedef long long ll;
typedef mt19937_64 mt;
#ifndef UNCLE
typedef basic_string<bool> vb;
typedef basic_string<int> vi;
typedef basic_string<ll> vl;
#else
typedef V<bool> vb;
typedef V<int> vi;
typedef V<ll> vl;
#endif
int M=0,mm;
unordered_map<ll,int> basToId;
vl idToBas;
V<short> basCol;
V<vl> qtNum;
void Prepro(){
V<vl> pw(10,vl(18,-1));
vl fct(18,-1);
FOR(ip,1,9){
pw[ip][0]=1;
FOR(i,1,17) pw[ip][i]=pw[ip][i-1]*ip;
}
fct[0]=1;
FOR(i,1,17) fct[i]=fct[i-1]*i; // <(ll)
auto GtRsOn=[](ll x){
while(x>=10){
ll nx=1;
while(x) nx*=x%10,x/=10;
x=nx;
}
return (short)x;
};
for(short q2=0;q2<=17;++q2) // ustawiam basy - potencjalnie moge zoptowac
for(short q3=0;q2+q3<=17;++q3)
for(short q4=0;q2+q3+q4<=17;++q4)
for(short q5=0;q2+q3+q4+q5<=17;++q5)
for(short q6=0;q2+q3+q4+q5+q6<=17;++q6)
for(short q7=0;q2+q3+q4+q5+q6+q7<=17;++q7)
for(short q8=0;q2+q3+q4+q5+q6+q7+q8<=17;++q8)
for(short q9=0;q2+q3+q4+q5+q6+q7+q8+q9<=17;++q9){
ll cBs=pw[2][q2]*pw[3][q3]*pw[4][q4]*pw[5][q5]*pw[6][q6]*pw[7][q7]*pw[8][q8]*pw[9][q9];
short ccol=GtRsOn(cBs);
if(ccol==0||basToId.count(cBs)) continue; //goat
basToId[cBs]=M++; //M=165 (tylko)
idToBas.pb(cBs), basCol.pb(ccol);
}
mm=M;
for(short q2=0;q2<=17;++q2) //potencjalnie moge zoptowac
for(short q3=0;q2+q3<=17;++q3)
for(short q4=0;q2+q3+q4<=17;++q4)
for(short q5=0;q2+q3+q4+q5<=17;++q5)
for(short q6=0;q2+q3+q4+q5+q6<=17;++q6)
for(short q7=0;q2+q3+q4+q5+q6+q7<=17;++q7)
for(short q8=0;q2+q3+q4+q5+q6+q7+q8<=17;++q8)
for(short q9=0;q2+q3+q4+q5+q6+q7+q8+q9<=17;++q9){
ll cBs=pw[2][q2]*pw[3][q3]*pw[4][q4]*pw[5][q5]*pw[6][q6]*pw[7][q7]*pw[8][q8]*pw[9][q9];
if(basToId.count(cBs)) continue;
short ccol=GtRsOn(cBs);
basToId[cBs]=mm++;
idToBas.pb(cBs), basCol.pb(ccol);
}
qtNum.as(18,vl(mm,0));
for(short q2=0;q2<=17;++q2) //potencjalnie moge zoptowac
for(short q3=0;q2+q3<=17;++q3)
for(short q4=0;q2+q3+q4<=17;++q4)
for(short q5=0;q2+q3+q4+q5<=17;++q5)
for(short q6=0;q2+q3+q4+q5+q6<=17;++q6)
for(short q7=0;q2+q3+q4+q5+q6+q7<=17;++q7)
for(short q8=0;q2+q3+q4+q5+q6+q7+q8<=17;++q8)
for(short q9=0;q2+q3+q4+q5+q6+q7+q8+q9<=17;++q9){
ll cBs=pw[2][q2]*pw[3][q3]*pw[4][q4]*pw[5][q5]*pw[6][q6]*pw[7][q7]*pw[8][q8]*pw[9][q9];
//~~czyli konczylby na zero~~ FALSZ -> to mi sie i tak przyda, [choc nie musze sie iterowac]
int cId=basToId[cBs];
for(short q1=0;q1+q2+q3+q4+q5+q6+q7+q8+q9<=17;++q1){
ll pro=fct[q1+q2+q3+q4+q5+q6+q7+q8+q9];
pro/=(fct[q1]*fct[q2]*fct[q3]*fct[q4]*fct[q5]*fct[q6]*fct[q7]*fct[q8]*fct[q9]);
qtNum[q1+q2+q3+q4+q5+q6+q7+q8+q9][cId]+=pro;
}
}
}
void Solve(){
ll N;
cin>>N;
vl qt(10,0);
qt[0]=N;
if(N==1'000'000'000'000'000'000) --N;
string s=to_string(N);
REP(im,M){
if(idToBas[im]>N) continue;
FOR(i,1,sz(s)-1) qt[basCol[im]]+=qtNum[i][im];
ll ilPre=1;
FOR(i,0,sz(s)-1){
if(ilPre==0||idToBas[im]<ilPre||idToBas[im]%ilPre!=0) break;
if(i==0&&s[0]=='1') continue;
FOR(j,1,s[i]-'0'-1){
ll cIl=ilPre*j;
if(idToBas[im]<cIl||idToBas[im]%cIl!=0) continue;
if(i!=sz(s)-1){
if(basToId.count(idToBas[im]/cIl)) qt[basCol[im]]+=qtNum[sz(s)-i-1][basToId[idToBas[im]/cIl]];
}
else if(cIl==idToBas[im]) ++qt[basCol[im]];
}
ilPre*=(s[i]-'0');
}
if(ilPre==idToBas[im]) ++qt[basCol[im]];
}
FOR(i,1,9) qt[0]-=qt[i];
FOR(i,0,9) cout<<qt[i]<<" ";
cout<<"\n";
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
Prepro();
int T;cin>>T;
while(T--) Solve();
}
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 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | #include <bits/stdc++.h>//1 20001000 using namespace std; #define FOR(i,p,q) for(int i=(p); i<=(q); ++i) #define ROF(i,p,q) for(int i=(p); i>=(q); --i) #define REP(i,q) for(int i=0; i<(q); ++i) #define pb push_back #define as assign #define rz resize #define Co const #define all(X) X.begin(), X.end() #define rall(X) X.rbegin(), X.rend() #define sz(X) (int)(X.size()) #define ckmax(a,b) a=max(a,b) #define ckmin(a,b) a=min(a,b) #define V vector typedef long long ll; typedef mt19937_64 mt; #ifndef UNCLE typedef basic_string<bool> vb; typedef basic_string<int> vi; typedef basic_string<ll> vl; #else typedef V<bool> vb; typedef V<int> vi; typedef V<ll> vl; #endif int M=0,mm; unordered_map<ll,int> basToId; vl idToBas; V<short> basCol; V<vl> qtNum; void Prepro(){ V<vl> pw(10,vl(18,-1)); vl fct(18,-1); FOR(ip,1,9){ pw[ip][0]=1; FOR(i,1,17) pw[ip][i]=pw[ip][i-1]*ip; } fct[0]=1; FOR(i,1,17) fct[i]=fct[i-1]*i; // <(ll) auto GtRsOn=[](ll x){ while(x>=10){ ll nx=1; while(x) nx*=x%10,x/=10; x=nx; } return (short)x; }; for(short q2=0;q2<=17;++q2) // ustawiam basy - potencjalnie moge zoptowac for(short q3=0;q2+q3<=17;++q3) for(short q4=0;q2+q3+q4<=17;++q4) for(short q5=0;q2+q3+q4+q5<=17;++q5) for(short q6=0;q2+q3+q4+q5+q6<=17;++q6) for(short q7=0;q2+q3+q4+q5+q6+q7<=17;++q7) for(short q8=0;q2+q3+q4+q5+q6+q7+q8<=17;++q8) for(short q9=0;q2+q3+q4+q5+q6+q7+q8+q9<=17;++q9){ ll cBs=pw[2][q2]*pw[3][q3]*pw[4][q4]*pw[5][q5]*pw[6][q6]*pw[7][q7]*pw[8][q8]*pw[9][q9]; short ccol=GtRsOn(cBs); if(ccol==0||basToId.count(cBs)) continue; //goat basToId[cBs]=M++; //M=165 (tylko) idToBas.pb(cBs), basCol.pb(ccol); } mm=M; for(short q2=0;q2<=17;++q2) //potencjalnie moge zoptowac for(short q3=0;q2+q3<=17;++q3) for(short q4=0;q2+q3+q4<=17;++q4) for(short q5=0;q2+q3+q4+q5<=17;++q5) for(short q6=0;q2+q3+q4+q5+q6<=17;++q6) for(short q7=0;q2+q3+q4+q5+q6+q7<=17;++q7) for(short q8=0;q2+q3+q4+q5+q6+q7+q8<=17;++q8) for(short q9=0;q2+q3+q4+q5+q6+q7+q8+q9<=17;++q9){ ll cBs=pw[2][q2]*pw[3][q3]*pw[4][q4]*pw[5][q5]*pw[6][q6]*pw[7][q7]*pw[8][q8]*pw[9][q9]; if(basToId.count(cBs)) continue; short ccol=GtRsOn(cBs); basToId[cBs]=mm++; idToBas.pb(cBs), basCol.pb(ccol); } qtNum.as(18,vl(mm,0)); for(short q2=0;q2<=17;++q2) //potencjalnie moge zoptowac for(short q3=0;q2+q3<=17;++q3) for(short q4=0;q2+q3+q4<=17;++q4) for(short q5=0;q2+q3+q4+q5<=17;++q5) for(short q6=0;q2+q3+q4+q5+q6<=17;++q6) for(short q7=0;q2+q3+q4+q5+q6+q7<=17;++q7) for(short q8=0;q2+q3+q4+q5+q6+q7+q8<=17;++q8) for(short q9=0;q2+q3+q4+q5+q6+q7+q8+q9<=17;++q9){ ll cBs=pw[2][q2]*pw[3][q3]*pw[4][q4]*pw[5][q5]*pw[6][q6]*pw[7][q7]*pw[8][q8]*pw[9][q9]; //~~czyli konczylby na zero~~ FALSZ -> to mi sie i tak przyda, [choc nie musze sie iterowac] int cId=basToId[cBs]; for(short q1=0;q1+q2+q3+q4+q5+q6+q7+q8+q9<=17;++q1){ ll pro=fct[q1+q2+q3+q4+q5+q6+q7+q8+q9]; pro/=(fct[q1]*fct[q2]*fct[q3]*fct[q4]*fct[q5]*fct[q6]*fct[q7]*fct[q8]*fct[q9]); qtNum[q1+q2+q3+q4+q5+q6+q7+q8+q9][cId]+=pro; } } } void Solve(){ ll N; cin>>N; vl qt(10,0); qt[0]=N; if(N==1'000'000'000'000'000'000) --N; string s=to_string(N); REP(im,M){ if(idToBas[im]>N) continue; FOR(i,1,sz(s)-1) qt[basCol[im]]+=qtNum[i][im]; ll ilPre=1; FOR(i,0,sz(s)-1){ if(ilPre==0||idToBas[im]<ilPre||idToBas[im]%ilPre!=0) break; if(i==0&&s[0]=='1') continue; FOR(j,1,s[i]-'0'-1){ ll cIl=ilPre*j; if(idToBas[im]<cIl||idToBas[im]%cIl!=0) continue; if(i!=sz(s)-1){ if(basToId.count(idToBas[im]/cIl)) qt[basCol[im]]+=qtNum[sz(s)-i-1][basToId[idToBas[im]/cIl]]; } else if(cIl==idToBas[im]) ++qt[basCol[im]]; } ilPre*=(s[i]-'0'); } if(ilPre==idToBas[im]) ++qt[basCol[im]]; } FOR(i,1,9) qt[0]-=qt[i]; FOR(i,0,9) cout<<qt[i]<<" "; cout<<"\n"; } int main(){ ios_base::sync_with_stdio(0); cin.tie(0), cout.tie(0); Prepro(); int T;cin>>T; while(T--) Solve(); } |
English