Anshuli and the Party

Vishi's birthday is on the door.

Anshuli and his friends are planning to give him a birthday party. For that Anshuli's friends want him to buy the cake. He needs to pay 'x' amount of money to buy the cake on the first day. After each day has passed, the cake becomes 'x' times the price that it was on the previous day. For buying the cake Anshuli has to collect money from all the friends and for that, he will need 'y' days and after 'y' days he will go and buy the cake.
Anshuli seeks your help in calculating the price of the cake on the yth day.
Take the price as modulo 10^9 + 7 as the price can be very large.

Example:

Input:  x = 9, y = 3
Output: 729

Approach

C++

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

unsigned long long power(unsigned long long x,
                         unsigned long long y)

{

    unsigned long long res = 1;
    while (y)
    {

        if (y & 1)
            res = ((res % mod) * (x % mod)) % mod;
        y = y >> 1;
        x = ((x % mod) * (x % mod)) % mod;
    }
    return res;
}

int main()
{

    unsigned long long x = 9y = 3ans;
    ans = power(xy);
    cout << ans % mod << "\n";

    return 0;
}


No comments:

Post a Comment