HDU, hdukmp

Source: Internet
Author: User

HDU, hdukmp

Zhx's contest Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission (s): 448 Accepted Submission (s): 147


Problem DescriptionAs one of the most powerful brushes, zhx is required to give his juniors N Problems.
Zhx thinks Ith Problem's difficulty is I . He wants to arrange these problems in a beautiful way.
Zhx defines a sequence {Ai} Beautiful if there is I That matches two rules below:
1: A1.. ai Are monotone decreasing or monotone increasing.
2: Ai .. Are monotone decreasing or monotone increasing.
He wants you to tell him that how many permutations of problems are there if the sequence of the problems 'difficulty is beautiful.
Zhx knows that the answer may be very huge, and you only need to tell him the answer module P .
InputMultiply test cases (less 1000 ). Seek EOF As the end of the file.
For each case, there are two integers N And P Separated by a space in a line .( 1 ≤ n, p ≤ 1018 )
OutputFor each test case, output a single line indicating the answer.
 
Sample Input
2 2333 5
 
Sample Output
21HintIn the first case, both sequence {1, 2} and {2, 1} are legal.In the second case, sequence {1, 2, 3}, {1, 3, 2}, {2, 1, 3}, {2, 3, 1}, {3, 1, 2}, {3, 2, 1} are legal, so the answer is 6 mod 5 = 1 
 
SourceBestCoder Round #33






The answer is (2 ^ N-2) % p.


However, n and p are all LL-type, and the LL will be exploded when the power is saved. Therefore, fast multiplication is used here. Fast Multiplication is similar to the power of Fast Multiplication, that is, changing the multiplication number to the plus sign.


Note: When n is 1, 1 is output, and when p is 1, 0 is output;


AC code:

# Include <cstdio> # include <cstring> # include <iostream> # include <algorithm> # define LL long using namespace std; LL n, p; LL multi (LL, LL B) {// fast multiplication, which is similar to the Fast Power LL ret = 0; while (B) {if (B & 1) ret = (ret + a) % p; a = (a + a) % p; B >>= 1;} return ret;} LL powmod (LL a, LL B) {// fast power LL ret = 1; while (B) {if (B & 1) ret = multi (ret, a) % p; a = multi (a, a) % p; B >>= 1 ;} return ret;} int main () {while (cin> n> p) {if (p = 1) {cout <0 <endl ;} else if (n = 1) {cout <1 <endl;} else {LL ans = powmod (2, n)-2; if (ans <0) ans + = p; cout <ans <endl ;}} 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.