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:
- /***************************************************************************
- * Copyright (C) 2008 by Liu Kaipeng *
- * LiuKaipeng at gmail dot com *
- ***************************************************************************/
- /* @JUDGE_ID 00000 10139 C++ "Factovisors" */
- #include <algorithm>
- #include <cmath>
- #include <cstdio>
- #include <cstring>
- #include <deque>
- #include <fstream>
- #include <iostream>
- #include <list>
- #include <map>
- #include <queue>
- #include <set>
- #include <stack>
- #include <string>
- #include <vector>
- using namespace std;
- bool is_factorial_divide(int n, int m)
- {
- if (m == 0) return false;
- if (n == 0 && m == 1) return true;
- for (int f = 2, mf = sqrt(m) + 1; m > n && f < mf; ++f) {
- int c = 0;
- for (; m % f == 0; m /= f, ++c) {}
- for (int nf = f; c > 0 && nf <= n; c -= n / nf, nf *= f) { }
- if (c > 0) return false;
- }
- return m <= n;
- }
- int main(int argc, char *argv[])
- {
- #ifndef ONLINE_JUDGE
- freopen((string(argv[0]) + ".in").c_str(), "r", stdin);
- freopen((string(argv[0]) + ".out").c_str(), "w", stdout);
- #endif
- char const *msg[] = {
- " does not divide ",
- " divides ",
- };
- for (int n, m; cin >> n >> m; )
- cout << m << msg[is_factorial_divide(n, m)] << n << "!/n";
- return 0;
- }