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;
    }
    
  • 相关阅读:
    Kafka架构
    MapReduce执行流程解析
    ZooKeeper选举机制
    Zookeeper全局一致性
    HDFS的快照
    在CentOS 6.5上安装NodeJS
    Node v0.12.5 稳定版发布
    CentOS6.5手动升级gcc4.8.2
    centos6 yum 安装 install c++4.8 gcc4.8
    Linux CentOS6系统安装最新版本Node.js环境及相关文件配置
  • 原文地址:https://www.cnblogs.com/pengwill/p/7367092.html
Copyright © 2011-2022 走看看