aboutsummaryrefslogtreecommitdiff
path: root/semestr-4/aisd/themis/AISDPOTEGOWANIE.cpp
blob: c1b76fe43b0df46d38801993c10c9dedc6d7803e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <bits/stdc++.h>
using namespace std;

int fast_pow(int a, int b, int m)
{
  if (b == 0)
  {
    return 1;
  }
  long long p = fast_pow(a, b / 2, m);
  p = (p * p) % m;
  if (b % 2 == 0)
    return (int)p;
  return (p * (long long)a) % m;
}

int main()
{
  ios_base::sync_with_stdio(false);
  cin.tie();
  int t;
  cin >> t;
  while (t--)
  {
    int a, b, m;
    cin >> a >> b >> m;
    cout << fast_pow(a, b, m) << "\n";
  }
}