A precision calculation
This time, the course mainly about the high-precision add, subtract, multiply.
First, the definition of a high-precision structure, the length of this number is stored, and the number itself.
1 struct gaojing 2 { 3 int n,z[2333]; 4 5 gaojing() 6 { 7 n=1; 8 memset(z,0,sizeof(z)); 9 } 10 11 void init() 12 { 13 scanf("%s",s+1); 14 int l=strlen(s+1); 15 reverse(s+1,s+l+1); 16 17 n = l; 18 for (int a=1;a<=n;a++) 19 z[a] = s[a]-'0'; 20 } 21 22 void print() 23 { 24 for (int a=n;a>=1;a--) 25 printf("%d",z[a]); 26 } 27 };
1, high-precision adder
Only need to read numbers as strings, bit by bit in sum, taking into account bit.
Here also need to note is that, when we write high-precision computing functions, parameters, it is best to read the address, rather than directly reading the string. So if this is a big string parameter Shihai to put this into the great string copied again, not only time consuming, but also consume memory.
1 gaojing operator+(const gaojing &a,const gaojing &b) 2 { 3 gaojing c; 4 c.n = max(a.n,b.n); 5 for (int i=1;i<=c.n;i++) 6 c.z[i] = a.z[i] + b.z[i]; 7 for (int i=1;i<=c.n;i++) 8 { 9 c.z[i+1] += c.z[i]/10; 10 c.z[i] = c.z[i]%10; 11 } 12 if (c.z[c.n+1] != 0) c.n++; 13 return c; 14 }
2, precision comparator
1 bool operator<(const gaojing &a,const gaojing &b) 2 { 3 if (a.n!=b.n) return a.n<b.n; 4 for (int i=a.n;i>=1;i--) 5 if (a.z[i] != b.z[i]) return a.z[i]<b.z[i]; 6 return false; 7 }
3, high-precision multiplication
First of all, and the ten * X = hundred, ten * = one thousand one hundred, one thousand Wan * = ten million available:
Bit i * j bits, the result to be added to the i + j-1 bits.
Finally, there is the difference between addition and high-precision point is that this should be removed leading zeros.
1 gaojing operator*(const gaojing &a,const gaojing &b) 2 { 3 gaojing c; 4 c.n = a.n + b.n; 5 for (int i=1;i<=a.n;i++) 6 for (int j=1;j<=b.n;j++) 7 c.z[i+j-1] += a.z[i] * b.z[j]; 8 for (int i=1;i<=c.n;i++) 9 { 10 c.z[i+1] += c.z[i]/10; 11 c.z[i] = c.z[i]%10; 12 } 13 while (c.n != 1 && c.z[c.n] == 0) 14 c.n--; 15 return c; 16 }
4, high-precision subtraction
In fact, the addition of thought and precision similar, but to judge the positive and negative (with a comparison function to determine just written to).
Write the code is not.
Second, the greatest common divisor, least common multiple.
Greatest common divisor function can be built:
__gcd(a,b);
Recommended use handwriting greatest common factor:
int gcd(int a,int b) { if (b==0) return a; else return gcd(b,a%b); }
The least common multiple words, according to multiply two numbers equal to their gcd * lcm can be obtained.
Third, the rapid power:
First, the purpose is to achieve rapid power fast exponentiation, suppose we require a ^ b, according to a simple algorithm is to continually multiply b times, so that the time complexity is O (b) that is, O (n) level quickly power can do O (logn), fast sorts. It works as follows:
Suppose we claim a ^ b, then b is in fact split into binary, the i-th bit weight of the binary number 2 ^ (i-1), when b == 11 when e.g., A ^ = A ^. 11 (2 + 2 ^ 0 ^ 3 ^ 2 + 1)
1 int ksm(int x,int y,int p) 2 { 3 int ans=1; 4 while (y) 5 { 6 if (y&1) ans=1ll*ans*x%p; 7 x=1ll*x*x%p; 8 y>>=1; 9 } 10 return ans; 11 }
Code is very short, rote also feasible, but it is best to understand it, in fact, is also well understood to b == 11, for example, b => 1011, the binary count from right to left, but take out order is a ^ (2 ^ 0) * a ^ (2 ^ 1) * a ^ (2 ^ 3), is from left to right. We continue to make base * = base that is tired and take aim, in order to contribute at any time ans.
To understand where the base * = base this step, see ::: base * base == base ^ 2, then take the next step, is to base ^ 2 * base ^ 2 == base ^ 4, then empathy base ^ 4 * base4 ? = base ^ 8 ,,,,, see is not done base -> base ^ 2 -> base ^ 4 -> base ^ 8 -> base ^ 16 -> base ^ 32 .... 2 ^ i is the index ... ah, look at the example above, a¹¹ = a ^ (2 ^ 0) * a ^ (2 ^ 1) * a ^ (2 ^ 3), which is not a perfect solution three , and, ah, fast power that's it.
Fourth, the matrix content
(I have written this part, and not repeat them here)