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

这道题与第 50 题其实大同小异。唯一一个值得注意的地方是 13363 超过了 int 型变量可以表示的整数的范围。所以我们的程序中不能出现 (a * b * c) % 1337 这样三个数字相乘的代码。

下面是我的代码:

int superPow(int a, vector<int>& b)
{
    int z = 1, a2, a4;
    int size = b.size() - 1;
    a = a % 1337;
    for(int i = size; i >=0; i--)
    {
        a2 = (a * a) % 1337;
        a4 = (a2 * a2) % 1337;
        switch(b[i])
        {
        case 1:
            z = (z * a) % 1337;
            break;
        case 2:
            z = (z * a2) % 1337;
            break;
        case 3:
            z = (z * a2) % 1337;
            z = (z * a) % 1337;
            break;
        case 4:
            z = (z * a4) % 1337;
            break;
        case 5:
            z = (z * a4) % 1337;
            z = (z * a) % 1337;
            break;
        case 6:
            z = (z * a4) % 1337;
            z = (z * a2) % 1337;
            break;
        case 7:
            z = (z * a4) % 1337;
            z = (z * a2) % 1337;
            z = (z * a) % 1337;
            break;
        case 8:
            z = (z * a4) % 1337;
            z = (z * a4) % 1337;
            break;
        case 9:
            z = (z * a4) % 1337;
            z = (z * a4) % 1337;
            z = (z * a) % 1337;
            break;
        default:
            break;
        }
        a = (a4 * a4) % 1337;
        a = (a * a2) % 1337;
    }
    return z;
}
时间: 2024-11-08 00:02:24

LeetCode 第 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 =

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; }

LeetCode 第 73 题 (Set Matrix Zeroes)

LeetCode 第 73 题 (Set Matrix Zeroes) Given a m x n matrix, if an element is 0, set its entire row and column to 0. Do it in place. Follow up: Did you use extra space? A straight forward solution using O(mn) space is probably a bad idea. A simple impro

leetcode中第一题twosum问题解答算法的可行性证明

leetcode中第一题twosum问题解答算法的可行性证明 一.引入 关于leetcode中第一题twosum问题,网上已有不少高人做出过解答,并提出了切实可行的算法实现.我在解答该题时参考了博客http://www.zixue7.com/article-9576-1.html的解答.为让读者更直观地阅读和理解本文,先简要摘录以上博客的内容如下: 题目还原 Two Sum Given an array of integers, find two numbers such that they a

Leetcode第五题_Longest Palindromic Substring

Longest Palindromic Substring Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring. Leetcode第5题,题目大概意思是给一个字符串,从中找出最长的回文串,所谓回文串,就是

LeetCode 第三题,Longest Substring Without Repeating Characters

题目: Given a string, find the length of the longest substring without repeating characters. For example, the longest substring without repeating letters for "abcabcbb" is "abc", which the length is 3. For "bbbbb" the longest s

leetcode第9题-Palindrom Number

这是leetcode的第九题,相对来说比较简单,目的很简单,就是判断一个int型的数是不是回文数.但是有几点需要考虑: 负数应该没有回文数,要加判断!要注意额外的空间申请问题.判断是否是回文数势必要对一个数进行反转,反转的时候就要考虑溢出的问题.实现的代码如下: #include<stdio.h> bool isPalindrom(int x) { if(x<0) return false; else { int tmp=x; int sum=0; while(tmp) { sum=su

LeetCode 第 19 题 (Remove Nth Node From End of List)

LeetCode 第 19 题 (Remove Nth Node From End of List) Given a linked list, remove the nth node from the end of list and return its head. For example, Given linked list: 1->2->3->4->5, and n = 2. After removing the second node from the end, the li

LeetCode 第 342 题(Power of Four)

LeetCode 第 342 题(Power of Four) Given an integer (signed 32 bits), write a function to check whether it is a power of 4. Example: Given num = 16, return true. Given num = 5, return false. Follow up: Could you solve it without loops/recursion? 题目很简单,