zoukankan      html  css  js  c++  java
  • [LintCode] Fast Power

    Calculate the (a^n) % b where a, b and n are all 32bit integers.

    Example

    For 231 % 3 = 2

    For 1001000 % 1000 = 0

     A straightforward solution is to multiply a by itself n - 1 times, then modularize the result by b. It takes O(n) runtime if we 

    consider each single arithmetic opertaion O(1) operation.

    A better approach is to use a divide and conquer method:

    x^n = x^(n/2) * x^(n/2), if n is even;

    x^n = x^(n/2) * x^(n/2) * x, if n is odd;

    T(n) = T(n/2) + O(1).  O(logn) runtime

    The following implementation uses this log algorithm. However,  it does not work correctly when n gets big.

    Since a, b, n are all 32 bits integers, so n can get really big, thus x^n become humongous, even a double type can't 

    contain all bits. In this case, x^n will be truncated to a 32 bit integer, causing incorrect result.

     1 public class Solution {
     2     public int fastPower(int a, int b, int n) {
     3         if(n == 0) {
     4             return 1 % b;
     5         }
     6         if(n == 1) {
     7             return a % b;
     8         }
     9         return (int)(power(a, n) % b);
    10     }
    11     private double power(int x, int n) {
    12         if(n == 0) {
    13             return 1;
    14         }
    15         double half = power(x, n / 2);
    16         if(n % 2 == 0) {
    17             return half * half;
    18         }
    19         return half * half * x;
    20     }
    21 }

    To fix the overflow issue, we need to modular arithmetic distributive property: (x*y) % N = ((x % N) * (y % N)) % N.

    Apply the % b operation when calculating the intermediate result. Because b is an integer, any number that gets % b

    is going to be inside an integer range.

    The following implementation shows this fix.

    However, it still has a hidden bug at lines 11 and 13.

    At line 10, each returned "product" is within integer range now, but when multiplying two integers that can be big, 

    it is possible that this multiplication overflows. Thus we need to declare the return type as long, so it gives room

    for possible overflow multiplication. The intermediate multiplications are modularized by b, ensuring the result within

    integer range. So we can safely convert from long to int.

     1 class Solution {
     2     public int fastPower(int a, int b, int n) {
     3         if (n == 1) {
     4             return a % b;
     5         }
     6         if (n == 0) {
     7             return 1 % b;
     8         }
     9         
    10         int product = fastPower(a, b, n / 2);
    11         product = (product * product) % b;
    12         if (n % 2 == 1) {
    13             product = (product * a) % b;
    14         }
    15         return (int) product;
    16     }
    17 }
     1 class Solution {
     2     public int fastPower(int a, int b, int n) {
     3         if (n == 1) {
     4             return a % b;
     5         }
     6         if (n == 0) {
     7             return 1 % b;
     8         }
     9         
    10         long product = fastPower(a, b, n / 2);
    11         product = (product * product) % b;
    12         if (n % 2 == 1) {
    13             product = (product * a) % b;
    14         }
    15         return (int) product;
    16     }
    17 }

    Modular arithmetic properties 

    Associativity: (x + (y + z)) % N = ((x + y) % N + z % N) % N;

    Commutativity: (x * y) % N = ((x % N) * (y % N)) % N; 

    Distributivity: (x * (y + z)) % N = ((x * y) % N + (x * z) % N) % N;

  • 相关阅读:
    运筹学 CheatSheet
    东南大学 2021 年夏季赛部分题解
    信号与系统期末复习精要
    信号量的基本同步模式
    OpenMP入门:求pi
    肉眼可见的 Z 变换性质
    操作系统概念 第7章 死锁
    操作系统概念 第10章 文件系统接口
    操作系统概念 第9章 虚拟内存
    操作系统概念 第8章 内存管理
  • 原文地址:https://www.cnblogs.com/lz87/p/7524329.html
Copyright © 2011-2022 走看看