Problem:
Power
For given integers
m
and
n
, compute
m
n
(mod 1,000,000,007). Here,
A
(mod
M
) is the remainder when
A
is divided by
M
.
Input
m
n
Two integers
m
and
n
are given in a line.
Output
Print
m
n
(mod 1,000,000,007) in a line.
Constraints
1 ≤
m
≤ 100
1 ≤
n
≤ 10
9
Sample Input 1
2 3
Sample Output 1
8
Sample Input 2
5 8
Sample Output 2
390625

Submitted solutions:

code_snippet: // This is free and unencumbered software released into the public domain.

// Anyone is free to copy, modify, publish, use, compile, sell, or
// distribute this software, either in source code form or as a compiled
// binary, for any purpose, commercial or non-commercial, and by any
// means.

// In jurisdictions that recognize copyright laws, the author or authors
// of this software dedicate any and all copyright interest in the
// software to the public domain. We make this dedication for the benefit
// of the public at large and to the detriment of our heirs and
// successors. We intend this dedication to be an overt act of
// relinquishment in perpetuity of all present and future rights to this
// software under copyright law.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
// IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
// OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.

// For more information, please refer to <http://unlicense.org>

/****************/
/* template.hpp */
/****************/

#include <cassert>
#include <iomanip>
#include <iostream>
#include <limits>
#include <sstream>

using namespace std;


template <typename T> string debug_v_(T x) {
  stringstream ss;
  ss << "[";
  bool first = true;
  for (const auto &itr : x) {
    if (!first) {
      ss << ", ";
    }
    ss << itr;
    first = false;
  }
  ss << "]";
  return ss.str();
}


struct Initializer {
  Initializer() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    cout << fixed << setprecision(15) << boolalpha;
  }
} initializer;

template <typename T, typename S> bool chmin(T &a, const S &b) {
  return a > b ? a = b, true : false;
}

template <typename T, typename S> bool chmax(T &a, const S &b) {
  return a < b ? a = b, true : false;
}

template <typename T> constexpr T inf() {
  return numeric_limits<T>::max() / 2 - 1;
}

/********************/
/* math/inverse.hpp */
/********************/

#include <vector>

class Inverse {
private:
  int64_t mod;
  vector<int64_t> inv;

public:
  Inverse() {}

  Inverse(int64_t mod, int64_t n = 1000000) : mod(mod), inv(n, 1) {
    for (int i = 2; i < n; ++i) {
      inv[i] = inv[mod % i] * (mod - mod / i) % mod;
    }
  }

  int64_t operator()(int64_t a) const {
    if (a < int(inv.size())) {
      return inv[a];
    }
    int64_t b = mod, x = 1, y = 0;
    while (b) {
      int64_t t = a / b;
      swap(a -= t * b, b);
      swap(x -= t * y, y);
    }
    return x < 0 ? x + mod : x;
  }
};

int64_t inverse(int64_t n, int64_t mod) {
  Inverse inv(mod, 0);
  return inv(n);
}

/*****************/
/* math/mint.hpp */
/*****************/

class Mint {
private:
  static int64_t mod;
  static Inverse inverse;
  int64_t val;

public:
  Mint() : val(0) {}

  Mint(const int64_t &val) {
    this->val = val % mod;
    if (this->val < 0) {
      this->val += mod;
    }
  }

  static void setMod(const int64_t &m) {
    mod = m;
    inverse = Inverse(m);
  }

  Mint operator-() const { return Mint(val ? mod - val : 0); }

  Mint &operator+=(const Mint &m) {
    val += m.val;
    if (val >= mod) {
      val -= mod;
    }
    return *this;
  }

  Mint operator+(const Mint &m) const {
    int64_t ret = val + m.val;
    if (ret >= mod) {
      ret -= mod;
    }
    return ret;
  }

  Mint &operator-=(const Mint &m) {
    val -= m.val;
    if (val < 0) {
      val += mod;
    }
    return *this;
  }

  Mint operator-(const Mint &m) const {
    int64_t ret = val - m.val;
    if (ret < 0) {
      ret += mod;
    }
    return ret;
  }

  Mint &operator*=(const Mint &m) {
    val *= m.val;
    val %= mod;
    return *this;
  }

  Mint operator*(const Mint &m) const {
    return val * m.val % mod;
  }

  Mint &operator/=(const Mint &m) {
    val *= inverse(m.val);
    val %= mod;
    return *this;
  }

  Mint operator/(const Mint &m) const {
    return val * inverse(m.val) % mod;
  }

  bool operator==(const Mint &m) const { return val == m.val; }

  Mint &operator++() { return *this += 1; }

  Mint &operator--() { return *this -= 1; }

  explicit operator char() const { return val; }

  explicit operator int() const { return val; }

  explicit operator int64_t() const { return val; }

  static Mint identity() { return 1; }

  friend istream& operator>>(istream& is, Mint& m) {
    is >> m.val;
    return is;
  }
};

int64_t Mint::mod = 998244353;
Inverse Mint::inverse(998244353);

std::ostream &operator<<(std::ostream &os, Mint a) {
  os << int64_t(a);
  return os;
}

Mint operator+(const int &n, const Mint &m) { return m + n; }

Mint operator-(const int &n, const Mint &m) { return -m + n; }

Mint operator*(const int &n, const Mint &m) { return m * n; }

Mint operator/(const int &n, const Mint &m) { return Mint(n) / m; }

Mint operator+(const int64_t &n, const Mint &m) { return m + n; }

Mint operator-(const int64_t &n, const Mint &m) { return -m + n; }

Mint operator*(const int64_t &n, const Mint &m) { return m * n; }

Mint operator/(const int64_t &n, const Mint &m) { return Mint(n) / m; }
/****************/
/* math/pow.hpp */
/****************/

template<typename, typename = std::void_t<>> struct has_identity : std::false_type {};
template<typename T> struct has_identity<T, std::void_t<decltype(std::declval<T>().identity())>> : std::true_type {};
template<typename, typename = std::void_t<>> struct has_identity_with_size : std::false_type {};
template<typename T> struct has_identity_with_size<T, std::void_t<decltype(std::declval<T>().identity(0))>> : std::true_type {};

template <typename T> T identity_element(const T &m) {
  if constexpr (has_identity<T>::value) {
    return T::identity();
  } else if constexpr (has_identity_with_size<T>::value) {
    return T::identity(m.size());
  } else {
    return 1;
  }
}

template <typename T> T pow(const T &m, int64_t n) {
  if (n == 0) {
    return identity_element<T>(m);
  } else if (n < 0) {
    return identity_element<T>(m) / pow(m, -n);
  }
  T mm = pow(m, n / 2);
  mm *= mm;
  if (n % 2) {
    mm *= m;
  }
  return mm;
}

/************/
/* main.cpp */
/************/

int main() {
  Mint::setMod(1000000007);
  Mint m;
  int n;
  cin >> m >> n;
  cout << pow(m, n) << endl;
}
accuracy: 1
time_ms: 10
memory_kb: 18808
final_rank: 1

code_snippet: /*****************************************************************//*!
Function
２つの整数 m, nについて、m^nを1,000,000,007で割った余りを求めてください。

入力
2つの整数 m, n が１つの空白区切りで１行に与えられます。

出力
m^nを1,000,000,007で割った余りを１行に出力します。
*//*****************************************************************/

#define _USE_MATH_DEFINES // for C++
#include<iostream>
#include<vector>
#include<sstream>
#include<cmath>
#include<iomanip>
#include<stdexcept>
#include<map>
#include<cctype>

int main(void)
{
    int den = 1000000007; //割る数(denominator(分母))
    long long m, n;     //m: 底、 n: 指数 
    std::cin >> m >> n;

    /******アルゴリズム******//*!
    1. m^(count) > den を満たすまで冪乗を行う。(count <= n)
    2. m^(count) % den = d とすると、{(m^(count))^b} % den = d^bとなる。 (b: (b*count) < n を満たす最大のb)
       また、冪乗されていない残りの指数を s とする。
       式で表すと、(n = count * b + s)、(m^n = (d^b) * (m^s))となる。
       これを計算するためのd, b, sを求める。
    3. 数値のオーバーフロー防止のため、パワー関数ではなくfor文で乗算し、逐次 den による余剰を求める。
    */

    int count = 0;      //現在何乗されたか
    long long  ans = 1;       //冪乗の途中経過
    bool flag = false;  //m^nがdenより大きいか否か

    //lより乗算が大きくなるまで回す
    do
    {
        count++;
        ans *= m;
        if (ans > den)
        {
            flag = true;
            break;
        }
    } while (count < n);

    //denよりansが大きくなった場合、その余りを求めて計算効率を上げる(小さい場合ansがそのまま余りになる)
    int d;  //ans%denを格納
    int b;  //dの冪(べき)
    int s;  //残りの冪
    if (flag)
    {
        d = ans % den;
        b = n / count;
        s = n % count;

        ans = d;

        //std::cout << "count:" << count << "\td:" << d << "\t" << "b:" << b << "\t" << "s:" << s << "\n";

        //アルゴリズム 3.の計算
        for (int i = 1; i < b; i++)
        {
            ans *= d;
            ans %= den;
        }

        for (int j = 0; j < s; j++)
        {
            ans *= m;
            ans %= den;
        }
    }

    std::cout << ans%den << std::endl;

    return 0;
}
accuracy: 1
time_ms: 640
memory_kb: 3440
final_rank: 3

code_snippet: #define _USE_MATH_DEFINES // for C++
#include<iostream>
#include<vector>
#include<sstream>
#include<cmath>
#include<iomanip>
#include<stdexcept>
#include<map>
#include<cctype>

int main(void)
{
    long long m, n;     //m: 底、 n: 指数 
    std::cin >> m >> n;

    int count = 0;      //現在何乗されたか
    long ans = 1;       //乗算の途中結果
    int l = 1000000007; //割る数
    bool flag = false;  //左項がlより大きいか否か

    //lより乗算が大きくなるまで回す
    do
    {
        count++;
        ans *= m;
        if (ans > l)
        {
            flag = true;
            break;
        }
    } while (count < n);

    //lよりansが大きくなった場合、その余りを求めて計算効率を上げる(小さい場合ansがそのまま余りになる)
    int d;  //ans%lを格納
    int b;  //dの冪(べき)
    int s;  //残りの冪
    if (flag)
    {
        d = ans % l;
        b = n / count;
        s = n % count;

        ans = d;
        for (int i = 1; i < b; i++)
        {
            ans *= d;
            ans %= l;
        }


        for (int j = 0; j < s; j++)
        {
            ans *= m;
            ans %= l;
        }
    }

    std::cout << ans%l << std::endl;

    return 0;
}
accuracy: 1
time_ms: 640
memory_kb: 3368
final_rank: 1