結果

問題 No.167 N^M mod 10
ユーザー kpinkcat
提出日時 2023-11-04 10:55:26
言語 C++17
(gcc 13.3.0 + boost 1.87.0)
結果
AC  
実行時間 4 ms / 1,000 ms
コード長 947 bytes
コンパイル時間 952 ms
コンパイル使用メモリ 107,284 KB
最終ジャッジ日時 2025-02-17 19:08:59
ジャッジサーバーID
(参考情報)
judge1 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<iostream>
#include<iomanip>
#include<string>
#include<algorithm>
#include<vector>
#include<set>
#include<list>
#include<queue>
#include<math.h>
#include<bitset>
using ll = long long;
using namespace std;

int div(string s, int div){
    int i;
    i = stoi(s.substr(0, 1));
    s = s.substr(1);
    while (s.size()){
        i *= 10;
        i += stoi(s.substr(0, 1));
        s = s.substr(1);
        i %= div;
    }
    return i;
}

int main(){
    string n, m;
    cin >> n >> m;
    int t1 = n[n.size()-1] - '0';
    ll r, ans = t1;
    vector<ll> modulo{1, 1, 4, 4, 2, 1, 1, 4, 4, 2};
    if (m == "0"){
        cout << 1 << endl;
        return 0;
    }
    if (modulo[t1] == 1) {
        cout << n[n.size()-1] << endl;
        return 0;
    } else {
        r = div(m, modulo[t1]);
        if (r == 0) r = modulo[t1];
        while(--r){
            ans *= t1;
            ans %= 10;
        }
        cout << ans << endl;
    }
}
0