zoukankan      html  css  js  c++  java
  • POJ.1995 Raising Modulo Numbers (快速幂)

    POJ.1995 Raising Modulo Numbers (快速幂)

    提议分析

    快速幂裸题
    分别给出递归写法和位运算写法。
    感觉位运算应该会更快一点,实际上这两个跑的一样快。不知道为什么(摊手

    代码总览

    #include <iostream>
    #include <cstdio>
    #include <algorithm>
    #include <cstring>
    #include <sstream>
    #include <set>
    #include <map>
    #include <queue>
    #include <stack>
    #include <cmath>
    #define INF 0x3f3f3f3f
    #define nmax 200
    #define MEM(x) memset(x,0,sizeof(x))
    using namespace std;
    //long long pow_mod(int a, int n, int m)
    //{
    //    if(n == 0) return 1;
    //    long long  x = pow_mod(a,n/2,m);// 每次砍一半
    //    long long ans = x * x % m;
    //    if(n%2 == 1) ans *= a % m;
    //    return ans;
    //}
    int qpow_mod(int a,int n, int m)
    {
        int ans = 1;
        while(n){
            if(n&1) ans = ans * a % m;
            n >>= 1;
            a = ((a%m)*(a%m))%m;
        }
        return ans;
    }
    int main()
    {
        //freopen("in.txt","r",stdin);
        int t;
        scanf("%d",&t);
        while(t--){
            int m,n;
            scanf("%d %d",&m,&n);
            int ans = 0;
            for(int i = 0; i<n;++i){
                int a,b;
                scanf("%d%d",&a,&b);
                ans+= qpow_mod(a,b,m);
                ans = ans % m;
            }
            printf("%d
    ",ans);
    
        }
        return 0;
    }
    
  • 相关阅读:
    再见2011,展望2012!
    软件测试经验分享
    常用window命令
    测试报告编写
    酒桌上的计算机网络
    OA压力测试案例
    FileSystemObject和Folders使用详细介绍
    关于盘点的问题汇总
    PB 图表数据窗口操作
    sqlserver2005中行转列的方法
  • 原文地址:https://www.cnblogs.com/pengwill/p/7367092.html
Copyright © 2011-2022 走看看