Skip to content

Instantly share code, notes, and snippets.

@draftcode
Created October 9, 2011 13:13
Show Gist options
  • Select an option

  • Save draftcode/1273665 to your computer and use it in GitHub Desktop.

Select an option

Save draftcode/1273665 to your computer and use it in GitHub Desktop.
#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