Hdoj 1395 2^x mod n = 1 "Violence"

Source: Internet
Author: User

Strategy: observed that 1 or can be divisible by 2 of the number will not be equal to 1, just need to infer is not except 1 odd, in turn find 2^x (mod (n))?

= 1 will be able to

The difficulty: Assume that each time it is based on the original X2 and then the inference will time out. At this point, we can use the congruence theorem.

AC by SWS;

Topic Link: http://acm.hdu.edu.cn/showproblem.php?

pid=1395

Code:

#include <stdio.h>int main () {int n;while (scanf ("%d", &n) = = 1) {if (n ==1| | n%2==0) {printf ("2^? MoD%d = 1\n ", n); continue;} Else{int sum = 2, I, DC = 1;for (i = 1;; i + +) if (sum%n! = 1) {sum = sum%n*2;//congruence theorem sumx2%n = = (sum%n) x (2%n) = = (2<n) = > Sum%n*2}elsebreak;printf ("2^%d mod%d = 1\n", I, N);}} return 0;}


Hdoj 1395 2^x mod n = 1 "Violence"

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.