結果

問題 No.167 N^M mod 10
ユーザー tnakao0123
提出日時 2016-03-24 14:40:19
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 3 ms / 1,000 ms
コード長 1,074 bytes
コンパイル時間 624 ms
コンパイル使用メモリ 84,768 KB
実行使用メモリ 6,944 KB
最終ジャッジ日時 2024-09-22 01:28:49
合計ジャッジ時間 1,545 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 2
other AC * 27
権限があれば一括ダウンロードができます

ソースコード

diff #

/* -*- coding: utf-8 -*-
 *
 * 167.cc: No.167 N^M mod 10 - yukicoder
 */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<string>
#include<vector>
#include<map>
#include<set>
#include<stack>
#include<list>
#include<queue>
#include<deque>
#include<algorithm>
#include<numeric>
#include<utility>
#include<complex>
#include<functional>
 
using namespace std;

/* constant */

/* typedef */

/* global variables */

/* subroutines */

/* main */

int main() {
  string ns, ms;
  cin >> ns >> ms;

  int d = ns[ns.size() - 1] - '0';
  int ds[10], e = 0;
  ds[e++] = d;
  
  for (;;) {
    int d0 = ds[e - 1] * d % 10;
    if (d0 == d) break;
    ds[e++] = d0;
  }
  //printf("e=%d\n", e);
  //for (int i = 0; i < e; i++) printf("%d", ds[i]); putchar('\n');

  int ans;
  if (ms == "0") ans = 1;
  else if (e == 1) ans = d;
  else {
    int r = 0;
    for (int i = 0; i < ms.size(); i++) r = (r * 10 + ms[i] - '0') % e;
    r = (r + e - 1) % e;
    //printf("r=%d\n", r);
    ans = ds[r];
  }

  printf("%d\n", ans);
  return 0;
}
0