Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma comment(linker,"/STACK:300000000")
- #define _CRT_SECURE_NO_WARNINGS
- #pragma warning(disable:4800)
- #include <iostream>
- #include <vector>
- #include <string>
- #include <algorithm>
- #include <cmath>
- #include <map>
- #include <set>
- #include <iomanip>
- #include <memory.h>
- #include <cstdio>
- #include <sstream>
- #include <deque>
- #include <bitset>
- #include <numeric>
- #include <ctime>
- #include <queue>
- using namespace std;
- #define show(x) cout << #x << " = " << (x) << endl;
- #define fori(i,n) for(int i = 0; i < (n); i++)
- #define forab(i,a,b) for(int i = (a); i <= (b); i++)
- #define sz(v) int((v).size())
- #define all(v) (v).begin(),(v).end()
- const double pi = 3.1415926535897932384626433832795;
- template<class T> T abs(const T &a) { return a >= 0 ? a : -a; };
- template<class T> T sqr(const T &x) { return x * x; }
- typedef pair<int,int> ii;
- typedef long long ll;
- ///////////////////////////////////////
- int n, k;
- ll m;
- vector<int> v;
- vector<int> ans;
- ll dp[16][1<<16];
- int gcd(int x, int y)
- {
- return y ? gcd(y,x%y) : x;
- }
- ll f(int prevInd, int mask)
- {
- if(mask == 0)
- return 1;
- ll &res = dp[prevInd][mask];
- if(res == -1)
- {
- res = 0;
- fori(i,n)
- if(mask&(1<<i))
- if(gcd(v[prevInd],v[i]) >= k)
- res += f(i,mask &~ (1<<i));
- }
- return res;
- }
- int main()
- {
- #ifndef ONLINE_JUDGE
- freopen("input.txt","r",stdin);
- freopen("output.txt","w",stdout);
- #endif
- cin >> n >> m >> k;
- v.resize(n);
- fori(i,n)
- cin >> v[i];
- memset(dp,-1,sizeof(dp));
- sort(all(v));
- ll t = 0;
- int mask = (1<<n)-1;
- fori(_,n)
- {
- bool found = false;
- fori(i,n)
- if(ans.empty() || gcd(ans.back(),v[i]) >= k)
- {
- if(mask&(1<<i))
- {
- mask &= ~(1<<i);
- ll test = f(i,mask);
- if(t+test >= m)
- {
- found = true;
- ans.push_back(v[i]);
- break;
- }
- else
- {
- t += test;
- mask |= (1<<i);
- }
- }
- }
- if(!found)
- break;
- }
- if(sz(ans) == n)
- fori(i,n)
- cout << ans[i] << ' ';
- else
- cout << -1;
- }
Advertisement
Add Comment
Please, Sign In to add comment