Codeforces 797A - k-Factorization Solution

 Solution in C++: 

///**********ALLAH IS ALMIGHTY************///

///AH Tonmoy

///Department of CSE,23rd batch

///Islamic University,Bangladesh  

  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. void kFactors(int n, int k)
  4. {
  5. vector<int> v;
  6. while (n%2 == 0)
  7. {
  8. v.push_back(2);
  9. n/= 2;
  10. }
  11. for (int i=3; i*i<=n; i=i+2)
  12. {
  13. while (n%i == 0)
  14. {
  15. n = n/i;
  16. v.push_back(i);
  17. }
  18. }
  19. if (n > 2)
  20. v.push_back(n);
  21. if (v.size() < k)
  22. {
  23. cout << "-1" << endl;
  24. return ;
  25. }
  26. for (int i=0; i<k-1; i++)
  27. cout << v[i] << " ";
  28. int r=1;
  29. for (int i=k-1; i<v.size(); i++)
  30. r = r*v[i];
  31. cout << r << endl;
  32. }
  33.  
  34. int main()
  35. {
  36. int n,k;
  37. cin>>n>>k;
  38. kFactors(n, k);
  39. return 0;
  40. }

No comments

Most View Post

Recent post

Codeforces Round 971 (Div. 4) 2009C. The Legend of Freya the Frog Solution

  Problem Link    https://codeforces.com/contest/2009/problem/C S olution in C++: /// Author : AH_Tonmoy #include < bits / stdc ++. h ...

Powered by Blogger.