Soluție HackerRank pentru Superpowers of 2, subdomeniul Number Theory, în C++14. Include cerința formatată, exemple, explicația pașilor și cod sursă.

  • Problemă: Superpowers of 2
  • Domeniu: Number Theory
  • Limbaj: C++14

Challenge: Superpowers of 2

Subdomeniu: Number Theory (number-theory)

Scor cont: 60.0 / 60

Submission status: Accepted

Submission score: 1.0

Submission ID: 464733548

Limbaj: cpp14

Link challenge: https://www.hackerrank.com/challenges/superpowers/problem

Cerință

You will be given two integers a, b. You are required to output the result of 2^(2^a) mod b.

Constraints

1 ≤ a, b ≤ 10^6

Input Format

First and only line of the input will contain two integers a, b separated by a single blank space.

Output Format

Output the desired result in a single line.

Cod sursă

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

int main(void)
{
    int a, b, ans = 2;
    cin >> a >> b;

    for(int i = 1; i <= a; i++){
        ans = (1LL * ans * ans) % b;
    }
    cout << ans % b << endl;

    return 0;
}
HackerRank Number Theory – Superpowers of 2