UVa 374 big Mod: fast power modulo

Source: Internet
Author: User
Tags range time limit

374-big Mod

Time limit:3.000 seconds

Http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem &problem=310

Calculate

For large values of B, P, and M using a efficient algorithm. (That's right, this is problem has a time dependency!!!.)

Input

Three integer values (in the order B, P, M) would be read one number per line. B and P are integers in the range 0 to 2147483647 inclusive. M is a integer in the range 1 to 46340 inclusive.

Output

The result of the computation. A single integer.

Sample Input

3
18132
1765
3

2374859
3029382
36123

Sample Output


2
13195

Take a review of this function.

Complete code:

/*0.012s*/
    
#include <cstdio>  
    
int main ()  
{  
    int b, p, mod, ans;  
    while (~SCANF ("%d%d%d", &b, &p, &mod))  
    {  
        b%= mod;  
        ans = 1;  
        while (p)  
        {  
            if (P & 1)  
                ans = ans * b mod;  
            b = b * b% mod;  
            P >>= 1;  
        }  
        printf ("%d\n", ans);  
    }  
    return 0;  
}

Author Signature: CSDN blog Synapse7

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/sjjg/

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.