Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #include <ext/pb_ds/assoc_container.hpp>
  3. #include <ext/pb_ds/tree_policy.hpp>
  4.  
  5. using namespace std;
  6. using namespace __gnu_pbds;
  7. typedef tree<int, null_type, less<int>, rb_tree_tag,tree_order_statistics_node_update> ordered_set;
  8.  
  9. #define scan(x) do{while((x=getchar())<'0'); for(x-='0'; '0'<=(_=getchar()); x=(x<<3)+(x<<1)+_-'0');}while(0)
  10. char _;
  11. #define complete_unique(a) a.erase(unique(a.begin(),a.end()),a.end())
  12. #define all(a) a.begin(),a.end()
  13. #define println printf("\n");
  14. #define readln(x) getline(cin,x);
  15. #define pb push_back
  16. #define endl "\n"
  17. #define INT_INF 0x3f3f3f3f
  18. #define LL_INF 0x3f3f3f3f3f3f3f3f
  19. #define MOD 1000000007
  20. #define MOD2 1494318097
  21. #define SEED 131
  22. #define mp make_pair
  23. #define fastio cin.tie(0); cin.sync_with_stdio(0);
  24.  
  25. #define MAXN 1005
  26.  
  27. typedef unsigned long long ull;
  28. typedef long long ll;
  29. typedef long double ld;
  30. typedef unordered_map<int,int> umii;
  31. typedef pair<int,int> pii;
  32. typedef pair<double,double> pdd;
  33. typedef pair<ll,ll> pll;
  34. typedef pair<int,pii> triple;
  35. typedef int8_t byte;
  36.  
  37. mt19937 g1(chrono::steady_clock::now().time_since_epoch().count());
  38.  
  39. int randint(int a, int b){return uniform_int_distribution<int>(a, b)(g1);}
  40. ll randlong(ll a,ll b){return uniform_int_distribution<long long>(a, b)(g1);}
  41.  
  42. ll gcd(ll a, ll b){return b == 0 ? a : gcd(b, a % b);}
  43. ll lcm(ll a, ll b){return a*b/gcd(a,b);}
  44. 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;}
  45. ll divmod(ll i, ll j, ll mod){i%=mod,j%=mod;return i*fpow(j,mod-2,mod)%mod;}
  46.  
  47. int len;
  48. ll K,arr[MAXN],mn=LLONG_MAX;
  49. vector<int> cnt[MAXN],base,psa[MAXN];
  50. vector<pair<ll,int>> def;
  51. map<ll,ll> dp[MAXN],rev;
  52. map<ll,bool> par[MAXN];
  53.  
  54. inline ll getHash(vector<int> &v){
  55. ll hsh = 0;
  56. for(int check:v)
  57. hsh = hsh*131+check;
  58. return hsh;
  59. }
  60.  
  61. inline vector<pair<ll,int>> primef(ll n){
  62. vector<pair<ll,int>> ret;
  63. map<ll,int> cnt;
  64. for(ll i=2; i<=sqrt(n); i++){
  65. while(n%i == 0){
  66. cnt[i]++;
  67. n/=i;
  68. }
  69. }
  70. if(n > 1) cnt[n]++;
  71. for(auto check:cnt)
  72. ret.pb(check);
  73. return ret;
  74. }
  75.  
  76. ll solve(int idx, vector<int> &v){
  77. ll hsh = getHash(v);
  78. if(dp[idx].count(hsh)) return dp[idx][hsh];
  79. if(idx == 0){
  80. bool good = true;
  81. for(auto &check:v){
  82. if(check){
  83. good = false;
  84. break;
  85. }
  86. }
  87. if(good) return dp[idx][hsh] = 0;
  88. return dp[idx][hsh] = LL_INF;
  89. }
  90. for(int i=0; i<v.size(); i++)
  91. if(psa[idx][i] < v[i])
  92. return dp[idx][hsh] = LL_INF;
  93. ll f = solve(idx-1,v);
  94. par[idx][hsh] = false;
  95. vector<int> used(def.size(),0);
  96. for(int i=0; i<v.size(); i++){
  97. int take = min(cnt[idx][i],v[i]);
  98. used[i] = take;
  99. v[i]-=take;
  100. }
  101. if(solve(idx-1,v)+arr[idx] < f){
  102. f = solve(idx-1,v)+arr[idx];
  103. par[idx][hsh] = true;
  104. }
  105. for(int i=0; i<used.size(); i++)
  106. v[i]+=used[i];
  107. return dp[idx][hsh] = f;
  108. }
  109.  
  110. int main(){
  111. scanf("%d %lld",&len,&K);
  112. for(int i=1; i<=len; i++){
  113. scanf(" %lld",&arr[i]);
  114. mn = min(mn,arr[i]);
  115. }
  116. if(K == 1) return 0*printf("%lld\n",mn);
  117. def = primef(K);
  118. sort(all(def));
  119. for(int i=0; i<def.size(); i++){
  120. rev[def[i].first] = i;
  121. base.pb(def[i].second);
  122. }
  123. psa[0].resize(base.size());
  124. for(auto &check:psa[0]) check = 0;
  125. for(int i=1; i<=len; i++){
  126. vector<pair<ll,int>> tmp = primef(arr[i]);
  127. vector<int> nxt((int)def.size(),0);
  128. for(pair<ll,int> check:tmp)
  129. if(K%check.first == 0)
  130. nxt[rev[check.first]] = check.second;
  131. cnt[i] = nxt;
  132. for(int k=0; k<base.size(); k++)
  133. psa[i].pb(psa[i-1][k]+cnt[i][k]);
  134. }
  135. ll res = solve(len,base);
  136. if(res == LL_INF) printf("-1\n");
  137. else{
  138. vector<int> ret;
  139. ll hsh = getHash(base);
  140. for(int i=len; i>=1; i--){
  141. if(par[i][hsh]){
  142. ret.pb(i);
  143. for(int k=0; k<base.size(); k++)
  144. base[k] = max(0,base[k]-cnt[i][k]);
  145. hsh = getHash(base);
  146. }
  147. }
  148. printf("%d\n",(int)ret.size());
  149. for(int check:ret)
  150. printf("%d ",check);
  151. }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement