简体   繁体   English

Java中的模幂问题

[英]Modular Exponentiation Issue in Java

import java.util.Scanner;

class codeabbey145 
{
    public static void main(String[] Args)  
    {
        Scanner input = new Scanner(System.in);
        double A = 0;
        double B = 0;
        double M = 0;
        System.out.println("\n\nHow many sets?");
        int s = input.nextInt();
        double X[] = new double[s];
        for(int i = 0; i<s; i++)
        {
            System.out.println("A: ");
            A = input.nextDouble();
            System.out.println("B: ");
            B = input.nextDouble();
            System.out.println("M: ");
            M = input.nextDouble();
            X[i] = (Math.pow(A, B)) % M;  //(A^B)%M
        }
        for(int j = 0; j<s; j++)
        {
            System.out.print(Math.round(X[j]) + " ");
        }
    }
}

I have been attempting to complete Exercise 145 on Codeabbey.com 我一直试图在Codeabbey.com上完成练习145

The formula given for modular exponentiation is: (A^B)%M 模幂的给定公式为:(A ^ B)%M

I tried my best to implement this formula into my code, but the answers I have been getting are incorrect. 我尽力将这个公式实现到代码中,但是得到的答案不正确。 Anybody know why this might be? 有人知道为什么会这样吗?

Thanks in advance 提前致谢

You code is Absolutely correct : Check here . 您的代码是完全正确的: 在此处检查。

Probably you should use BigInteger: for handling large numbers , double is never recommended. 可能应该使用BigInteger:处理大量数字时,绝对不建议使用double。

Here is the working example: check this live demo 这是工作示例:检查此现场演示

Code

public static void main(String[] Args) {
        Scanner input = new Scanner(System.in);

        System.out.println("\n\nHow many sets?");
        int s = input.nextInt();
        BigInteger[] X = new BigInteger[s];
        for (int i = 0; i < s; i++) {
            System.out.println("A: ");
            BigInteger A = input.nextBigInteger();
            System.out.println("B: ");
            BigInteger B = input.nextBigInteger();
            System.out.println("M: ");
            BigInteger M = input.nextBigInteger();
            X[i] = A.modPow(B, M); //(A^B)%M
        }
        for (int i = 0; i < X.length; i++) {
            System.out.println(X[i]);
        }
    }

I have attempted this question on CodeAbbey . 我在CodeAbbey上尝试了这个问题。 My solution was accepted. 我的解决方案被接受了。

解决方案被接受

Code: 码:

import java.util.*;
import java.lang.*;
import java.io.*;
import java.math.BigInteger;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
    public static void main(String[] Args) {
        Scanner input = new Scanner(System.in);
        int s = input.nextInt();
        BigInteger[] X = new BigInteger[s];
        for (int i = 0; i < s; i++) {
            BigInteger A = input.nextBigInteger();
            BigInteger B = input.nextBigInteger();
            BigInteger M = input.nextBigInteger();
            X[i] = A.modPow(B, M);
        }
        for (int i = 0; i < X.length; i++) {
            System.out.println(X[i]+" ");
        }
    }
}

Most likely the values are too big to fit into a double , so they overflow. 该值很可能太大而无法容纳double ,因此它们会溢出。 Your best bet is probably to implement using BigInteger objects. 最好的选择可能是使用BigInteger对象来实现。

BigInteger has pow and mod functions that you can use for your calculations. BigInteger具有powmod函数,可用于计算。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM