HDU 1852 quick power determination

Source: Internet
Author: User

The question wa has been a lot of back... sad reminder... you cannot directly reverse the yuan for computation. You still need to use the tips in number theory...
Stick to the question of shenniu ..
// This subject requires S
// Conclusion: S = (251 ^ (n + 1)-1) * (2 ^ (3n + 1)-1)/250
// Two equal-ratio series and multiplication
//
// Reasoning:
// 2008 = 2 ^ 3*251
// So 2008 ^ N has 3 N 2 and N 251
// All the factors that only consist of 2 are
// 2 ^ 0 2 ^ 1 2 ^ 2... 2 ^ (3N)
// Set the set C = {2 ^ 0, 2 ^ 1, 2 ^ 2..., 2 ^ (3N )};
// SUM (C) = 2 ^ (3n + 1)-1

// The factors produced in combination with 251 are:
// 251 ^ 0 * C
// 251 ^ 1 * C
//...
// 251 ^ N * C

// The sum of all factors is:
// S = (251 ^ (n + 1)-1)/250*(2 ^ (3n + 1)-1)

// Calculate S % K:
// S is large and cannot be stored in common data types. You need to calculate S % K directly.
// Because S has a denominator of 250, set S to X/250
// S % K = (X/250) % K = (X % (250 * K)/250
// The form of first finding the remainder and then Division
Once you know the techniques in number theory, you cannot use your back-to-source to find out, because not meeting the requirements of gcd (250, k) or gcd (2, k) is not necessarily of mutual quality, the code is no longer a problem,
 
# Include <stdio. h>
Int mult (int a1, int n, int k ){
If (n = 0) return 1;
_ Int64 B = 1, a = a1;
While (n> 1 ){
If (n % 2 = 0 ){
A = a * a % k;
N/= 2;
}
Else {
B = B * a % k;
N --;
}
}
Return a * B % k;
}
Int main (){
Int n, k;
While (scanf ("% d", & n, & k), n & k ){
_ Int64 a = mult (2, 3 * n + 1,250 * k );
_ Int64 B = mult (251, n + 1,250 * k );
A --, B --;
A = (a * B) % (250 * k );
A/= 250;
Printf ("% d \ n", mult (2008, a, k ));
}
}

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.