結果

問題 No.167 N^M mod 10
ユーザー not_522not_522
提出日時 2015-07-28 10:29:38
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 70 ms / 1,000 ms
コード長 3,863 bytes
コンパイル時間 1,593 ms
コンパイル使用メモリ 165,020 KB
実行使用メモリ 19,024 KB
最終ジャッジ日時 2023-10-21 23:51:05
合計ジャッジ時間 4,728 ms
ジャッジサーバーID
(参考情報)
judge14 / judge15
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 67 ms
19,024 KB
testcase_01 AC 65 ms
19,024 KB
testcase_02 AC 67 ms
19,024 KB
testcase_03 AC 67 ms
19,024 KB
testcase_04 AC 67 ms
19,024 KB
testcase_05 AC 66 ms
19,024 KB
testcase_06 AC 69 ms
19,024 KB
testcase_07 AC 69 ms
19,024 KB
testcase_08 AC 68 ms
19,024 KB
testcase_09 AC 67 ms
19,024 KB
testcase_10 AC 67 ms
19,024 KB
testcase_11 AC 68 ms
19,024 KB
testcase_12 AC 68 ms
19,024 KB
testcase_13 AC 67 ms
19,024 KB
testcase_14 AC 67 ms
19,024 KB
testcase_15 AC 69 ms
19,024 KB
testcase_16 AC 68 ms
19,024 KB
testcase_17 AC 68 ms
19,024 KB
testcase_18 AC 70 ms
19,024 KB
testcase_19 AC 65 ms
19,024 KB
testcase_20 AC 66 ms
19,024 KB
testcase_21 AC 64 ms
19,024 KB
testcase_22 AC 68 ms
19,024 KB
testcase_23 AC 67 ms
19,024 KB
testcase_24 AC 65 ms
19,024 KB
testcase_25 AC 64 ms
19,024 KB
testcase_26 AC 64 ms
19,024 KB
testcase_27 AC 64 ms
19,024 KB
testcase_28 AC 66 ms
19,024 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>

using namespace std;

namespace arithmetic {
  template<typename T> class Addition {
  public:
    template<typename V> T operator+(const V& v) const {
      return T(static_cast<const T&>(*this)) += v;
    }
  };

  template<typename T> class Subtraction {
  public:
    template<typename V> T operator-(const V& v) const {
      return T(static_cast<const T&>(*this)) -= v;
    }
  };

  template<typename T> class Multiplication {
  public:
    template<typename V> T operator*(const V& v) const {
      return T(static_cast<const T&>(*this)) *= v;
    }
  };

  template<typename T> class Division {
  public:
    template<typename V> T operator/(const V& v) const {
      return T(static_cast<const T&>(*this)) /= v;
    }
  };

  template<typename T> class Modulus {
  public:
    template<typename V> T operator%(const V& v) const {
      return T(static_cast<const T&>(*this)) %= v;
    }
  };
}

template<typename T> class IndivisibleArithmetic : public arithmetic::Addition<T>, public arithmetic::Subtraction<T>, public arithmetic::Multiplication<T> {};

template<typename T> class Arithmetic : public IndivisibleArithmetic<T>, public arithmetic::Division<T> {};

class Inverse {
private:
  long long mod;
	vector<long long> inv;
  
public:
  Inverse() {}
  
	Inverse(long long mod, long long n = 1000000) : mod(mod) {
    inv = vector<long long>(n, 1);
    for (int i = 2; i < n; ++i) inv[i] = inv[mod % i] * (mod - mod / i) % mod;
  }
  
	long long operator()(long long a) const {
		if (a < (int)inv.size()) return inv[a];
		long long b = mod, x = 1, y = 0;
		while (b) {
			long long t = a / b;
			swap(a -= t * b, b);
			swap(x -= t * y, y);
		}
		return (x %= mod) < 0 ? x + mod : x;
	}
};

class Mint : public Arithmetic<Mint> {
private:
  static long long mod;
  static Inverse inverse;
  long long val;
	
public:
	Mint() {}

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

  static void setMod(const long long& m) {
    mod = m;
    inverse = Inverse(m);
  }
	
	Mint operator+=(const Mint& m) {
		val += m.val;
		if (val >= mod) val -= mod;
		return *this;
	}
  
	Mint operator-=(const Mint& m) {
		val -= m.val;
		if (val < 0) val += mod;
		return *this;
	}
  
	Mint operator*=(const Mint& m) {
		val *= m.val;
		val %= mod;
		return *this;
	}
  
	Mint operator/=(const Mint& m) {
		val *= inverse(m.val);
		val %= mod;
		return *this;
	}
	
	Mint operator++() {return val += 1;}
	
	operator long long() {
		return val;
	}

  Mint identity() const {
    return 1;
  }
};

long long Mint::mod = 1000000007;
Inverse Mint::inverse(1000000007);

ostream& operator<<(ostream& os, Mint a) {
	os << (long long)a;
	return os;
}

istream& operator>>(istream& is, Mint& a) {
	long long n;
	is >> n;
	a = n;
	return is;
}

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

template<typename T> inline T toInteger(const string&);

template<> inline int toInteger<int>(const string& s) {
  return stoi(s);
}

template<> inline long toInteger<long>(const string& s) {
  return stol(s);
}

template<> inline long long toInteger<long long>(const string& s) {
  return stoll(s);
}

template<typename T = long long> inline T toInteger(const string& s, int n) {
  T res = 0;
  for (char c : s) {
    if (isdigit(c)) res = res * n + c - '0';
    else if (isalpha(c)) res = res * n + tolower(c) - 'a' + 10;
  }
  return s[0] == '-' ? -res : res;
}

int main() {
  string n, m;
  cin >> n >> m;
  m = "0" + m;
  Mint::setMod(10);
  Mint nn = toInteger<int>(n.substr(n.size() - 1));
  int mm = toInteger<int>(m.substr(m.size() - 2));
  if (m.size() != 2u && mm == 0) mm = 100;
  cout << pow(nn, mm) << endl;
}
0