HDU 1452 factor and + product function + inverse element

Source: Internet
Author: User

The explanation is too incisive and hard to explain...

Magic bull link http://www.cppblog.com/vontroy/archive/2010/10/02/128356.html

 

Calculate the factor of 2004 ^ X and S (2004 ^ X) mod m, M = 29

S (2004 ^ X) % 29
Factors and S are product functions, that is, gcd (a, B) = 1 => S (A * B) = S (a) * s (B)

2004 ^ x = 4 ^ x * 3 ^ x * 167 ^ x
S (2004 ^ X) = S (2 ^ (2x) * s (3 ^ X) * s (167 ^ X)

If P is a prime number => S (P ^ X) = 1 + P ^ 2 + + P ^ x = (P ^ (x + 1)-1) /(p-1)

S (2004 ^ X) = (2 ^ (2x + 1)-1) * (3 ^ (x + 1)-1) /2*(167 ^ (x + 1)-1)/166

167% 29 = 22

S (2004 ^ X) = (2 ^ (2x + 1)-1) * (3 ^ (x + 1)-1) /2*(22 ^ (x + 1)-1)/21

(A * B)/C % m = A % m * B % m * inv (c)

C * inv (c) = all the numbers where the 1% M modulo is 1 Inv (c) is the smallest and can be divisible by C

Inv (2) = 15, inv (21) = 18 2*15 = 1 mod 29, 18*21 = 1 mod 29

S (2004 ^ X) = (2 ^ (2x + 1)-1) * (3 ^ (x + 1)-1) /2*(22 ^ (x + 1)-1)/21
= (2 ^ (2x + 1)-1) * (3 ^ (x + 1)-1) * 15*(22 ^ (x + 1)-1) * 18
**************************************** ***********************/

# Include <iostream>
# Include <cmath>
Using namespace STD;

Int _ POW (int A, int N) {// evaluate the solution of a ^ n % 29
Int B = 1;
While (n> 1) {// This code is hard to explain. Let's take a look at the idea of binary.
If (N % 2 = 0 ){
A = A * A % 29;
N/= 2;
}
Else {
B = B * A % 29;
N --;
}
}
Return a * B % 29;
}

Int main (){
Int X, A, B, C;
While (CIN> x, x) {// The Reverse element is obtained directly.
A = _ POW (2, 2 * x + 1 );
B = _ POW (3, (x + 1 ));
C = _ POW (22, (x + 1 ));
Cout <(A-1) * (b-1) * 15 * (c-1) * 18% 29 <Endl;
}
}

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.