Saturday, February 8, 2020

POJ.3421 X-factor Chains

1.Problem
http://poj.org/problem?id=3421

2.Idea
Prime number factorize.

3.Source
 ll fac[20];  
 void solve(int x)  
 {  
      map<int, int> res;  
      for (int i = 2; i*i <= x; i++) {  
           while (x%i == 0) {  
                ++res[i];  
                x /= i;  
           }  
      }  
      if (x != 1) res[x] = 1;  
      fac[0] = 1;  
      fac[1] = 1;  
      for (int i = 2; i < 20; i++) {  
           fac[i] = fac[i - 1] * (ll)i;  
      }  
      if (res.size() == 1) {  
           cout << res.begin()->second << " 1" << endl;  
           return;  
      }  
      //calc  
      ll total = 0, dup = 1;  
      for (map<int, int>::iterator itr = res.begin(); itr != res.end(); itr++) {  
           total += itr->second;  
           dup = dup * fac[itr->second];  
      }  
      cout << total << " " << fac[total] / dup << endl;  
 }  
 int main()  
 {  
      int x;  
      while (scanf("%d", &x) > 0) {  
           solve(x);  
      }  
      return 0;  
 }  

No comments:

Post a Comment