Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _USE_MATH_DEFINES
- #include <iostream>
- #include <conio.h>
- #include <string>
- #include <set>
- #include <map>
- #include <algorithm>
- #include <string>
- #include <math.h>
- #include <vector>
- #include <cmath>
- #include <sstream>
- #include <queue>
- using namespace std;
- string lis(vector<string>s)
- {
- string last;
- last = "";
- for (auto i = s.begin(); i != s.end(); i++)
- {
- last += *i;
- }
- return last;
- }
- string clean(string b)
- {
- for (int i = 0; i <=b.size()-1; i++)
- {
- if (b[i] != '0')
- {
- b = b.substr(i, b.size() - i);
- break;
- }
- }
- return b;
- }
- string d(char b, char a,string s, int num)
- {
- int prom;
- prom = (b - '0') - (a - '0');
- if (prom >= 0)
- s[num] = prom+'0';
- else
- {
- if (num != 0)
- {
- if (num == 1 && s[0] == '-')
- {
- s = "-" + to_string((s[num] - '0') - prom) + s.substr(2, s.size() - num-1);
- }
- else
- {
- s[num] = 10 + prom + '0';
- s = d(s[num - 1], 1 + '0', s, num - 1);
- }
- }
- else
- {
- s = "-" + to_string(abs((s[num] - '0') + prom)) + s.substr(1, s.size() - num-1);
- }
- }
- return s;
- }
- string dec(string b, string a)
- {
- int j;
- j = a.size() - 1;
- for (int i = b.size() - 1; i >= 0; i--)
- {
- if (j<0)
- break;
- b = d(b[i], a[j], b, i);
- j--;
- }
- return b;
- }
- bool gcd05(string a, string b, long long k) {
- while (a != b) {
- if (atoi(dec(b,a).c_str()) < 0) {
- string tmp = a;
- a = b;
- b = tmp;
- }
- b = dec(b, a);
- b = clean(b);
- }
- return atoi(dec(a,to_string(k)).c_str())>=0;
- }
- int main()
- {
- long long N, M, K, m;
- string last, proms;
- vector <string> s;
- last = "";
- long long prom;
- cin >> N >> M >> K;
- m = M;
- for (int i = 1; i <= N; i++)
- {
- cin >> prom;
- s.push_back(to_string(prom));
- }
- sort(s.begin(), s.end());
- last = lis(s);
- while (next_permutation(s.begin(), s.end()))
- {
- proms = lis(s);
- if (gcd05(last, proms, K))
- M--;
- if (M == 0)
- break;
- }
- if (m == M)
- {
- cout << -1;
- }
- else
- for (auto i = s.begin(); i != s.end(); i++)
- {
- cout << *i << " ";
- }
- _getch();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment