zoukankan      html  css  js  c++  java
  • [解题报告]374 Big Mod

     Big Mod 

    Calculate

    displaymath25

    for large values of BP, and M using an efficient algorithm. (That's right, this problem has a time dependency !!!.)

    Input

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

    Output

    The result of the computation. A single integer.

    Sample Input

    3
    18132
    17
    
    17
    1765
    3
    
    2374859
    3029382
    36123

    Sample Output

    13
    2
    13195



    关键是(A*B)%C=(A%C)*(B%C)
    #include <stdio.h>
    int bigmod( int b, int p, int m )
    {
        if(p==0) return 1;
        if (p==1) return b;
        int a=bigmod(b,p/2,m);
        if (p%2)
            return (((a*a)%m)*b)%m;
        else return (a*a)%m;
    }
    
    int main()
    {
        int b,p,m;
        while(scanf("%d%d%d",&b,&p,&m)!=EOF)
            printf("%d\n",bigmod(b%m,p,m));
        return 0;
    }
     
  • 相关阅读:
    HDOJ 1093
    HDOJ 1089
    HDOJ 1094
    qsort函数
    HDOJ 1092
    HDOJ 1091
    NYOJ 448(贪心)
    HDOJ 1090
    HDOJ 1097(幂取模)
    winform用户输入查询与拼音首字母的结合,提高用户的操作体验
  • 原文地址:https://www.cnblogs.com/TheLaughingMan/p/2924842.html
Copyright © 2011-2022 走看看