#include <bits/stdc++.h> using namespace std; #define int long long #define ii pair<int,int> #define iii tuple<int,int,int> #define fi first #define se second #define endl '\n' #define pub push_back #define pob pop_back #define puf push_front #define pof pop_front #define lb lower_bound #define ub upper_bound #define rep(x,start,end) for(int x=(start)-((start)>(end));x!=(end)-((start)>(end));((start)<(end)?x++:x--)) #define all(x) (x).begin(),(x).end() #define sz(x) (int)(x).size() mt19937 rng(chrono::system_clock::now().time_since_epoch().count()); vector<ii> v[18]; vector<ii> proc(vector<ii> v){ sort(all(v)); vector<ii> res; for (auto [a,b]:v){ if (a%10==0) continue; if (!res.empty() && res.back().fi==a) res.back().se+=b; else res.pub({a,b}); } return res; } int f(int x){ int res=1; while (x){ res*=x%10; x/=10; } return res; } int g(int x){ while (x>=10) x=f(x); return x; } int n; int ans[10]; map<ii,vector<int> > m; void solve(int cnt,int mul){ if (mul%10==0) return; if (!m.count(ii(cnt,mul))){ vector<int> res(10,0); for (auto [a,b]:v[cnt]){ int v=g(mul*a); if (v) res[v]+=b; } m[ii(cnt,mul)]=res; } rep(x,0,10) ans[x]+=m[ii(cnt,mul)][x]; } signed main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin.exceptions(ios::badbit | ios::failbit); v[0]={{1,1}}; rep(x,0,17){ for (auto [a,b]:v[x]) rep(y,1,10) v[x+1].pub({a*y,b}); v[x+1]=proc(v[x+1]); } int TC; cin>>TC; while (TC--){ cin>>n; rep(x,0,10) ans[x]=0; ans[0]=n; if (n%10==0) n--; int p=1; int c=1; int cnt=0; while (c*10<=n) c*=10,cnt++; rep(x,1,cnt+1){ solve(x,1); } while (c){ rep(x,1,(n/c)%10){ solve(cnt,p*x); } p*=(n/c)%10; c/=10; cnt--; } p=g(p); if (p) ans[p]++; rep(x,1,10) ans[0]-=ans[x]; rep(x,0,10) cout<<ans[x]<<" \n"[x==9]; } }
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 | #include <bits/stdc++.h> using namespace std; #define int long long #define ii pair<int,int> #define iii tuple<int,int,int> #define fi first #define se second #define endl '\n' #define pub push_back #define pob pop_back #define puf push_front #define pof pop_front #define lb lower_bound #define ub upper_bound #define rep(x,start,end) for(int x=(start)-((start)>(end));x!=(end)-((start)>(end));((start)<(end)?x++:x--)) #define all(x) (x).begin(),(x).end() #define sz(x) (int)(x).size() mt19937 rng(chrono::system_clock::now().time_since_epoch().count()); vector<ii> v[18]; vector<ii> proc(vector<ii> v){ sort(all(v)); vector<ii> res; for (auto [a,b]:v){ if (a%10==0) continue; if (!res.empty() && res.back().fi==a) res.back().se+=b; else res.pub({a,b}); } return res; } int f(int x){ int res=1; while (x){ res*=x%10; x/=10; } return res; } int g(int x){ while (x>=10) x=f(x); return x; } int n; int ans[10]; map<ii,vector<int> > m; void solve(int cnt,int mul){ if (mul%10==0) return; if (!m.count(ii(cnt,mul))){ vector<int> res(10,0); for (auto [a,b]:v[cnt]){ int v=g(mul*a); if (v) res[v]+=b; } m[ii(cnt,mul)]=res; } rep(x,0,10) ans[x]+=m[ii(cnt,mul)][x]; } signed main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cin.exceptions(ios::badbit | ios::failbit); v[0]={{1,1}}; rep(x,0,17){ for (auto [a,b]:v[x]) rep(y,1,10) v[x+1].pub({a*y,b}); v[x+1]=proc(v[x+1]); } int TC; cin>>TC; while (TC--){ cin>>n; rep(x,0,10) ans[x]=0; ans[0]=n; if (n%10==0) n--; int p=1; int c=1; int cnt=0; while (c*10<=n) c*=10,cnt++; rep(x,1,cnt+1){ solve(x,1); } while (c){ rep(x,1,(n/c)%10){ solve(cnt,p*x); } p*=(n/c)%10; c/=10; cnt--; } p=g(p); if (p) ans[p]++; rep(x,1,10) ans[0]-=ans[x]; rep(x,0,10) cout<<ans[x]<<" \n"[x==9]; } } |