2

I have already tried modPow() function for BigInteger in java.
But it needed too long.

I know the modular multiplication, even exponentiation too.

But I am unable solve this problem because of constraints.

a, b have values which can have 1000000 digits in it (so huge, is it not)? Now I want to find (a**b)%c. As a first step,we can do a = a % c. But here, even b is so huge.

c = 10^9+7

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
Bhavesh Munot
  • 675
  • 1
  • 6
  • 13
  • 1
    Not sure what TLE is in this context? – Louis Wasserman Feb 02 '15 at 20:16
  • libgmp has modular exponentation function, `mpz_powm` – André Puel Feb 02 '15 at 20:16
  • 2
    The standard algorithm for modular exponentiation can handle million-digit exponents without much difficulty. It certainly won't be the slowest thing you'll be doing with `b`. – Sneftel Feb 02 '15 at 20:17
  • @LouisWasserman Probably 'Time Limit Exceeded' – user4520 Feb 02 '15 at 20:17
  • TLE is Time Limit Exceeded. It is a problem on HackerEarth competitive programming website. – Bhavesh Munot Feb 02 '15 at 20:18
  • This is standard for RSA, right? One way to do this is to split b into its bit representations. Then compute a^(bj) mod c for each bit. Noting that as the bit position increases, you can save by doing a=(a*a) mod c. – thang Feb 02 '15 at 20:27
  • And as I suspected, this is a dup: http://stackoverflow.com/questions/8496182/calculating-powa-b-mod-n. This specific problem is standard for RSA, so it must have been asked a ton of times. Not surprisingly, I was right! :p – thang Feb 02 '15 at 20:30
  • Can you use BigInteger, and instead of computing the power directly, do a loop of multiplying by a and modulusing by c for a times? – Brian J Feb 02 '15 at 20:30
  • 1
    @BrianJ: Doesn't `modPow` do that efficiently already? – Deduplicator Feb 02 '15 at 20:33
  • 1
    In which language? Java or C? Or both? Moral about TLE: don't use TLAs. – user207421 Feb 02 '15 at 20:34
  • @Deduplicator I misread the question the first time. I thought modPow and BigInteger were separate techniques he tried, not a method of the BigInteger class. – Brian J Feb 02 '15 at 20:35
  • @EJP: The dup-target you chose. It shows the general case for calculating modular power, but OP is already using a pre-made function doing that. His problem is, the general way is too slow. There's a far more efficient method, *especially* if `c` is constant and so relatively small. Which are both requirements not valid on the dupe-target. – Deduplicator Feb 02 '15 at 20:47
  • @Deduplicator, I am interested in this "far more efficient method". Can you elaborate? – thang Feb 02 '15 at 20:54
  • 2
    @thang: Sure. Take a look at [euler's theorem](http://en.wikipedia.org/wiki/Euler%27s_totient_function#Euler.27s_theorem). As `c` is managably small, it's easily factorized and thus we can use that theorem to reduce the exponent. Whereafter the general `modPow`-function is easily fast enough. – Deduplicator Feb 02 '15 at 20:57
  • @Deduplicator, how fast is it to prime factorize a number of order 10^9+7? About O(10^9) right? Schneider's algorithm runs in log b iterations. For log b to be > 10^9, you'd need b to be 10^9 bits long, which is a 120MB number! Maybe there is a trick you're thinking about that escaped me... please elaborate. – thang Feb 02 '15 at 21:05
  • @thang: `10^9+7` is the exact number, not it's length according to the OP. – Deduplicator Feb 02 '15 at 21:08
  • @Deduplicator, yes, what is the run time to prime factorize a number? About order of the number itself. It's actually polynomial time with respect tot he number itself. Some of the better algorithms reduced the exponent of the polynomial down to < 1, but even then, it's still huge. – thang Feb 02 '15 at 21:08
  • 1
    @thang: Prime-factoring is sub-exponential in the bitlength. But that does not really matter so much here. Remember `c` is constant. Anyway, a first step is primality-testing, which is far cheaper. – Deduplicator Feb 02 '15 at 21:16
  • @Deduplicator, what's a sub-exponential prime factorization algorithm? The first step is cheap, but then the second step is slow.... To solve the problem the way you proposed, you need to do all the steps. – thang Feb 02 '15 at 23:27
  • 1
    @Deduplicator Just to short circuit all that, `10^9 + 7` is prime. – Sneftel Feb 03 '15 at 09:21
  • possible duplicate of [How to calculate the mod of large exponents?](http://stackoverflow.com/questions/20410389/how-to-calculate-the-mod-of-large-exponents) – Joe Feb 03 '15 at 09:24

1 Answers1

0

Try using this function. It's using some JS engine to calculate anything. Put expression as the String parameter and run it.

private void calculateExpression(String exp){

    try {
        //calling a JS engine which helps us to calculate what we need
        ScriptEngineManager mgr = new ScriptEngineManager();
        ScriptEngine engine = mgr.getEngineByName("JavaScript");
        Object result = engine.eval(exp);

        //checking if we got any error
        if (result.toString().equals("Infinity") || result.toString().equals("NaN")){
            System.out.println("Can't divide with zero");
        }           
        else if (result != null){
            Double doubleResult = new Double("" + result);

            //checking if the result is an int value
            if ((doubleResult == Math.floor(doubleResult)) && !Double.isInfinite(doubleResult)) {
                text.setText("" + (doubleResult.longValue()));
            }
            else {
                //means that it's double value
                text.setText(result.toString());
            }
        }
    }
    catch (Exception e) { 
        e.printStackTrace();
    }
}
roeygol
  • 4,908
  • 9
  • 51
  • 88
  • Sorry, but that's plain too damn slow. So even if it would deliver the result sometime, that's far too late. – Deduplicator Feb 03 '15 at 15:01
  • Ok, but did it make the solution you were needed for ? – roeygol Feb 03 '15 at 15:50
  • **I** didn't need it. And the straight-forward algorithm is too slow according to OP. Look at the comments, I hinted at the right solution there. You are welcome to use it all. – Deduplicator Feb 03 '15 at 15:58