acm:a^b%p-number theory-fast power-fast multiplication

Source: Internet
Author: User

A^b
Time Limit: 1000MS Memory Limit: 65535KB 64bit IO Format:

Description

Ask for A's B-square, modulo mod (1<=a,b,mod<=1e18)

Input

Multiple sets of inputs, one row per set of data, 3 positive integers, respectively a,b,mod

Output

One row for each set of data output, for the answer

Sample Input

2 10 100000005 100 10 2 37

Sample Output

102400


Template problem, mainly considering the 1e18 of the huge, ordinary fast power will explode ll so in the place of multiplication with the fast multiply, avoid the burst LL.

#include "Cstdio" Long long Mod_mul (long long A,long long B,long long) {Long long r=0;long long t=a; while (b) {if (b&1) R= (r+t)%p;t= (t<<1)%p;b>>=1;} return r;} Long Long Mod_pro (long long A,long long B,long long p) {Long long r=1;long long t=a; while (b) {if (b&1) R=mod_mul (r,t,p )%p;t=mod_mul (t,t,p)%p;b>>=1;} return r;} int main () {Long Long a,b,mod;while (~scanf ("%i64d%i64d%i64d", &a,&b,&mod)) {printf ("%i64d\n", Mod_pro (A, B , MoD));} return 0;}

  

acm:a^b%p-number theory-fast power-fast multiplication

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.