Quipex icon

Untitled

Quipex | PRO | 04/15/17 04:03:53 PM UTC | 0 ⭐ | 371 👁️ | Never ⏰ | []
C++ |

2.09 KB

|

None

|

0 👍

/

0 👎

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <cmath>
#include <cstring>
#include <string>
#include <iterator>
#include <iomanip>
using namespace std;
 
#define sqr(x) ((x)*(x))
#define cbr(x) ((x)*(x)*(x))
#define rep(c, i, n) for((i)=(c); (i)<(n); (i)++)
struct pt { long long x, y; };
double vecmul(pt a, pt b) { /*Векторное умножение А*В*/ return (a.x*b.y - b.x*a.y); } //скалярное умножение a*b = |a|*|b|*cos(a,b)
 
double dist(double ax, double ay, double bx, double by) { return sqrt(sqr(ax - bx) + sqr(ay - by)); }
 
//DBG
template<typename T, typename T2>void printarr(T  a[], T2 sz, T2 beg = 0) { for (T2 i = beg; i<sz; i++) cout << a[i] << " "; cout << endl; }
#define DBG(a)         cout<<#a<<"="<<(a)<<"\n"
#define DBG2(a,b)       cout<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<"\n"
#define DBG3(a,b,c)     cout<<#a<<"="<<(a)<<", "<<#b<<"="<<(b)<<", "<<#c<<"="<<(c)<<"\n"
 
//Files
#define FILE_MODE(FILE) freopen(FILE".in", "r", stdin), freopen(FILE".out", "w", stdout)
 
//Constants
#define PI    3.1415926535897932
#define INF   1011111111
#define LLINF 1000111000111000111LL
#define eps   1e-14
#define mod   1000000007
#define ll long long
#define ull unsigned long long
//-------------------------------------------------
#define N 21
 
void dfs(int v);
vector <int> path;
vector <int> dividers;
int n, k;
 
 
int main() {
    cin >> n >> k;
    bool impossible = false;
    if (k == 1) {
        cout << n; return 0;
    }
    dfs(n);
    if (path.size() == !k) { cout << -1; return 0; }
    copy(path.begin(), path.end(), ostream_iterator<int>(cout, " "));
    return 0;
}
 
void dfs(int v) {
    if (path.size() + 1 < k) {
        if (v % 2 == 0 && path.size() != k && v / 2 != 1) {
            path.push_back(2);
            dfs(v / 2);
        }
        if (v % 3 == 0 && path.size() != k && v / 3 != 1) {
            path.push_back(3);
            dfs(v / 3);
        }
        if (v % 5 == 0 && path.size() != k && v / 5 != 1) {
            path.push_back(5);
            dfs(v / 5);
        }
        if (path.size() != k && path.size() != 0)
            path.pop_back();
    }
    else {
        path.push_back(v);
    }
}

Comments