Zoj problem set-1489 2 ^ x mod n = 1

Source: Internet
Author: User

Time Limit: 2 seconds memory limit: 65536 KB

Give a number N, find the minimum X that satisfies 2 ^ x mod n = 1.

Input

One positive integer on each line, the value of N.

Output

If the minimum x exists, print a line with 2 ^ x mod n = 1.

Print 2 ^? MOD n = 1 otherwise.

You shoshould replace X and N with specific numbers.

Sample Input

2
5

Sample output

2 ^? MoD 2 = 1
2 ^ 4 mod 5 = 1


Author:Ma, Xiao
Source:Zoj monthly, February 2003

This question seems to have few descriptions. I like this question. I am not very good at English ....

The question is very clear, that is, to divide the X power of 2 by the remainder of N to the X value of 1. When I first saw this question, I thought it would be a good way to use the table-driven method, so I thought I would first create a table of 2 to the power of 32, then, you only need to traverse the table every time you obtain the result.Code:

 
# Include <iostream>
 
Using NamespaceSTD;
 
Const IntNum = 32;
Int* Pow2 =New Int[Num];
 
VoidInitial2pow ()
 
{
 
* Pow2 = 1;
For(IntI = 1; I <num; I ++)
 
* (Pow2 + I) = 2*(pow2 + I-1 ));
 
}
 
IntMain ()
 
{
 
 
Initial2pow ();
 
IntN;
 
While(CIN> N)
 
{
If(N % 2 = 0 | n <2)
 
{
 
Cout <"2 ^? MoD"<N <"= 1"<Endl;
 
Continue;
}
 
BoolFind =False;
 
For(IntI = 1; I <num; I ++)
 
{
If(* (Pow2 + I) % N = 1)
 
{
 
Cout <"2 ^"<I <"MoD"<N <"= 1"<Endl;
 
Find =True;
Break;
 
}
 
}
 
If(! Find)
 
{
Cout <"2 ^? MoD"<N <"= 1"<Endl;
 
}
 
}
 
}

I feel that the code is concise and can be submitted, but the submitted code gets wa! At this time, I realized that it may be due to precision issues, and the 32-bit int may not be enough. So I used 64-bit long to replace Int. After resubmitting, I still get wa! At this moment, I found that this question is still not that simple! At least it should not be simply a table driver to solve this problem. You still need some mathematical skills. First, I wrote the computing process on top of the PEN:

A/B = C .............. D. Multiply the two sides of the equation by 2 to get 2a/B = 2C ....... 2d. Now we can see that the remainder of 2a % B is the same as that of 2D % B!

If 2D/B = C0 ..... D0, then 2a/B = 2C + C0 ....... D0, with these mathematical inferences, the corrected code is as follows:

 
# Include <iostream>
 
Using NamespaceSTD;
 
IntMain ()
 
{
IntN;
 
While(CIN> N)
 
{
 
If(N % 2 = 0 | n <2)
 
{
Cout <"2 ^? MoD"<N <"= 1"<Endl;
 
Continue;
 
}
 
BoolFind =False;
IntD = 1;
 
For(IntI = 1; I ++)
 
{
 
D * = 2;
 
If(D % N = 1)
{
 
Cout <"2 ^"<I <"MoD"<N <"= 1"<Endl;
 
Find =True;
 
Break;
}
 
D = D % N;
 
}
 
If(! Find)
 
{
Cout <"2 ^? MoD"<N <"= 1"<Endl;
 
}
 
}
 
}

AC successful!

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.