April 2019

Big Kahuna Burgers

Solution

Implementation

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

long long int power(long long int a, long long int b) // modular exponentiation
{
    int res=1;
    while(b)
    {
        if(b&1)
            res=res*a%9;
        b>>=1;
        a = a *a % 9;
    }

    return res? res: 9; // if the remainder is 0, then the sum of digits is 9.
}

int main()
{
    

    int t;
    cin>>t;
    while(t-->0)
    {
        long long int a,n;
        cin>>a>>n;
        cout<< power(a%9,n)<< endl;
    }
    return 0;
}