Codeforces 797A - k-Factorization Solution
Solution in C++:
///**********ALLAH IS ALMIGHTY************///
///AH Tonmoy
///Department of CSE,23rd batch
///Islamic University,Bangladesh
- #include <bits/stdc++.h>
- using namespace std;
- void kFactors(int n, int k)
- {
- vector<int> v;
- while (n%2 == 0)
- {
- v.push_back(2);
- n/= 2;
- }
- for (int i=3; i*i<=n; i=i+2)
- {
- while (n%i == 0)
- {
- n = n/i;
- v.push_back(i);
- }
- }
- if (n > 2)
- v.push_back(n);
- if (v.size() < k)
- {
- cout << "-1" << endl;
- return ;
- }
- for (int i=0; i<k-1; i++)
- cout << v[i] << " ";
- int r=1;
- for (int i=k-1; i<v.size(); i++)
- r = r*v[i];
- cout << r << endl;
- }
- int main()
- {
- int n,k;
- cin>>n>>k;
- kFactors(n, k);
- return 0;
- }
No comments