CSES Solutions
#1095Mathematics

Exponentiation

View on CSES

Solution

1095-Exponentiation.cpp
1#include<bits/stdc++.h>
2using namespace std;
3#define int long long
4int binpow(int a, int b, int m){
5  if(b==0) return 1;
6  if(b==1) return a;
7  int tmp=binpow(a, b>>1, m);
8  tmp = tmp * tmp % m;
9  if(b&1) tmp = tmp * a % m;
10  return tmp;
11}
12signed main() {
13  int n; cin>>n;
14  for(int i=0;i<n;++i){
15    int a,b; cin>>a>>b;
16    cout<<binpow(a,b,1e9+7)<<'\n';
17  }
18}

Editorial not yet generated for this problem. Run the editorial generation script to add hints and detailed explanations.