Problem:
E: 28
問題文
トランプゲームの大富豪において，ランクが $2$, $8$ のカードは強力です．
そこで，$10$ 進数表記で数字の $2$, $8$ のみからなる整数を
良い整数
と呼ぶことにします．
良い整数を小さいものから列挙すると $2, 8, 22, 28, 82, 88, \cdots$ となります．
$n$ を正の整数とします．$n$ が良い整数の積の形で表現できるとき，
最大でいくつの積になるか求めてください．できないなら $-1$ と出力してください．
入力
$n$
制約
$1 \leq n \leq 10^{18}$
出力
答えを $1$ 行で出力してください．
サンプル
サンプル入力1
1
サンプル出力1
-1
サンプル入力2
2
サンプル出力2
1
サンプル入力3
88
サンプル出力3
3
$2 \times 2 \times 22$ と表せます．
サンプル入力4
100
サンプル出力4
-1
サンプル入力5
173553147234869248
サンプル出力5
11
$2^6 \times 28 \times 2222^3 \times 8828$ と表せます．

Submitted solutions:

code_snippet: #include<bits/stdc++.h>
#define rep(i,a,...) for(int i = (a)*(strlen(#__VA_ARGS__)!=0);i<(int)(strlen(#__VA_ARGS__)?__VA_ARGS__:(a));++i)
#define per(i,a,...) for(int i = (strlen(#__VA_ARGS__)?__VA_ARGS__:(a))-1;i>=(int)(strlen(#__VA_ARGS__)?(a):0);--i)
#define foreach(i, n) for(auto &i:(n))
#define all(x) (x).begin(), (x).end()
#define bit(x) (1ll << (x))
#define lambda(RES_TYPE, ...) (function<RES_TYPE(__VA_ARGS__)>)[&](__VA_ARGS__) -> RES_TYPE
#define method(FUNC_NAME, RES_TYPE, ...) function<RES_TYPE(__VA_ARGS__)> FUNC_NAME = lambda(RES_TYPE, __VA_ARGS__)
using namespace std;
using ll = long long;
using pii = pair<int,int>;
using pll = pair<ll,ll>;
//const ll MOD = (ll)1e9+7;
const ll MOD = 998244353;
const int INF = (ll)1e9+7;
const ll INFLL = (ll)1e18;
template<class t>
using vvector = vector<vector<t>>;
template<class t>
using vvvector = vector<vector<vector<t>>>;
template<class t>
using priority_queuer = priority_queue<t, vector<t>, greater<t>>;
template<class t, class u> bool chmax(t &a, u b){if(a<b){a=b;return true;}return false;}
template<class t, class u> bool chmin(t &a, u b){if(a>b){a=b;return true;}return false;}
#ifdef DEBUG
#define debug(x) cout<<"LINE "<<__LINE__<<": "<<#x<<" = "<<x<<endl;
#else
#define debug(x) (void)0
#endif

namespace templates{
    ll modpow(ll x, ll b,ll mod=MOD){
        ll res = 1;
        while(b){
            if(b&1)res = res * x % mod;
            x = x * x % mod;
            b>>=1;
        }
        return res;
    }

    ll modinv(ll x){
        return modpow(x, MOD-2);
    }

    bool was_output = false;

    void print();
    template <class t> void print(const vector<t> &);
    template <class t, class u> void print(const pair<t, u> &);
    template <class t> void print(const t&);
    template <class Head, class... Tail> void print(const Head&, const Tail&...);

    template <class t> void println(const vector<vector<t>>&);
    template <class t> void println(const vector<t>&);
    template <class t> void println(const t&);
    template <class Head, class... Tail> void println(const Head&, const Tail&...);
    void println();
    void newline();

    void print(){
        return;
    }

    template <class t>
    void print(const vector<t>&x){
        for(const t&i:x)print(i);
    }
    template <class t, class u>
    void print(const pair<t,u>&p){
        print(p.first);
        print(p.second);
    }
    template <class t>
    void print(const t&x){
        if(was_output)cout<<" ";
        cout<<x;
        was_output = true;
    }
    template <class Head, class... Tail>
    void print(const Head&head,const Tail&...tail){
        print(head);
        print(tail...);
    }

    template<class t>
    void println(const vector<vector<t>>&x){
        for(vector<t> i:x)println(i);
    }
    template<class t>
    void println(const vector<t>&x){
        for(const t&i:x)print(i);
        println();
    }
    template <class t>
    void println(const t&x){
        print(x);
        println();
    }
    void println(){
        if(was_output){
            cout << endl;
            was_output = false;
        }
    }
    template <class Head, class... Tail>
    void println(const Head&head,const Tail&...tail){
        print(head);
        println(tail...);
    }

    void newline(){
        was_output = true;
        println();
    }

    template<class t>
    istream& operator>>(istream&is, vector<t>&x){
        for(auto &i:x)is >> i;
        return is;
    }

    template<class t, class u>
    istream& operator>>(istream&is, pair<t, u>&x){
        is >> x.first >> x.second;
        return is;
    }

    template<class t>
    ostream& operator<<(ostream&os, vector<t> &v){
        os << "{";
        for(t &i:v){
            os << i << ", ";
        }
        os << "}";
        return os;
    }

    template<class t = long long>
    t in(){
        t res; cin >> res; return res;
    }

    template<class t>
    vector<t> sorted(vector<t> line,function<bool(t,t)> comp=[](t a,t b){return a<b;}){
        sort(line.begin(),line.end(),comp);
        return line;
    }

    template<class t>
    vector<t> reversed(vector<t> line){
        reverse(line.begin(),line.end());
        return line;
    }
    string reversed(string str){
        reverse(str.begin(),str.end());
        return str;
    }

    long long gcd(long long a,long long b){
        while(b){
            a %= b;
            swap(a,b);
        }
        return a;
    }

    long long lcm(long long a,long long b){
        return a / gcd(a,b) * b;
    }

    class output_initializer{
    public:
        output_initializer(){
            ios::sync_with_stdio(false);
            cin.tie(nullptr);
            cout << setprecision(20);
        }
    };output_initializer OUTPUT_INITIALIZER_INSTANCE = output_initializer();
}

using namespace templates;

vector<ll> gene28(){
    vector<ll> res;
    string buf = "";
    method(rec,void,int rem){
        if(rem==0){
            res.emplace_back(stoll(buf));
            return;
        }
        buf += "2";
        rec(rem-1);
        buf.erase(buf.end()-1);
        buf += "8";
        rec(rem-1);
        buf.erase(buf.end()-1);
    };
    rep(i,1,19){
        rec(i);
    }
    sort(all(res));
    return res;
}

int func(){
    ll n = in();
    if(n==1)return -1;
    map<ll,int> m;
    vector<ll> v28 = gene28();
    method(rec,int,ll n){
        if(n==1)return 0;
        if(m.count(n))return m[n];
        int res = -INF;
        foreach(i,v28){
            if(i > n)break;
            if(n % i == 0){
                chmax(res,1+rec(n/i));
            }
        }
        return m[n] = res;
    };

    int res = rec(n);
    if(res<0)return -1;
    return res;
}


int main(){
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    println(func());
    return 0;
}
accuracy: 1
time_ms: 50
memory_kb: 9236
final_rank: 9

code_snippet: #include <iostream>
#include <iomanip>
#include <algorithm>
#include <array>
#include <cassert>
#include <map>
#include <utility>
#include <vector>

template <class InputIterator>
std::ostream& range_output(std::ostream& os_arg, InputIterator first_arg, InputIterator last_arg){ if(first_arg != last_arg){ do{ os_arg << *(first_arg++); if(first_arg == last_arg) break; os_arg << ' '; } while(true); }  return os_arg; }
template <class Tp> std::ostream& operator << (std::ostream& os_arg, const std::vector<Tp>& arr_arg){ return range_output(os_arg, arr_arg.cbegin(), arr_arg.cend()); }
template <class Tp, std::size_t Size> std::ostream& operator << (std::ostream& os_arg, const std::array<Tp, Size>& arr_arg){ return range_output(os_arg, arr_arg.cbegin(), arr_arg.cend()); }
template <class S, class T> std::ostream& operator << (std::ostream& os_arg, const std::pair<S, T>& pair_arg){ return os_arg << '(' << pair_arg.first << ", " << pair_arg.second << ')'; }

#ifndef ONLINE_JUDGE
    template <typename Head> void dump_out(Head head_arg){ std::cerr << head_arg << '\n'; }
    template <typename Head, typename... Tail>
    void dump_out(Head head_arg, Tail... tail_args){ std::cerr << head_arg << ", "; dump_out(tail_args...); }
    #define dump(...) do { std::cerr << "[in line " << __LINE__ << "] " << #__VA_ARGS__ << " : "; dump_out(__VA_ARGS__); } while(false)
#else
    #define dump(...) (void(0))
#endif

template <class S, class T> bool chmax(S& x, const T& y){ if(x < y){x = y; return true; } return false; }
template <class S, class T> bool chmin(S& x, const T& y){ if(x > y){x = y; return true; } return false; }

std::vector<long long int> S, T;

std::map<long long int, int> dp;

void dfs1(int depth = 0, long long int val = 0, long long int D = 1){
    if(val != 0) S.push_back(val);
    if(depth < 9){
        dfs1(depth + 1, val + 2 * D, 10 * D);
        dfs1(depth + 1, val + 8 * D, 10 * D);
    }
}

void dfs2(int depth = 0, long long int val = 0, long long int D = 1){
    if(val != 0) T.push_back(val);
    if(depth < 18){
        dfs2(depth + 1, val + 2 * D, 10 * D);
        dfs2(depth + 1, val + 8 * D, 10 * D);
    }
}

int solve(long long int v){
    if(v % 2 == 1) return -1;
    if(dp.find(v) != dp.end()) return dp[v];

    int res = -1;
    const auto itr = std::lower_bound(T.cbegin(), T.cend(), v);
    if(itr != T.cend() and *itr == v) res = 1;
    for(const auto s : S){
        const long long int t = v / s;
        if(t < s) break;
        if(v % s != 0) continue;
        const int r = solve(t);
        if(r == -1) continue;
        chmax(res, r + 1);
    }
    return dp[v] = res;
}



int main(void){
    std::cin.tie(nullptr); std::ios_base::sync_with_stdio(false);
    long long int n; std::cin >> n;
    dfs1(); std::sort(S.begin(), S.end());
    dfs2(); std::sort(T.begin(), T.end());
    std::cout << solve(n) << '\n';
}
accuracy: 1
time_ms: 20
memory_kb: 9368
final_rank: 5

code_snippet: #include <bits/stdc++.h>
using namespace std;

int main() {
	int64_t N;
	cin>>N;
	vector<int64_t>p;
	for(int64_t i=(1<<19)-1;i>0;i--){
		int64_t A=-1,K=1e18L,B=0;
		for(int j=18;j>=0;j--){
			if(A==-1&&!(i&(1<<j))){
				K/=10;
				continue;
			}
			if(A==-1&&(i&(1<<j))){
				A=0;
				B=j;
			}
			else if(i&(1<<j)){
				B--;
				A+=8*K;
			}
			else{
				A+=2*K;
			}
			K/=10;
		}
		if(B==0){
			continue;
		}
		p.push_back(A);
	}
	int Z=0;
	if(N==1){
		cout<<"-1"<<endl;
		return 0;
	}
	map<int64_t,int64_t> m;
	m[N]=0;
	for(int i=0;i<p.size();i++){
		for(auto x:m){
			int64_t b,c;
			tie(b,c)=x;
			while(b%p[i]==0){
				b/=p[i];
				c++;
				if(!m.count(b)){
					m[b]=c;
				}
				else{
					m[b]=max(c,m[b]);
				}
			}
		}
	}
	if(m.count(1)){
		cout<<m[1]<<endl;
	}
	else{
		cout<<"-1"<<endl;
	}
}
accuracy: 1
time_ms: 20
memory_kb: 9396
final_rank: 6

code_snippet: #include <bits/stdc++.h>
using namespace std;

int main() {
	int64_t N;
	cin>>N;
	vector<int64_t>p;
	for(int64_t i=(1<<19)-1;i>0;i--){
		int64_t A=-1,K=1e18L,B=0;
		for(int j=18;j>=0;j--){
			if(A==-1&&!(i&(1<<j))){
				K/=10;
				continue;
			}
			if(A==-1&&(i&(1<<j))){
				A=0;
				B=j;
			}
			else if(i&(1<<j)){
				B--;
				A+=8*K;
			}
			else{
				A+=2*K;
			}
			K/=10;
		}
		if(B==0){
			continue;
		}
		p.push_back(A);
	}
	int Z=0;
	if(N==1){
		cout<<"-1"<<endl;
		return 0;
	}
	for(int i=0;i<p.size();i++){
		while(N%p[i]==0){
			N/=p[i];
			Z++;
		}
	}
	if(N==1){
		cout<<Z<<endl;
	}
	else{
		cout<<"-1"<<endl;
	}
}
accuracy: 0.1346153846153846
time_ms: 20
memory_kb: 9000
final_rank: 18

code_snippet: #if 1
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <stack>
#include <array>
#include <deque>
#include <algorithm>
#include <utility>
#include <cstdint>
#include <functional>
#include <iomanip>
#include <numeric>
#include <assert.h>
#include <bitset>
#include <list>
#include <cmath>
//#include <atcoder/all>

auto& in = std::cin;
auto& out = std::cout;
#define all_range(C) std::begin(C), std::end(C)
const double PI = 3.141592653589793238462643383279502884197169399375105820974944;

std::map<uint64_t, int> makeable;
std::set<uint64_t> prim2;
void dfs(int i, uint64_t base) {
    if (i == 18) {
        return;
    }

    if (!makeable.count(base + 2)) {
        //makeable.insert({ base + 2,1 });
        prim2.insert(base + 2);
        //makeable.insert(base + 2);
        //for (auto& p : prim2) {
        //    if ((base + 2) < 10000000000000000000 / p) {
        //        makeable.insert((base + 2) * p);
        //    }
        //}
    }
    if (!makeable.count(base + 8)) {
        //makeable.insert({ base + 8,1 });
        prim2.insert(base + 8);
        //makeable.insert(base + 8);
        //for (auto& p : prim2) {
        //    if ((base + 8) < 10000000000000000000 / p) {
        //        makeable.insert((base + 8) * p);
        //    }
        //}
    }
    dfs(i + 1, (base + 2) * 10);
    dfs(i + 1, (base + 8) * 10);
}

uint64_t N;
std::map<std::pair< uint64_t, uint64_t>, int > dp;
int dfs2(uint64_t n, std::set<uint64_t>::iterator iter) {

    if (dp.count({ n, 0 })) {
        return dp[{n, 0}];
    }
    auto iter_end = ++iter;
    int res = -1;
    for (;;) {
        if (n == 1) {
            return 0;
        }
        if (*iter == 1) { return dp[{n, 0}] = res; }
        if (n % 2 == 1) { return dp[{n, 0}] = res; }
        if (n % *iter == 0) {
            auto tmp = dfs2(n / *iter, --prim2.upper_bound(n / *iter));
            if (tmp >= 0) {
                res = std::max(res, 1 + tmp);
            }
        }
        //return std::max(res, dfs2(n, std::prev(iter)));
        --iter;
    }
}

int main()
{
    using std::endl;
    in.sync_with_stdio(false);
    out.sync_with_stdio(false);
    in.tie(nullptr);
    out.tie(nullptr);
    prim2.insert(1);
    dfs(0, 0);

    in >> N;
    if (N == 1) { out << -1 << endl; return 0; }
    int res = -1;
    std::set<uint64_t>::iterator iter = --prim2.end();
    while (*iter > 1000000) {
        if (N % *iter == 0) {
            auto tmp = dfs2(N / *iter, --prim2.upper_bound(N / *iter));
            if(tmp >= 0) res = std::max(res, 1 + tmp);
            if ((N / *iter) % *iter == 0) {
                tmp = dfs2(N / *iter / *iter, --prim2.upper_bound(N / *iter / *iter));
                if (tmp >= 0) res = std::max(res, 2 + tmp);
            }
        }
        --iter;
    }
    out <<  std::max(res, dfs2(N, iter)) << endl;

    return 0;
}
#endif
accuracy: 1
time_ms: 150
memory_kb: 27776
final_rank: 13

code_snippet: #include<iostream>
#include<string>
#include<cstdio>
#include<vector>
#include<cmath>
#include<algorithm>
#include<functional>
#include<iomanip>
#include<queue>
#include<ciso646>
#include<random>
#include<map>
#include<set>
#include<bitset>
#include<stack>
#include<unordered_map>
#include<utility>
#include<cassert>
#include<complex>
#include<numeric>
#include<array>
using namespace std;

//#define int long long
typedef long long ll;

typedef unsigned long long ul;
typedef unsigned int ui;
constexpr ll mod = 1000000007;
const ll INF = mod * mod;
typedef pair<int, int>P;
#define stop char nyaa;cin>>nyaa;
#define rep(i,n) for(int i=0;i<n;i++)
#define per(i,n) for(int i=n-1;i>=0;i--)
#define Rep(i,sta,n) for(int i=sta;i<n;i++)
#define rep1(i,n) for(int i=1;i<=n;i++)
#define per1(i,n) for(int i=n;i>=1;i--)
#define Rep1(i,sta,n) for(int i=sta;i<=n;i++)
#define all(v) (v).begin(),(v).end()
typedef pair<ll, ll> LP;
typedef long double ld;
typedef pair<ld, ld> LDP;
const ld eps = 1e-12;
const ld pi = acosl(-1.0);

ll mod_pow(ll x, ll n, ll m = mod) {
	ll res = 1;
	while (n) {
		if (n & 1)res = res * x % m;
		x = x * x % m; n >>= 1;
	}
	return res;
}
struct modint {
	ll n;
	modint() :n(0) { ; }
	modint(ll m) :n(m) {
		if (n >= mod)n %= mod;
		else if (n < 0)n = (n % mod + mod) % mod;
	}
	operator int() { return n; }
};
bool operator==(modint a, modint b) { return a.n == b.n; }
modint operator+=(modint& a, modint b) { a.n += b.n; if (a.n >= mod)a.n -= mod; return a; }
modint operator-=(modint& a, modint b) { a.n -= b.n; if (a.n < 0)a.n += mod; return a; }
modint operator*=(modint& a, modint b) { a.n = ((ll)a.n * b.n) % mod; return a; }
modint operator+(modint a, modint b) { return a += b; }
modint operator-(modint a, modint b) { return a -= b; }
modint operator*(modint a, modint b) { return a *= b; }
modint operator^(modint a, ll n) {
	if (n == 0)return modint(1);
	modint res = (a * a) ^ (n / 2);
	if (n % 2)res = res * a;
	return res;
}

ll inv(ll a, ll p) {
	return (a == 1 ? 1 : (1 - p * inv(p % a, a)) / a + p);
}
modint operator/(modint a, modint b) { return a * modint(inv(b, mod)); }

const int max_n = 1 << 18;
modint fact[max_n], factinv[max_n];
void init_f() {
	fact[0] = modint(1);
	for (int i = 0; i < max_n - 1; i++) {
		fact[i + 1] = fact[i] * modint(i + 1);
	}
	factinv[max_n - 1] = modint(1) / fact[max_n - 1];
	for (int i = max_n - 2; i >= 0; i--) {
		factinv[i] = factinv[i + 1] * modint(i + 1);
	}
}
modint comb(int a, int b) {
	if (a < 0 || b < 0 || a < b)return 0;
	return fact[a] * factinv[b] * factinv[a - b];
}

void solve() {
	vector<ll> v;
	rep1(i, 18) {
		rep(j, (1 << i)) {
			ll s = 0;
			ll t = 1;
			rep(k, i) {
				if (j & (1 << k)) {
					s += t * 2;
				}
				else {
					s += t * 8;
				}
				t *= 10;
			}
			v.push_back(s);
		}
	}
	sort(all(v), greater<ll>());
	ll n; cin >> n;
	if (n == 1) {
		cout << -1 << "\n"; return;
	}
	map<ll, int> mp;
	map<ll, int> nex;
	mp[n] = 0;
	rep(i, v.size()) {
		nex.clear();
		nex = mp;
		for (LP p : mp) {
			if (p.first % v[i] == 0) {
				ll cop = p.first;
				int cop2 = p.second;
				while (cop % v[i] == 0) {
					cop /= v[i]; cop2++;
					nex[cop] = max(nex[cop], cop2);
				}
			}
		}
		mp = nex;
	}
	if (mp.find(1) == mp.end())cout << -1 << "\n";
	else cout << mp[1] << "\n";
}

signed main() {
	ios::sync_with_stdio(false);
	cin.tie(0);
	//cout << fixed << setprecision(15);
	//init_f();
	//init();
	//expr();
	//int t; cin >> t; rep(i, t)
		solve();
	return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 13404
final_rank: 10

code_snippet: #include <bits/stdc++.h>

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()
#define HHH(x) cerr << "L" << __LINE__ << ": " << #x << " = " << (x) << endl

template <typename T> T &chmin(T &a, const T &b) { return a = std::min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = std::max(a, b); }

using ll = long long;
using ld = long double;

using namespace std;

const int INF = 1e9;

vector<ll> goods;

map<pair<ll,int>,int> memo;

int dfs(ll n, int i) {
  // cout << n << " " << i << endl;
  if (n == 1) return 0;
  if (i == -1) return -INF;
  auto p = make_pair(n, i);
  if (memo.count(p)) return memo[p];
  int res = -INF;
  if (n % goods[i] == 0) chmax(res, dfs(n / goods[i], i) + 1);
  chmax(res, dfs(n, i - 1));
  return memo[p] = res;
}

int main() {
  for (int n_digit = 1; n_digit <= 18; ++n_digit) {
    REP(bit, 1 << n_digit) {
      ll val = 0;
      for (int i = 0; i < n_digit; ++i) {
        if ((bit >> i) & 1) val = val * 10 + 8;
        else val = val * 10 + 2;
      }
      goods.push_back(val);
    }
  }
  ll n;
  cin >> n;
  vector<ll> new_goods;
  for (ll x: goods) {
    if (n % x == 0) new_goods.push_back(x);
  }
  goods = new_goods;
  // REP(i,goods.size()) cout << goods[i] << endl;
  int res = dfs(n, goods.size() - 1);
  if (res <= 0) cout << -1 << endl;
  else cout << res << endl;
  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 9148
final_rank: 4

code_snippet: #include<bits/stdc++.h>
using namespace std;
typedef long long ll;


signed main(){
    ios::sync_with_stdio(false);
	cin.tie(0);
    cout << fixed << setprecision(20);

    ll n;
    cin>>n;
    vector<ll> v;
    queue<ll> q;
    q.push(0);
    while(q.size()){
        ll now=q.front();
        if(now>n) break;
        q.pop();
        ll p=now*10 + 2;
        if(n%p==0) v.push_back(p);
        q.push(p);
        p = now*10 + 8;
        if(n%p==0) v.push_back(p);
        q.push(p);
    }
    // cerr << v.size() << endl;
    // for(auto i:v){
    //     cerr << i << endl;
    // }
    map<ll,ll> mp;
    mp[n]=0;
    mp[1] = -1;
    for(int i=0;i<v.size();i++){
        ll now=v[i];
        for(auto j:mp){
            ll a = j.first;
            while(a%now == 0){
                mp[a/now] = max(mp[a/now],mp[a]+1);
                a/=now;
            }
        }        
    }
    cout << mp[1] << endl;
    
}
accuracy: 1
time_ms: 10
memory_kb: 7000
final_rank: 3

code_snippet: #include<bits/stdc++.h>
using namespace std;
using LL = long long;

vector<LL> st;
int ans = -1;

void dfs(LL N, int k, int use){
    if(k >= st.size()) return;
    LL now = st[k];
    if(N == 1){
        ans = max(ans, use);
        return;
    }
    if(now > N) return;
    if(N % now == 0){
        dfs(N/now, k, use+1);
    }
    dfs(N, k+1, use);
}

int main(){
    LL N;
    cin>>N;
    if(N == 1){
        cout << -1 << endl;
        return 0;
    }
    set<LL> M;
    for(int i=1;i<=18;i++)
    {
        for(int j=0;j<(1<<i);j++)
        {
            LL now=0;
            for(int k=0;k<i;k++) now = now*10 + (j>>k&1 ? 8:2);
            if(__builtin_popcountll(j) == i) continue;
            M.insert(now);
        }
    }
    for(auto &&i : M)if(N % i == 0) st.push_back(i);
    dfs(N, 0, 0);
    cout << ans << endl;
}
accuracy: 1
time_ms: 160
memory_kb: 27724
final_rank: 14

code_snippet: #include<iostream>
#include<vector>
using namespace std;
long N;
vector<long>A;
int dfs(long n,int i)
{
	if(i==A.size())
	{
		return n==1?0:-1;
	}
	int ans=dfs(n,i+1);
	int t=0;
	while(n%A[i]==0)
	{
		t++;n/=A[i];
		int now=dfs(n,i+1);
		if(now>=0)ans=max(ans,now+t);
	}
	return ans;
}
main()
{
    cin>>N;
	if(N==1)
	{
		cout<<-1<<endl;
		return 0;
	}
    for(int i=1;i<=18;i++)
    {
        for(int j=0;j<1<<i;j++)
        {
            long now=0;
            for(int k=0;k<i;k++)now=now*10+(j>>k&1?8:2);
			if(N%now==0)A.push_back(now);
		}
	}
	cout<<dfs(N,0)<<endl;
}
accuracy: 1
time_ms: 10
memory_kb: 3156
final_rank: 1

code_snippet: #include<iostream>
#include<vector>
using namespace std;
long N;
vector<int>A;
int dfs(long n,int i)
{
	if(i==A.size())
	{
		return n==1?0:-1;
	}
	int ans=dfs(n,i+1);
	int t=0;
	while(n%A[i]==0)
	{
		t++;n/=A[i];
		int now=dfs(n,i+1);
		if(now>=0)ans=max(ans,now+t);
	}
	return ans;
}
main()
{
    cin>>N;
	if(N==1)
	{
		cout<<-1<<endl;
		return 0;
	}
    for(int i=1;i<=18;i++)
    {
        for(int j=0;j<1<<i;j++)
        {
            long now=0;
            for(int k=0;k<i;k++)now=now*10+(j>>k&1?8:2);
			if(N%now==0)A.push_back(now);
		}
	}
	cout<<dfs(N,0)<<endl;
}
accuracy: 0.17307692307692307
time_ms: 10
memory_kb: 3156
final_rank: 16

code_snippet: #include<bits/stdc++.h>
using namespace std;
long N;
main()
{
    cin>>N;
    if(N == 1){
        cout << -1 << endl;
        return 0;
    }
    set<long> M;
    for(int i=1;i<=18;i++)
    {
        for(int j=0;j<1<<i;j++)
        {
            long now=0;
            for(int k=0;k<i;k++)now=now*10+(j>>k&1?8:2);
            if(__builtin_popcountll(j) == i) continue;
            M.insert(now);
        }
    }
    int ans = 0;
    for(auto i=M.rbegin(); i!=M.rend(); i++){
        long x = *i;
        while(N > 1 && N%x == 0){
            N /= x;
            ans++;
            // cout << x << endl;
        }
    }
    if(N > 1) ans = -1;
    // cout << N << endl;

    cout << ans << endl;
}
accuracy: 0.1346153846153846
time_ms: 140
memory_kb: 27672
final_rank: 19

code_snippet: #include <bits/stdc++.h>

#define ALL(a) (a).begin(), (a).end()
#define llong long long

using namespace std;
map<llong, llong> m;
vector<llong> l;


llong dfs(llong n){

	if(n == 1)return 0;
	if(m.find(n) != m.end())return m[n];
	m[n] = -1;
	for(auto e : l){
		if(e > n)break;
		if(n%e == 0){
			llong val = dfs(n/e);
			if(val < 0) continue;
			m[n] = max(m[n], 1+val);
	
		}
	}
	if(m[n] == 0)return -1;
	return m[n];
}

/*
llong dfs(llong n){
	if(n == 1)return 0;
	if(m.find(n) != m.end())return m[n];
	llong ret = 
	for(auto e : l){
		if(e > n)break;
		if(n%e == 0)m[n] = max(m[n], 1+dfs(n/e));
	}
	if(m[n] == 0)return -1;
	return m[n];
}
*/
signed main(){
	llong n; cin >> n;
	for(llong i = 1; i < 19; i++){
		for(llong j = 0; j < (1 << i); j++){
			llong tmp = 0;
			llong t = 1;
			for(llong k = 0; k < i; k++){
				if(j & (1 << k)){
					tmp += 8*t;
				}
				else{
					tmp += 2*t;
				}
				t *= 10;
			}
			l.push_back(tmp);
		}
	}
//	for(int i = 0; i < 20; i++)cerr << l[i] << " ";
//	cerr << endl;
	if(n == 1){
		cout << -1 << endl;
		return 0;
	}
	m[1] = 0;
	cout << dfs(n) << endl;


	return 0;
}
accuracy: 1
time_ms: 40
memory_kb: 7076
final_rank: 7

code_snippet: #include <cstdio>
#include <cstdint>
#include <vector>
#include <map>

int inf = 1e9;

std::map<intmax_t, int> memo;

int dfs(intmax_t n, const std::vector<intmax_t>& div) {
  if (n == 1) return 0;
  if (memo.find(n) != memo.end()) return memo.at(n);
  int res = 0;
  for (auto x: div) {
    if (x > n) break;
    if (n % x != 0) continue;
    int cur = dfs(n / x, div) + 1;
    res = std::max(cur, res);
  }
  if (res == 0) res = -1;
  memo[n] = res;
  return res;
}

int main() {
  intmax_t n;
  scanf("%jd", &n);

  if (n == 1)
    return puts("-1"), 0;

  std::vector<intmax_t> div{2, 8};
  auto tmp = div;
  intmax_t bound = 1e18;
  while (true) {
    bool updated = false;
    std::vector<intmax_t> tmp0;
    for (auto x: tmp) {
      if (10*x > bound) continue;
      tmp0.push_back(10*x+2);
      tmp0.push_back(10*x+8);
      div.push_back(10*x+2);
      div.push_back(10*x+8);
      updated = true;
    }
    tmp = std::move(tmp0);
    if (!updated) break;
  }

  int res = dfs(n, div);
  if (!res) res = -1;
  printf("%d\n", res);
}
accuracy: 1
time_ms: 20
memory_kb: 12524
final_rank: 8

code_snippet: #include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
const int inf = (1 << 30);
vector<long long> d;
int solve(int pos, long long cur) {
	if (pos == d.size()) return -inf;
	if (cur == 1) return 0;
	int ret = solve(pos + 1, cur);
	if (cur % d[pos] == 0) ret = max(ret, solve(pos, cur / d[pos]) + 1);
	return ret;
}
int main() {
	long long n;
	cin >> n;
	for (int i = 1; i <= 18; ++i) {
		for (int j = 0; j < (1 << i) - 1; ++j) {
			long long cur = 0;
			for (int k = 0; k < i; ++k) {
				cur = cur * 10 + (((j >> k) & 1) ? 8 : 2);
			}
			if (n % cur == 0) {
				d.push_back(cur);
			}
		}
	}
	int ans = solve(0, n);
	cout << (ans >= 0 ? ans : -1) << endl;
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 3164
final_rank: 2

code_snippet: #include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
const int inf = (1 << 30);
vector<long long> d;
int solve(int pos, long long cur) {
	if (pos == d.size()) return -inf;
	if (cur == 1) return 0;
	int ret = solve(pos + 1, cur);
	if (cur % d[pos] == 0) ret = max(ret, solve(pos, cur / d[pos]) + 1);
	return ret;
}
int main() {
	long long n;
	cin >> n;
	for (int i = 1; i <= 18; ++i) {
		for (int j = 0; j < (1 << i) - 1; ++j) {
			long long cur = 0;
			for (int k = 0; k < i; ++k) {
				cur = cur * 10 + (((j >> k) & 1) ? 8 : 2);
			}
			if (n % cur == 0) {
				d.push_back(cur);
			}
		}
	}
	int ans = solve(0, n);
	cout << (ans != -inf ? ans : -1) << endl;
	return 0;
}
accuracy: 0.057692307692307696
time_ms: 10
memory_kb: 3160
final_rank: 20

code_snippet: #include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll> P;

#define fi first
#define se second
#define repl(i,a,b) for(ll i=(ll)(a);i<(ll)(b);i++)
#define rep(i,n) repl(i,0,n)
#define all(x) (x).begin(),(x).end()
#define dbg(x) cout<<#x"="<<x<<endl
#define mmax(x,y) (x>y?x:y)
#define mmin(x,y) (x<y?x:y)
#define maxch(x,y) x=mmax(x,y)
#define minch(x,y) x=mmin(x,y)
#define uni(x) x.erase(unique(all(x)),x.end())
#define exist(x,y) (find(all(x),y)!=x.end())
#define bcnt __builtin_popcount

#define INF 1e16
#define mod 1000000007
ll n;
vector<ll> vs;
map<ll,ll> dp[2];

int main(){

  repl(i,1,19){
    rep(S,1<<i){
      string tmp;
      rep(d,i){
        if((S>>d)&1)tmp+="8";
        else tmp+="2";
      }
      vs.push_back(stoll(tmp));
    }
  }
  reverse(all(vs));
  cin>>n;
  if(n==1){
    cout<<-1<<endl;
    return 0;
  }
  ll m=vs.size();

  int crt=0,nxt=1;
  dp[crt][n]=0;
  rep(i,m){
    ll v=vs[i];
    dp[nxt].clear();
    for(auto it : dp[crt]){
      if(it.fi%v==0){
        ll tmp=it.fi,cnt=1;
        while(tmp%v==0){
          maxch(dp[nxt][tmp/v],it.se+cnt);
          tmp/=v;
          cnt++;
        }
      }
      maxch(dp[nxt][it.fi],it.se);
    }
    swap(crt,nxt);
  }
  if(dp[crt].find(1)==dp[crt].end())cout<<-1<<endl;
  else cout<<dp[crt][1]<<endl;
  return 0;
}
accuracy: 1
time_ms: 200
memory_kb: 6948
final_rank: 12

code_snippet: #include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define rep(i,n) for(int (i)=0;(i)<(int)(n);++(i))
#define all(x) (x).begin(),(x).end()
#define pb push_back
#define fi first
#define se second
#define dbg(x) cout<<#x" = "<<((x))<<endl
template<class T,class U> ostream& operator<<(ostream& o, const pair<T,U> &p){o<<"("<<p.fi<<","<<p.se<<")";return o;}
template<class T> ostream& operator<<(ostream& o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}

ll make(int d, int mask){
    string s = "";
    rep(i,d){
        if(mask>>i&1) s += '8';
        else s += '2';
    }
    return atoll(s.c_str());
}

const int INF = 19191919;
vector<ll> v;
int V;

const int N = 1<<10;
map<ll,int> dp[N];
int dfs(int d, ll n){
    if(d==V){
        if(n==1) return 0;
        return -INF;
    }
    if(dp[d].count(n)) return dp[d][n];

    int ret = -INF;
    if(n%v[d]==0) ret = max(ret, dfs(d, n/v[d])+1);
    ret = max(ret, dfs(d+1, n));

    return dp[d][n] = ret;
}

int main(){
    ll n;
    cin >>n;
    if(n==1){
        cout << -1 << endl;
        return 0;
    }

    for(int i=1; i<=9; ++i){
        rep(mask,1<<i) v.pb(make(i,mask));
    }
    V = v.size();

    int ans = -1;

    ans = max(ans, dfs(0,n));
    for(int i=10; i<=18; ++i){
        rep(mask,1<<i){
            ll val = make(i,mask);
            if(n%val==0){
                ans = max(ans, dfs(0, n/val)+1);
            }
        }
    }

    cout << ans << endl;
    return 0;
}
accuracy: 1
time_ms: 340
memory_kb: 78760
final_rank: 15

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define _MACRO(_1, _2, _3, NAME, ...) NAME
#define _repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define _rep(i,n) _repl(i,0,n)
#define rep(...) _MACRO(__VA_ARGS__, _repl, _rep)(__VA_ARGS__)
#define pb push_back
#define all(x) begin(x),end(x)
#define uniq(x) sort(all(x)),(x).erase(unique(all(x)),end(x))
#ifdef LOCAL
#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
void _dbg(string){cerr<<endl;}
template<class H,class... T> void _dbg(string s,H h,T... t){int l=s.find(',');cerr<<s.substr(0,l)<<" = "<<h<<", ";_dbg(s.substr(l+1),t...);}
template<class T,class U> ostream& operator<<(ostream &o, const pair<T,U> &p){o<<"("<<p.first<<","<<p.second<<")";return o;}
template<class T> ostream& operator<<(ostream &o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}
#else
#define dbg(...) {}
#endif

#define long long long // for codeforces

vector<long> goods = {2,8};
map<long,int> memo;

int dfs(long val){
  if (memo.count(val)) return memo[val];
  int ans = -1;
  for(auto v : goods){
    if(v > val) break;
    if(v == val) {
      ans = max(ans, 1);
    }
    if(val%v==0){
      int r = dfs(val/v);
      if(r > 0) ans = max(ans, r + 1);
    }
  }
  return memo[val] = ans;
}

bool isgood(long x){
  while(x>0){
    if(x%10 !=2 && x%10 != 8) return false;
    x /= 10;
  }
  return true;
}

int main(){
  rep(_,9){
    int s = goods.size();
    rep(i,s) if (goods[i] * 10 <= 1000000000){
      goods.pb(goods[i]*10 + 2);
      goods.pb(goods[i]*10 + 8);
    }
  }

  long v;
  cin>>v;

  int ans = dfs(v);
  if(isgood(v)) ans = max(ans, 1);

  cout << ans << endl;

  return 0;
}
accuracy: 1
time_ms: 110
memory_kb: 3348
final_rank: 11

code_snippet: #include <bits/stdc++.h>
using namespace std;
#define _MACRO(_1, _2, _3, NAME, ...) NAME
#define _repl(i,a,b) for(int i=(int)(a);i<(int)(b);i++)
#define _rep(i,n) _repl(i,0,n)
#define rep(...) _MACRO(__VA_ARGS__, _repl, _rep)(__VA_ARGS__)
#define pb push_back
#define all(x) begin(x),end(x)
#define uniq(x) sort(all(x)),(x).erase(unique(all(x)),end(x))
#ifdef LOCAL
#define dbg(...) _dbg(#__VA_ARGS__, __VA_ARGS__)
void _dbg(string){cerr<<endl;}
template<class H,class... T> void _dbg(string s,H h,T... t){int l=s.find(',');cerr<<s.substr(0,l)<<" = "<<h<<", ";_dbg(s.substr(l+1),t...);}
template<class T,class U> ostream& operator<<(ostream &o, const pair<T,U> &p){o<<"("<<p.first<<","<<p.second<<")";return o;}
template<class T> ostream& operator<<(ostream &o, const vector<T> &v){o<<"[";for(T t:v){o<<t<<",";}o<<"]";return o;}
#else
#define dbg(...) {}
#endif

#define long long long // for codeforces

vector<long> goods = {2,8};
map<long,int> memo;

int dfs(long val){
  if (memo.count(val)) return memo[val];
  int ans = -1;
  for(auto v : goods){
    if(v > val) break;
    if(v == val) {
      ans = max(ans, 1);
    }
    if(val%v==0){
      int r = dfs(val/v);
      if(r > 0) ans = max(ans, r + 1);
    }
  }
  return memo[val] = ans;
}

int main(){
  rep(_,9){
    int s = goods.size();
    rep(i,s) if (goods[i] * 10 <= 1000000000){
      goods.pb(goods[i]*10 + 2);
      goods.pb(goods[i]*10 + 8);
    }
  }

  long v;
  cin>>v;

  cout << dfs(v) << endl;

  return 0;
}
accuracy: 0.17307692307692307
time_ms: 70
memory_kb: 3344
final_rank: 17