UVa Problem Solution: 10139 - Factovisors

来源:互联网 发布:学校排课软件 编辑:程序博客网 时间:2024/05/21 09:32

We check each of the prime factors of m to see if n! has enough prime factors of the same value. If so, m can divide n!, otherwise m can not divide n!.

Code:
  1. /***************************************************************************
  2.  *   Copyright (C) 2008 by Liu Kaipeng                                     *
  3.  *   LiuKaipeng at gmail dot com                                           *
  4.  ***************************************************************************/
  5. /* @JUDGE_ID 00000 10139 C++ "Factovisors" */
  6. #include <algorithm>
  7. #include <cmath>
  8. #include <cstdio>
  9. #include <cstring>
  10. #include <deque>
  11. #include <fstream>
  12. #include <iostream>
  13. #include <list>
  14. #include <map>
  15. #include <queue>
  16. #include <set>
  17. #include <stack>
  18. #include <string>
  19. #include <vector>
  20. using namespace std;
  21.      
  22. bool is_factorial_divide(int n, int m)
  23. {
  24.   if (m == 0) return false;
  25.   if (n == 0 && m == 1) return true;
  26.   for (int f = 2, mf = sqrt(m) + 1; m > n && f < mf; ++f) {
  27.     int c = 0;
  28.     for (; m % f == 0; m /= f, ++c) {}
  29.     for (int nf = f; c > 0 && nf <= n; c -= n / nf, nf *= f) { }
  30.     if (c > 0) return false;
  31.   }
  32.   return m <= n;
  33. }
  34.           
  35. int main(int argc, char *argv[])
  36. {
  37. #ifndef ONLINE_JUDGE
  38.   freopen((string(argv[0]) + ".in").c_str(), "r", stdin);
  39.   freopen((string(argv[0]) + ".out").c_str(), "w", stdout);
  40. #endif
  41.   char const *msg[] = {
  42.     " does not divide ",
  43.     " divides ",
  44.   };
  45.   for (int n, m; cin >> n >> m; )
  46.     cout << m << msg[is_factorial_divide(n, m)] << n << "!/n";
  47.   
  48.   return 0;
  49. }

原创粉丝点击