Ultraviolet A Problem 10139 factovisors (factorial and Division)

Source: Internet
Author: User
Tags dot net
// Factovisors (factorial and division) // PC/Ultraviolet IDs: 110704/10139, popularity: A, success rate: average level: 2 // verdict: accepted // submission date: 2011-06-11 // UV Run Time: 0.312 S // copyright (c) 2011, Qiu. Metaphysis # Yeah dot net // The idea is relatively simple. Calculate the number M and number 2-n gcd. If GCD is greater than 1, m/= GCD, if the last M is not // 1, it indicates that M cannot divide N !, Note that the input may exist in some special cases. You only need to handle it. It is strange that on the // programming challenges, the Rt is 0.072 seconds, but on the ultraviolet A, it is always TLE. Then, a statement for testing the prime number is added to the // command, it should be caused by special test data. Therefore, you need to check the conditions in the loop in time and exit the // loop to avoid TLE. # Include <iostream> # include <cmath> using namespace STD; // determine whether a number is a prime number. Bool is_prime (long unsigned number) {If (number = 2) return true; If (Number % 2 = 0) return false; For (long unsigned c = 3; c <= Ceil (SQRT (number); C + = 2) if (Number % C = 0) return false; return true;} // calculate the two-digit GCD. Long unsigned gcd (long unsigned A, long unsigned B) {if (a <B) {long unsigned TMP = A; A = B; B = TMP ;} while (B> 0) {long unsigned TMP = A; A = B; B = TMP % B;} return a;} int main (INT AC, char * AV []) {long unsigned n, m; // read N and M. While (CIN> N> m) {bool divided = false; long unsigned T = m, G; // in special cases, M = 1. If (M = 1) {divided = true; goto print;} // in special cases, M = 1. If (M = 0) goto print; // If M is a prime number and M is greater than N, M and 2-N are mutually Prime. If (is_prime (m) & M> N) goto print; // m <= N, of course, division can be performed. If (M <= N) {divided = true; goto print;} // you can find gcd (M, c) in descending order of N to 2 ). For (long unsigned c = N; C> = 2; c --) {G = gcd (M, c); // when there is a maximum public approx greater than 1, determine whether exit conditions are met. If (G> 1) {M/= g; // m <C, division is allowed. If (M <c) {divided = true; goto print;} // m> = C. If (is_prime (M) goto print ;}// print the result. Note the differences in output words. Print: If (divided) cout <t <"divides" <n <"! "<Endl; elsecout <t <" does not divide "<n <"! "<Endl;} 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.