Skip to content

Instantly share code, notes, and snippets.

@mejibyte
Created October 3, 2011 05:04
Show Gist options
  • Save mejibyte/1258471 to your computer and use it in GitHub Desktop.
Save mejibyte/1258471 to your computer and use it in GitHub Desktop.
Division of very big numbers in O(n) where n is the number of digits of the big number.
int D[3000];
int len;
int Mod(int x) {
int res = 0;
for (int i = len-1; i >= 0; --i)
res = (res * 10 + D[i]) % x;
return res;
}
void Div(int x) {
int left = 0, will;
for (int i = len-1; i >= 0; --i) {
will = (left * 10 + D[i]) % x;
D[i] = (left * 10 + D[i]) / x;
left = will;
}
while (len && D[len-1] == 0) len--;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment