Skip to content

Instantly share code, notes, and snippets.

@fxxntrbl
Created July 18, 2020 11:07
Show Gist options
  • Save fxxntrbl/6c912dc348f096553f2b15ab66532a87 to your computer and use it in GitHub Desktop.
Save fxxntrbl/6c912dc348f096553f2b15ab66532a87 to your computer and use it in GitHub Desktop.
분할 정복 알고리즘 - (x * y) % m
#include <bits/stdc++.h>
using namespace std;
typedef long long int ll;
ll f (ll x, ll y, ll m) {
if (y == 0) return 0;
if (y == 1) return x % m;
if (y % 2 == 1) {
ll sib = f (x, y - 1, m) % m;
return ((sib % m) + x % m) % m;
}
else {
ll al = f (x, y / 2, m) % m;
return ((al % m) + (al % m)) % m;
}
}
int main () {
ll s, ib, al;
cin >> s >> ib >> al;
cout << f (s, ib ,al);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment