Va 10139 factovisors (number theory)

Source: Internet
Author: User
Factovisorsthe factorial function, n! Is defined thus for n a non-negative integer:
   0! = 1   n! = n * (n-1)!   (n > 0)
We say that a divides B if there exists an integer k such that
   k*a = b

The input to your program consists of several lines, each containing two non-negative integers, N and M, both less than 2 ^ 31. for each input line, output a line stating whether or not m divides n !, In the format shown below.

Sample Input
6 96 2720 1000020 1000001000 1009
Output for sample input
9 divides 6!27 does not divide 6!10000 divides 20!100000 does not divide 20!1009 does not divide 1000!

Question: Give N and M, and ask if m can divide the factorial of N. Analysis: prime factor decomposition can be performed on m to obtain the number of each prime factor, and n! To compare the number of this factor. If it is greater than n! The number of this factor, you cannot divide.
# Include <stdio. h> # include <string. h> # include <math. h> const int maxn = 100005; int vis [maxn], prime [10000], num; void get_prime () // returns the prime number {num = 0; memset (VIS, 0, sizeof (VIS); For (INT I = 2; I <maxn; I ++) {If (! Vis [I]) {Prime [num ++] = I; for (Int J = I + I; j <maxn; j + = I) vis [J] = 1 ;}}} int CAL (int w, int p) // calculate the number of P in the factorial of W {int ans = 0; while (W) {w/= P; ans + = W;} return ans;} bool judge (int n, int m) {int K = (INT) SQRT (m + 0.5 ); for (INT I = 0; I <num & prime [I] <= K; I ++) {If (M % prime [I] = 0) {int CNT = 0; while (M % prime [I] = 0) {CNT ++; M/= prime [I];} If (CAL (n, prime [I]) <CNT) return false ;} // If m at this time! = 1, then M must be a prime number. If n> = m, m must be able to divide n! If (M> 1 & n <m) return false; return true;} int main () {int n, m; get_prime (); While (~ Scanf ("% d", & N, & M) {If (Judge (n, m) printf ("% d divides % d! \ N ", m, n); else printf (" % d does not divide % d! \ N ", m, n);} return 0 ;}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.