372. Super Pow

    /*
     * 372. Super Pow
     * 2016-7-16 by Mingyang
     */
      private int mod = 1337;
        public int superPow(int a, int[] b) {
            int n = b.length;
            int ans = 1;
            for (int i = n - 1; i >= 0; i--) {
                ans = ans * quick_pow(a, b[i]) % mod;
                a = quick_pow(a, 10);
            }
            return ans;
        }
        int quick_pow(int a, int b) {
            int ans = 1;
            a %= mod;
            while (b > 0) {
                if ((b & 1) !=0) ans = ans * a % mod;
                a = a * a % mod;
                b >>= 1;
            }
            return ans;
        }
时间: 2024-10-13 09:47:59

372. Super Pow的相关文章

[Math_Medium] 372. Super Pow 2018-09-27

原题:372. Super Pow Your task is to calculate ab mod 1337 where a is a positive integer and b is an extremely large positive integer given in the form of an array. 题目大意: 给定两个数 a, b,其中 b 会非常大,因此 b 用一个 int 的 的数组给出,然后求 \(a^b\%1337\) 初始思路1: 首先 \((a*b)\%M =

LeetCode 第 372 题 (Super Pow)

LeetCode 第 372 题 (Super Pow) Your task is to calculate ab mod 1337 where a is a positive integer and b is an extremely large positive integer given in the form of an array. Example1: a = 2 b = [3] Result: 8 Example2: a = 2 b = [1,0] Result: 1024 这道题与

[LeetCode] Super Pow 超级次方

Your task is to calculate ab mod 1337 where a is a positive integer and b is an extremely large positive integer given in the form of an array. Example1: a = 2 b = [3] Result: 8 Example2: a = 2 b = [1,0] Result: 1024 Credits:Special thanks to @Stomac

LeetCode "Super Pow"

A simple math on modular calculations.Code below can be simplified of course. class Solution { const int MOD = 1337; long long _pow(int a, int p) { if(p == 0) return 1; if(p == 1) return a; long long pr = _pow(a, p/2); long long r = p % 2 ? a : 1; re

过中等难度题目.0310

  .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Medium     . 288 Unique Word Abbreviation      15.8% Medium     . 29 Divide Two Integers      16.0% Medium     . 166 Fraction to Recurring Decimal      17.

继续过中等难度.0309

  .   8  String to Integer (atoi)    13.9% Medium   . 151 Reverse Words in a String      15.7% Medium     . 288 Unique Word Abbreviation      15.8% Medium     . 29 Divide Two Integers      16.0% Medium     . 166 Fraction to Recurring Decimal      17.

LeetCode Problems List 题目汇总

No. Title Level Rate 1 Two Sum Medium 17.70% 2 Add Two Numbers Medium 21.10% 3 Longest Substring Without Repeating Characters Medium 20.60% 4 Median of Two Sorted Arrays Hard 17.40% 5 Longest Palindromic Substring Medium 20.70% 6 ZigZag Conversion Ea

【LeetCode】数学(共106题)

p.p1 { margin: 0.0px 0.0px 0.0px 0.0px; font: 12.0px Helvetica } [2]Add Two Numbers [7]Reverse Integer [8]String to Integer (atoi) [9]Palindrome Number [12]Integer to Roman [13]Roman to Integer [29]Divide Two Integers [43]Multiply Strings [50]Pow(x,

python基本用法

PYTHONPATH PYTHONPATH是python moudle的搜索路径.即import xxx会从$PYTHONPATH寻找xxx. 中文编码问题 #coding=utf-8 查看导入的包的路径 import a_module print(a_module.__file__) map(function, iterable, ...) 参数 function -- 函数 iterable -- 一个或多个序列 返回值 Python 2.x 返回列表. Python 3.x 返回迭代器.