Ultraviolet A 10392-Factoring Large Numbers

Source: Internet
Author: User

This question refers to breaking down big numbers. The number range is Longlong, and it seems that it cannot be violent. However, the question provides a condition, with at most one reason
The child size exceeds 1000000. Haha, This is a storm. In this case, we can enumerate the factors of less than 1000000. If the remaining part is greater than 6 times of 10
It must be a factor of N, so no violence is needed. If the number is less than 10, it must be 1 for 6 times, because the 2-000000 factors are all processed.
In this way, the question will not time out. Indeed, violence requires skill. Also note that % lld is used for the request.

# Include <stdio. h>
# Include <math. h>
Typedef long LL;
# Define MAX (6000000)
Bool bPrime [MAX];
Int nPrime [MAX];
Int nNum;

Void InitPrime ()
{
LL nMax = sqrt (MAX) + 1;
BPrime [0] = bPrime [1] = true;
For (int I = 2; I <= nMax; ++ I)
{
If (! BPrime [I])
{
For (int j = 2 * I; j <MAX; j + = I)
{
BPrime [j] = true;
}
}
}
For (int I = 2; I <MAX; ++ I)
{
If (! BPrime [I])
NPrime [nNum ++] = I;
}

}

Bool IsPrime (LL nN)
{
If (nN <MAX) return! BPrime [nN];
LL nMax = sqrt (double) nN) + 1;
For (LL j = 0, I = nPrime [j]; I <= nMax; ++ j, I = nPrime [j])
{
If (nN % I = 0)
{
Return false;
}
}
Return true;
}

Int main ()
{
LL nN;

InitPrime ();
While (scanf ("% lld", & nN), nN> = 0)
{
If (nN <= 2)
{
Printf ("%-lld \ n", nN );
Continue;
}

Int nMax = sqrt (double) nN) + 1;
For (LL I = 2; I <= 1000000 & I <= nMax; ++ I)
{
While (nN % I = 0)
{
Printf ("%-lld \ n", I );
NN/= I;
}
If (nN <6000000 & IsPrime (nN ))
{
Break;
}
}
If (nN! = 1)
Printf ("%-lld \ n", nN );
Printf ("\ 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.