KarleFKremen

Untitled

Apr 9th, 2016
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.67 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. /****************************************
  4.  *                                      *
  5.  *  Using:      Extended SP template    *
  6.  *  Made by:    Karle F. Kremen         *
  7.  *  At:         5.2.16 00:41:50 ALMT    *
  8.  *  For:        LKSH Qualification      *
  9.  *                                      *
  10.  ****************************************/
  11.  
  12. // settings
  13.     #define FILE "good"
  14.     #define USE_FREOPEN 1
  15.     #define USE_LONG 1
  16.     #define USE_IOSOPT 0
  17.     #define INTERACT 0
  18.  
  19. // system
  20.     #if USE_LONG
  21.         typedef long long ll;
  22.     #else
  23.         typedef int ll;
  24.     #endif
  25.     #if USE_LONG
  26.         typedef unsigned long long ull;
  27.     #else
  28.         typedef unsigned int ull;
  29.     #endif
  30.     typedef short sym;
  31.     #if INTERACT
  32.         #define eol endl
  33.     #else
  34.         #define eol '\n'
  35.     #endif
  36.     #define pb push_back
  37.     #define mp make_pair
  38.     #define p(a, b) pair < a, b >
  39.     #define sq(x) (x * x)
  40.     #define fi first
  41.     #define se second
  42.     #define rm erase
  43.     #define ins insert
  44.     #define rev(a) reverse(a.begin(), a.end())
  45.     #define ord(a) sort(a.begin(), a.end())
  46.     #define rs resize
  47.     #define sz size
  48.     #define M3 (ll)1e3
  49.     #define M4 (ll)1e4
  50.     #define M5 (ll)1e5
  51.     #define M6 (ll)1e6
  52.     #define M7 (ll)1e7
  53.     #define M8 (ll)1e8
  54.     #define M9 (ll)1e9
  55.     #define stlprint(a) \
  56.         for(auto __LEVEL_1_STL_ITERATOR = a.begin(); __LEVEL_1_STL_ITERATOR != a.end(); __LEVEL_1_STL_ITERATOR++) \
  57.         { \
  58.             cout << (*__LEVEL_1_STL_ITERATOR) << ' '; \
  59.         }
  60.     #define stlfor(a) \
  61.         for(auto it = a.begin(); it != a.end(); it++)
  62.     #define die(a) \
  63.         { \
  64.             cout << a << endl; \
  65.             exit(0); \
  66.         }
  67.  
  68. using namespace std;
  69.  
  70. ll prm[] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71 };
  71.  
  72. ll bpow(ll n, ll p, ll base)
  73. {
  74.     if(p == 1)
  75.         return n % base;
  76.     if(p == 0)
  77.         return 1;
  78.     if(p & 1)
  79.     {
  80.         return ((n % base) * (bpow(n, p - 1, base) % base)) % base;
  81.     }
  82.     ll tmp = bpow(n, p >> 1, base) % base;
  83.     return (tmp * tmp) % base;
  84. }
  85.  
  86. bool isPrime(ll n)
  87. {
  88.     if(n < 2)
  89.         return 0;
  90.     for(int i = 0; i < 20; i++)
  91.     {
  92.         if(prm[i] == n)
  93.             return 1;
  94.         ll pw = bpow(prm[i], n - 1, n);
  95.         if(pw != 1)
  96.             return 0;
  97.     }
  98.     return 1;
  99. }
  100.  
  101. int main()
  102. {
  103.     // ios{
  104.         #if USE_IOSOPT
  105.             ios_base::sync_with_stdio(false);
  106.             cin.tie(0);
  107.         #endif
  108.         #if USE_FREOPEN
  109.             freopen(FILE".in", "r", stdin);
  110.             freopen(FILE".out", "w", stdout);
  111.  
  112.         #endif
  113.     ll n, k;
  114.     cin >> n >> k;
  115.     ll kc = 0;
  116.     if(isPrime(n))
  117.         die(-1);
  118.     while(1)
  119.     {
  120.         if(n % k == 0)
  121.         {
  122.             n /= k;
  123.             kc++;
  124.             continue;
  125.         }
  126.         break;
  127.     }
  128.     if(isPrime(n) && kc <= 1)
  129.         die(-1);
  130.     if(kc > 1)
  131.     {
  132.         cout << pow(k, kc) << ' ';
  133.         kc = 0;
  134.     }
  135.     for(int i = 2; (i * i) <= n; i++)
  136.     {
  137.         while(n % i == 0)
  138.         {
  139.             n /= i;
  140.             if(kc > 0)
  141.             {
  142.                 cout << (i * k) << ' ';
  143.                 kc--;
  144.                 continue;
  145.             }
  146.             cout << i << ' ';
  147.         }
  148.     }
  149.     cout << n << endl;
  150. }
Advertisement
Add Comment
Please, Sign In to add comment