前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >挑战程序竞赛系列(15):2.6快速幂运算

挑战程序竞赛系列(15):2.6快速幂运算

作者头像
用户1147447
发布2019-05-26 09:54:21
4030
发布2019-05-26 09:54:21
举报
文章被收录于专栏:机器学习入门

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://cloud.tencent.com/developer/article/1434818

挑战程序竞赛系列(15):2.6快速幂运算

详细代码可以fork下Github上leetcode项目,不定期更新。

练习题如下:

POJ 3641: Pseudoprime numbers

判断在当前基数a时,满足费马小定理的伪素数。

代码如下:

代码语言:javascript
复制
public static void main(String[] args) throws IOException {
        Scanner in = new Scanner(System.in);
        while (true) {
            int p = in.nextInt();
            int a = in.nextInt();
            if (p == 0 && a == 0)
                break;
            if (a != quickPow(a, p, p)){
                System.out.println("no");
                continue;
            }
            if (isPrime(p)) System.out.println("no");
            else System.out.println("yes");
        }
    }

    public static boolean isPrime(int num){
        for (int i = 2; i * i <= num; ++i){
            if (num % i == 0) return false;
        }
        return true;
    }

    public static long quickMul(long a, long b, long mod) {
        long ans = 0;
        while (b != 0) {
            if ((b & 1) != 0) {
                ans = (ans + a) % mod;
            }
            b >>= 1;
            a = (a + a) % mod;
        }
        return ans;
    }

    public static long quickPow(long a, long b, long mod) {
        long ans = 1;
        while (b != 0) {
            if ((b & 1) != 0) {
                ans = quickMul(ans, a, mod);
            }
            b >>= 1;
            a = quickMul(a, a, mod);
        }
        return ans;
    }

POJ 1995: Raising Modulo Numbers

取模运算,水题。

代码如下:

代码语言:javascript
复制
public static void main(String[] args) throws IOException {
        Scanner in = new Scanner(System.in);
        int Z = in.nextInt();
        while ((Z--) != 0){
            int M = in.nextInt();
            int N = in.nextInt();
            long ans = 0;
            for (int i = 0; i < N; ++i){
                long a = in.nextLong();
                long b = in.nextLong();
                ans = (ans + quickPow(a, b, M)) % M;
            }
            System.out.println(ans);
        }
    }

    public static long quickMul(long a, long b, long mod){
        long res = 0;
        while (b != 0){
            if ((b & 1) != 0){
                res = (res + a) % mod;
            }
            b >>= 1;
            a = (a + a) % mod;
        }
        return res;
    }

    public static long quickPow(long a, long b, long mod){
        long res = 1;
        while (b != 0){
            if ((b & 1) != 0){
                res = quickMul(res, a, mod);
            }
            b >>= 1;
            a = quickMul(a, a, mod);
        }
        return res;
    }
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2017年06月21日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 挑战程序竞赛系列(15):2.6快速幂运算
    • POJ 3641: Pseudoprime numbers
      • POJ 1995: Raising Modulo Numbers
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档