Created
October 3, 2011 05:04
-
-
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.
This file contains 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
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