結果

問題 No.167 N^M mod 10
ユーザー @abcde@abcde
提出日時 2019-03-03 14:36:08
言語 C++11
(gcc 11.4.0)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 1,965 bytes
コンパイル時間 2,383 ms
コンパイル使用メモリ 161,452 KB
実行使用メモリ 4,348 KB
最終ジャッジ日時 2023-10-22 00:26:30
合計ジャッジ時間 2,530 ms
ジャッジサーバーID
(参考情報)
judge14 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

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

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

int main() {
    
    // 1. 入力情報取得.
    string N, M;
    cin >> N >> M;
    
    // 2. N の M乗 (mod10) の値は?
    // 2-0. 0乗は, 先に処理.
    if(M == "0"){
        cout << "1" << endl;
        return 0;
    }
    
    // 2-1. N (mod10) を 記録.
    int nLen   = N.size();
    int nMod10 = stoi(N.substr(nLen - 1, 1));

    // 2-2. M (mod4) を 記録.
    int mLen   = M.size();
    int mMod4 = 0;
    if(mLen > 1){
        int first  = stoi(M.substr(mLen - 1, 1)); // 1の位.
        int second = stoi(M.substr(mLen - 2, 1)); // 10の位.
        first--;
        if(first < 0) first += 10, second++;  // M = 20 -> M = 19 になった場合などを想定.
        if(second % 2 == 0) mMod4 = first % 4;
        if(first % 4 == 0 && second % 2 == 1) mMod4 = 2;
        if(first % 4 == 1 && second % 2 == 1) mMod4 = 3;
        if(first % 4 == 2 && second % 2 == 1) mMod4 = 0;
        if(first % 4 == 3 && second % 2 == 1) mMod4 = 1;
    }
    if(mLen == 1){
        int first  = stoi(M.substr(mLen - 1, 1)); // 1の位.
        first--;
        if(first < 0) first += 10;
        mMod4 = first % 4;
    }

    // 3. べき乗の動き.
    // 3-1. 1の位の値で分岐.
    int pow[10][4] = {{0, 0, 0, 0},
    {1, 1, 1, 1},
    {2, 4, 8, 6},
    {3, 9, 7, 1},
    {4, 6, 5, 6},
    {5, 5, 5, 5},
    {6, 6, 6, 6},
    {7, 9, 3, 1},
    {8, 4, 2, 6},
    {9, 1, 9, 1}
    };

    // 3-2. N (mod10) を M乗した場合 の 1の位の値.
    int ans = 0;
    if(nMod10 == 0) ans = 0;
    if(nMod10 == 1) ans = 1;
    if(nMod10 == 2) ans = pow[2][mMod4];
    if(nMod10 == 3) ans = pow[3][mMod4];
    if(nMod10 == 4) ans = pow[4][mMod4];
    if(nMod10 == 5) ans = 5;
    if(nMod10 == 6) ans = 6;
    if(nMod10 == 7) ans = pow[7][mMod4];
    if(nMod10 == 8) ans = pow[8][mMod4];
    if(nMod10 == 9) ans = pow[9][mMod4];
    
    // 4. 出力.
    cout << ans << endl;
    return 0;
    
}
0