Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #include <ext/pb_ds/assoc_container.hpp>
- #include <ext/pb_ds/tree_policy.hpp>
- using namespace std;
- using namespace __gnu_pbds;
- typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;
- #define scan(x) do{while((x=getchar())<'0'); for(x-='0'; '0'<=(_=getchar()); x=(x<<3)+(x<<1)+_-'0');}while(0)
- char _;
- #define complete_unique(a) a.erase(unique(a.begin(),a.end()),a.end())
- #define all(a) a.begin(),a.end()
- #define println printf("\n");
- #define readln(x) getline(cin,x);
- #define pb push_back
- #define endl "\n"
- #define INT_INF 0x3f3f3f3f
- #define LL_INF 0x3f3f3f3f3f3f3f3f
- #define MOD 1000000007
- #define MOD2 1494318097
- #define SEED 131
- #define mp make_pair
- #define fastio cin.tie(0); cin.sync_with_stdio(0);
- #define MAXN 1005
- typedef unsigned long long ull;
- typedef long long ll;
- typedef long double ld;
- typedef unordered_map<int,int> umii;
- typedef pair<int,int> pii;
- typedef pair<double,double> pdd;
- typedef pair<ll,ll> pll;
- typedef pair<int,pii> triple;
- typedef int8_t byte;
- mt19937 g1(chrono::steady_clock::now().time_since_epoch().count());
- int randint(int a, int b){return uniform_int_distribution<int>(a, b)(g1);}
- ll randlong(ll a,ll b){return uniform_int_distribution<long long>(a, b)(g1);}
- ll gcd(ll a, ll b){return b == 0 ? a : gcd(b, a % b);}
- ll lcm(ll a, ll b){return a*b/gcd(a,b);}
- ll fpow(ll b, ll exp, ll mod){if(exp == 0) return 1;ll t = fpow(b,exp/2,mod);if(exp&1) return t*t%mod*b%mod;return t*t%mod;}
- ll divmod(ll i, ll j, ll mod){i%=mod,j%=mod;return i*fpow(j,mod-2,mod)%mod;}
- int len;
- ll K,arr[MAXN],mn=LLONG_MAX;
- vector<int> cnt[MAXN],base,psa[MAXN];
- vector<pair<ll,int>> def;
- map<ll,ll> dp[MAXN],rev;
- map<ll,bool> par[MAXN];
- inline ll getHash(vector<int> &v){
- ll hsh = 0;
- for(int check:v)
- hsh = hsh*131+check;
- return hsh;
- }
- inline vector<pair<ll,int>> primef(ll n){
- vector<pair<ll,int>> ret;
- map<ll,int> cnt;
- for(ll i=2; i<=sqrt(n); i++){
- while(n%i == 0){
- cnt[i]++;
- n/=i;
- }
- }
- if(n > 1) cnt[n]++;
- for(auto check:cnt)
- ret.pb(check);
- return ret;
- }
- ll solve(int idx, vector<int> &v){
- ll hsh = getHash(v);
- if(dp[idx].count(hsh)) return dp[idx][hsh];
- if(idx == 0){
- bool good = true;
- for(auto &check:v){
- if(check){
- good = false;
- break;
- }
- }
- if(good) return dp[idx][hsh] = 0;
- return dp[idx][hsh] = LL_INF;
- }
- for(int i=0; i<v.size(); i++)
- if(psa[idx][i] < v[i])
- return dp[idx][hsh] = LL_INF;
- ll f = solve(idx-1,v);
- par[idx][hsh] = false;
- vector<int> used(def.size(),0);
- for(int i=0; i<v.size(); i++){
- int take = min(cnt[idx][i],v[i]);
- used[i] = take;
- v[i]-=take;
- }
- if(solve(idx-1,v)+arr[idx] < f){
- f = solve(idx-1,v)+arr[idx];
- par[idx][hsh] = true;
- }
- for(int i=0; i<used.size(); i++)
- v[i]+=used[i];
- return dp[idx][hsh] = f;
- }
- int main(){
- scanf("%d %lld",&len,&K);
- for(int i=1; i<=len; i++){
- scanf(" %lld",&arr[i]);
- mn = min(mn,arr[i]);
- }
- if(K == 1) return 0*printf("%lld\n",mn);
- def = primef(K);
- sort(all(def));
- for(int i=0; i<def.size(); i++){
- rev[def[i].first] = i;
- base.pb(def[i].second);
- }
- psa[0].resize(base.size());
- for(auto &check:psa[0]) check = 0;
- for(int i=1; i<=len; i++){
- vector<pair<ll,int>> tmp = primef(arr[i]);
- vector<int> nxt((int)def.size(),0);
- for(pair<ll,int> check:tmp)
- if(K%check.first == 0)
- nxt[rev[check.first]] = check.second;
- cnt[i] = nxt;
- for(int k=0; k<base.size(); k++)
- psa[i].pb(psa[i-1][k]+cnt[i][k]);
- }
- ll res = solve(len,base);
- if(res == LL_INF) printf("-1\n");
- else{
- vector<int> ret;
- ll hsh = getHash(base);
- for(int i=len; i>=1; i--){
- if(par[i][hsh]){
- ret.pb(i);
- for(int k=0; k<base.size(); k++)
- base[k] = max(0,base[k]-cnt[i][k]);
- hsh = getHash(base);
- }
- }
- printf("%d\n",(int)ret.size());
- for(int check:ret)
- printf("%d ",check);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement