Github最终优化代码: https://github.com/laiy/Datastructure-Algorithm/blob/master/sicily/1020.c
题目如下:
1020. Big Integer
Constraints
Time Limit: 1 secs, Memory Limit: 32 MB
Description
Long long ago, there was a super computer that could deal with VeryLongIntegers(no VeryLongInteger will be negative). Do you know how this computer stores the VeryLongIntegers? This computer has a set of n positive integers: b1,b2,...,bn, which is called a basis for the computer.
The basis satisfies two properties:
1) 1 < bi <= 1000 (1 <= i <= n),
2) gcd(bi,bj) = 1 (1 <= i,j <= n, i ≠ j).
Let M = b1*b2*...*bn
Given an integer x, which is nonegative and less than M, the ordered n-tuples (x mod b1, x mod b2, ..., x mod bn), which is called the representation of x, will be put into the computer.
Input
The input consists of T test cases. The number of test cases (T) is given in the first line of the input.
Each test case contains three lines.
The first line contains an integer n(<=100).
The second line contains n integers: b1,b2,...,bn, which is the basis of the computer.
The third line contains a single VeryLongInteger x.
Each VeryLongInteger will be 400 or fewer characters in length, and will only contain digits (no VeryLongInteger will be negative).
Output
For each test case, print exactly one line -- the representation of x.
The output format is:(r1,r2,...,rn)
Sample Input
2 3 2 3 5 10 4 2 3 5 7 13
Sample Output
(0,1,0) (1,1,3,6)
Problem Source
ZSUACM Team Member
博主刚看到这题目的时候,没怎么多想,写了个如下公式:
(x0*10^length-1 + x1*10^length-2 + ... + xlength-1*10^0)%b
=(x0*10^length-1%b + x1*10^length-2%b + ... + xlength-1*10^0%b)%b
然后这里关键是求出10^x%b出来。
注意到在求解10^x%b=(10%b * 10^x-1%b)%b,这里有一个明显的递归公式。
即,在求解10^x%b的时候已经包含了10^x-1%b的计算,而10^x-1%b的计算也会包含10^x-2%b的计算。
换句话说,我们可以实现求出需要的10^length%b,并用一个数组来保存每一步计算出来的10^x%b。
想出来的时候博主认为这样写已经效率很好了,没有重复计算,跑出来的结果是0.05s。
核心代码如下:
1 for (i = 0; i < n; i++) { 2 z = b[i]; 3 remember_arr[0] = 1; 4 5 temp = 10 % z; 6 for (j = 1; j < length; j++) { 7 remember_arr[j] = (temp * remember_arr[j - 1]) % z; 8 } 9 10 result[i] = 0; 11 count = length - 1; 12 for (j = 0; j < length; j++) { 13 result[i] += (((x[j] - 48) % z) * remember_arr[count--]) % z; 14 } 15 result[i] %= z; 16 }
然后,博主google发现有一种更快的写法,即用代码模拟人手算的过程,核心代码如下:
1 for (j = 0; j < length; j++) 2 result = (result * 10 + big_number[j]) % b;
用这种写法跑的是0.03s。
最后我们来思考一下,人手算的过程的借位都是10,为什么? 因为如果借位太多计算不过来,但是计算机不一样,对计算机来说,xx/b的计算和xxxxxxxxxxx/b的计算是一个原子性的操作,就是说,计算机有能力借更多的位。
我们看int最大大概10^10,那么如果一次借位10^9是不是大大减少了这个运算呢?
比如说,现在有长度为36的字符串,如果按原本的写法,要借位35次,换句话说,有35个求余计算,如果把这个字符串切成4个长度为9的字符串,然后以这些碎片为单位进行运算和借位的话,只需要借位运算4次。
更改了求余运算的基本运算长度之后,性能将大幅提高,最终结果为0.00s。
核心代码如下:
1 num_pieces = length / 9 + 1; 2 pieces[0] = str_to_int(p, length % 9); 3 p += length % 9; 4 for (i = 1; i < num_pieces; i++) 5 pieces[i] = str_to_int(p, 9), p += 9; 6 for (i = 0; i < n; i++) 7 r[i] = calculate_result(b[i]);
函数实现如下:
1 int str_to_int(char *str, int l) { 2 result = 0, j = 0; 3 while (j < l) 4 result = result * 10 + str[j++] - ‘0‘; 5 return result; 6 } 7 8 int calculate_result(int b) { 9 t = 1000000000 % b; 10 result = 0; 11 for (j = 0; j < num_pieces; j++) 12 result = (result * t + pieces[j] % b) % b; 13 return result; 14 }