mramine364

divisors from factors

Aug 19th, 2016
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.91 KB | None | 0 0
  1. #include<iostream>
  2. #include<vector>
  3.  
  4. using namespace std;
  5.  
  6. vector<long> divs(vector<vector<int>> fs, int s, int e){
  7.     vector<long> r;
  8.     if (e == s){
  9.         vector<int> fa = fs[s];
  10.         long c = 1, p = 0;
  11.         while (p <= fa[1]){
  12.             r.push_back(c);
  13.             c *= fa[0];
  14.             p++;
  15.         }
  16.         return r;
  17.     }
  18.     vector<long> sr = divs(fs, s + 1, e);
  19.     //cout << "s, e: " << s + 1 << ", " << e << "\n";
  20.     //for (int i = 0; i < sr.size(); i++){
  21.         //cout << sr[i] << ", ";
  22.     //}cout << endl;
  23.  
  24.     vector<int> fa = fs[s];
  25.     long c = 1, p = 0;
  26.     while (p <= fa[1]){
  27.         // r.add(p);
  28.         for (long n : sr){
  29.             r.push_back(n*c);
  30.         }
  31.         c *= fa[0];
  32.         p++;
  33.     }
  34.     return r;
  35. }
  36.  
  37. int main(){
  38.    
  39.     vector<vector<int>> fs;
  40.     fs.push_back({ 2, 2 });
  41.     fs.push_back({ 3, 1 });
  42.     //fs.push_back({ 5, 3 });
  43.  
  44.     vector<long> d = divs(fs, 0, fs.size() - 1);
  45.  
  46.     for (int i = 0; i < d.size(); i++){
  47.         cout << d[i] << ", ";
  48.     }cout << endl;
  49.  
  50.     getchar();
  51.     return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment