結果

問題 No.167 N^M mod 10
ユーザー hanorverhanorver
提出日時 2016-04-15 16:09:26
言語 C++11
(gcc 11.4.0)
結果
TLE  
実行時間 -
コード長 740 bytes
コンパイル時間 548 ms
コンパイル使用メモリ 61,052 KB
実行使用メモリ 13,884 KB
最終ジャッジ日時 2024-04-15 01:46:17
合計ジャッジ時間 2,914 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include<iostream>
#include<string>
#include<cmath>

// 一桁の数 n が何乗するとループするか
int calc_loop_count(int n) {
	for (int i = 2; i < 100; i++) {
		int num = std::pow(n, i);
		if (num % 10 == n) {
			return i;
		}
	}
	return 1;
}

int remainder(int n, std::string p) {
	int ans = 1;
	for (int i = 0; !p.empty(); i++) {
		int val = p.back() - '0';
		p.pop_back();
		int loop = pow(10, i);
		for (int j = 0; j < loop; j++) {
			int mul = pow(n, val);
			ans = (ans * mul) % 10;
		}
	}
	return ans;
}

int main() {
	std::string n, m;

	std::cin >> n >> m;

	int num = n.back() - '0'; // 最後の一桁を取り出す

	int loop_count = calc_loop_count(num);

	std::cout << remainder(num, m) << std::endl;

	return 0;
}
0