HDU 1395 2^x mod n = 1 violence ~ ~ Best to learn Euler theorem ~ ~ ~

Source: Internet
Author: User

2^X mod n = 1Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others)
Total submission (s): 13341 Accepted Submission (s): 4143


Problem descriptiongive a number n, find the minimum x (x>0) that satisfies 2^x mod n = 1.

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

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

Print 2^? MoD n = 1 otherwise.

You should replace x and n with specific numbers.

Sample Input
25

Sample Output
2^? MoD 2 = 12^4 MoD 5 = 1

Authorma, Xiao Euler theorem Baidu Encyclopedia mode operation Nature as a acmer, or take a good look at the above Baidu Encyclopedia, very useful

According to modulo p multiplication inverse: For the integer A, p if there is an integer b, satisfies a*b mod p=1 is called B is a modulo p multiplication inverse element.

A necessary and sufficient condition of the multiplication inverse of the existence modulo P is gcd (a,p) = 1, which makes A=2^x,b=1,p=n

If present x uses 2^x mod n=1 then gcd (2^x,n) =1

(1) Because the value of x is greater than 0. The 2^x factor is only one 2, so when n is even, gcd (2^x,n) =2k (k=1,2,3 ...), i.e. no x at this time, makes 2^x mod n=1.

(2) When n is odd gcd (2^x,n) = 1, an x is required to make the 2^x mod n=1.

(3) As the result of any modulo 1 is 0, so when n=1, regardless of x value, 2^x mod n=0.

Combining the above (1), (2), (3), when the value of n is 1 or even, there is no X to make the 2^x mod n=1, and in other cases there must be an X to make 2^x mod n = 1.


Code:
#include <stdio.h>int main () {int n, while (~scanf ("%d", &n)) {if (N==1 | | n%2==0) {printf ("2^? MoD%d = 1\n ", n);} Else{int j = 1, Mi=2;while (true) {mi%= n, if (mi = = 1) {printf ("2^%d mod%d = 1\n", j,n); Mi *= 2; ++j;} }}return 0;}

with June

HDU 1395 2^x mod n = 1 violence ~ ~ Best to learn Euler theorem ~ ~ ~

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.