Subscribe Us

Responsive Advertisement

Advertisement

Binary Exponentiation | Number Theory in cpp

 

Binary Exponentiation | Number Theory in cpp




#include<bits/stdc++.h>

using namespace std;

#define ll long long int

ll power(ll base,ll pow)

{

    if(base==0)return 0;


    ll res=1;

    while(pow)

    {

        if(pow%2)

        {

            res*=base;

            pow--;


        }

        else

        {

            base*=base;

            pow/=2;

        }

    }

    return res;

}

int main()

{


    ll t,n,m;

    cin>>t;

    while(t--)

    {

        cin>>n>>m;

        cout<<power(n,m)<<endl;

    }




    return 0;

}


Post a Comment

0 Comments