Hdoj 1395 2 ^ x mod n = 1 [brute force]

Source: Internet
Author: User

Strategy: we can see that the remainder is not equal to 1 or the number that can be divisible by 2. We only need to judge whether it is an odd number other than 1, search for 2 ^ X (mod (N) in sequence ))? = 1.

Difficulty: if every time it is based on the original × 2, it will time out. At this time, we can use the same remainder theorem.

AC by SWS;

Question link: http://acm.hdu.edu.cn/showproblem.php? PID = 1, 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; // same remainder theorem sum × 2% N = (sum % N) × (2% N) = (2 <n) => sum % N * 2} elsebreak; printf ("2 ^ % d mod % d = 1 \ n", I, 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.