Created
October 9, 2011 13:13
-
-
Save draftcode/1273665 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| using namespace std; | |
| typedef unsigned long long ull; | |
| ull pow(ull p, ull n) { | |
| ull w = 1; | |
| for (ull i = 1ULL << (sizeof(ull)*8-1); i != 0; i >>= 1) { | |
| if ((n & i) == 0) { | |
| w *= w; | |
| } else { | |
| w *= w * p; | |
| } | |
| } | |
| return w; | |
| } | |
| ull modpow(ull p, ull n, ull m) { | |
| ull w = 1; | |
| for (ull i = 1ULL << (sizeof(ull)*8-1); i != 0; i >>= 1) { | |
| if ((n & i) == 0) { | |
| w *= w; | |
| } else { | |
| w *= w * p; | |
| } | |
| w %= m; | |
| } | |
| return w; | |
| } | |
| int main(void) { | |
| cout << modpow(3, 10, 1000) << endl; | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment