HDU 1395 number theory tips

Source: Internet
Author: User

Small and medium number theory skills: because only one remainder of mod N is required at a time, for the convenience of computer operations, first convert it ..

(A * B) % N = [(a % N) * (B % N)] % N;

(A + B) % N = [(a % n) + (B % N)] % N;

Question: Chinese

Idea: Euler's theorem, but it requires the smallest integer, so we can only use Euler's Theorem to determine the optimization...

For example: 2 ^ K mod n = 1; then K = E (n) must be true; and if such formula is true,Then (2, n) must be of mutual quality (Euler's theorem ),I won't prove it...

Let's try again with brute force. I can't think of a better solution...

Code:

# Include <iostream>
Using namespace STD;
Int main (){
Int N;
While (CIN> N ){
If (N % 2 = 0 | n = 1 ){
Printf ("2 ^? MoD % d = 1 \ n ", N );
Continue;
}
Else {
Int sum = 1;
For (INT I = 1; I ++ ){
Sum * = 2;
If (sum % N = 1 ){
Printf ("2 ^ % d mod % d = 1 \ n", I, n );
Break;
}
Sum = sum % N;
}
}
}
}

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.