Winter 2018 02 01 A question about modulo operation

Source: Internet
Author: User

Title: Give a positive integer n, ask if there is x, satisfy the condition 2^x mod n=1, if present, find the minimum value of x.

Analysis: 1, if the given n is 1, then there is certainly no such x;

2, if the given is even, 2 of the power to take more than an even number is definitely to the even, so also can not find;

3, if the given is an odd number, its digit number is 3, 5, 7, 9, and 2 of the power of the units of 2, 4, 6, 8, respectively-1 is 1, 3, 5, 7, that is, odd multiples can find relative, then 2 of the second power of each odd number will find a suitable power to meet test instructions.

Code:

#include <stdio.h>

int main ()
{
int n;
while (scanf ("%d", &n)!=eof)
{
if (n==1 | | n%2==0)
{
printf ("2^?") MoD%d = 1\n ", n);
}
Else
{
Int j = 1, mi=2;
while (1)
{
mi%= n;//Let min equals min to take the remainder of N, because this number to take the remaining n is 1 with the quotient is irrelevant, next only to see the remainder can be.
if (mi = = 1)
{
printf ("2^%d mod%d = 1\n", j,n);
break;
}
Mi *= 2;
j + +;
}
}
}
return 0;
}

Winter 2018 02 01 A question about modulo operation

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.