๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm ๐Ÿง‘๐Ÿป‍๐Ÿ’ป/๋ฐฑ์ค€(BOJ)

[๋ฐฑ์ค€,c++] 13171๋ฒˆ - A

by dkswnkk 2021. 11. 4.
 

13171๋ฒˆ: A

์Œ์ด ์•„๋‹Œ ๋‘ ์ •์ˆ˜ A, X ๊ฐ€ ์žˆ์„ ๋•Œ AX์„ ๊ตฌํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ƒ๊ฐํ•ด๋ณด์ž. ๋ฌผ๋ก  ์ด ์ˆ˜๋Š” ๋งค์šฐ ํด ์ˆ˜ ์žˆ๊ธฐ์—, 1,000,000,007 (= 109 + 7)๋กœ ๋‚˜๋ˆˆ ๋‚˜๋จธ์ง€๋ฅผ ๊ตฌํ•  ๊ฒƒ์ด๋‹ค. a mod x๋ฅผ a๋ฅผ x๋กœ ๋‚˜๋ˆด์„ ๋•Œ์˜ ๋‚˜๋จธ์ง€๋ผ๊ณ  ํ‘œ

www.acmicpc.net

10^4๋ฅผ ๊ตฌํ• ๋•Œ 101010*10 ๋ณด๋‹ค๋Š” 10^2 * 10^2 ๊ฐ€ ๋” ๋น ๋ฅธ ๊ฒฝ์šฐ์ž„์„ ์ƒ๊ฐํ•˜์—ฌ ํ’€์ดํ•œ๋‹ค.
a^b์ผ๋•Œ b๊ฐ€ ํ™€์ˆ˜์ธ ๊ฒฝ์šฐ๋Š” a๋ฅผ ํ•œ๋ฒˆ ๋” ๊ณฑํ•ด์ค˜์•ผ ํ•œ๋‹ค.

#include <iostream>
#define ll unsigned long long int
#define mod 1000000007
using namespace std;

ll pow(ll a, ll b) {
    if (b == 0) return 1;
    else {
        ll n = pow(a, b / 2);
        ll temp = (n * n) % mod;
        if (b % 2 == 0) return temp;
        else return (temp * a) % mod;
    }
}
int main() {

    ios_base::sync_with_stdio(false);
    cin.tie(0);

    ll A, B; cin >> A >> B;
    cout << pow(A%mod, B); //mod๋กœ ๋จผ์ € ๋‚˜๋ˆ ์ฃผ๋Š”๊ฒŒ ํ•ต์‹ฌ.

}

๋Œ“๊ธ€