全部博文(930)
分类: LINUX
2009-05-13 14:38:03
Description Problems involving the computation of exact values of very large magnitude and precision are common. For example, the computation of the national debt is a taxing experience for many computer systems. This problem requires that you write a program to compute the exact value of Rn where R is a real number ( 0.0 < R < 99.999 ) and n is an integer such that 0 < n <= 25. Input The input will consist of a set of pairs of values for R and n. The R value will occupy columns 1 through 6, and the n value will be in columns 8 and 9. Output The output will consist of one line for each line of input giving the exact value of R^n. Leading zeros should be suppressed in the output. Insignificant trailing zeros must not be printed. Don't print the decimal point if the result is an integer. Sample Input 95.123 12 0.4321 20 5.1234 15 6.7592 9 98.999 10 1.0100 12 Sample Output 548815620517731830194541.899025343415715973535967221869852721 .00000005148554641076956121994511276767154838481760200726351203835429763013462401 43992025569.928573701266488041146654993318703707511666295476720493953024 29448126.764121021618164430206909037173276672 90429072743629540498.107596019456651774561044010001 1.126825030131969720661201 |
很容易看出规律出来:
1、粉红色格式里头的数为对应列和行位置上数的乘积。如第一行,9=9 X 1,这样就可以求出所有粉红色格子里头的数。
2、把粉红色格子里头的数的不同位分别放在红色斜线的两侧,如果十位没有,那么用零补充。
3、从右下角开始,我们把同处于斜线一侧的数相加,结果存放到对应的左下部位的格子里头。如4=4, 5=2+0+3, 5=6+3+4+0+2 mod 10[进位为一], 0=1[进位]+3+7+2+6+0+1 mod 10,以此类推,直到左上角。
实际上,仔细分析以下,我们就会这种乘法和我们小学就学习过的“竖式乘法”原理是一样的,所以肯定是正确的。但是,这种描述方式给我们带来了算法设计和实现上的突破。
下面我们就需要把它实现了。
首先,我们定义数据的存储方式,乘数和被乘数直接存放到两个一維数组里头,中间过程(即粉红色背景部分)用一个三维数据来存放,存放形式如下:
假设该三维数组的名称为Rect,则有如下关系:
Rect[3][2][1] = 4 Rect[3][2][0] = 0 Rect[3][1][1] = 2 Rect[2][2][1] = 3 ... |
|
shell> make lnmp cc lnmp.c -o lnmp shell> ./lnmp Please input two large numbers: 2 5 The product of the muliplication is: 10 shell> ./lnmp Please input two large numbers: 22222222222222222222222222222222222222222222222222222222222222222222222222 5 The product of the muliplication is: 111111111111111111111111111111111111111111111111111111111111111111111111110 shell> ./lnmp Please input two large numbers: 0000000000000000000000000000000000000000000000002 000000000000000000000000000000000000000000000055 The product of the muliplication is: 110 |
|
95.123 12 0.4321 20 5.1234 15 6.7592 9 98.999 10 1.0100 12 0.0001 20 3.0000 25 |
shell>make rton cc rton.c -o rton shell> time ./rton .00000005148554641076956121994511276767154838481760200726351203835429763013462401 43992025569.928573701266488041146654993318703707511666295476720493953024 29448126.764121021618164430206909037173276672 90429072743629540498.107596019456651774561044010001 1.126825030131969720661201 .00000000000000000000000000000000000000000000000000000000000000000000000000000001 847288609443 real 0m0.023s user 0m0.020s sys 0m0.000s |
|
|
chinaunix网友2009-09-11 09:49:32
你给出了实现大整数相乘的代码, 却没有分析算法的复杂度。其实解决大整数相乘的问题还有一个简单的方法。 问题定义: 求出Z, where Z=X*Y; 其中X, Y, Z都将以多项式的方式表达(即以数组的方式), 比如说x=12, 那么用数组方式表达就是x=[1, 2]; 为了方便讨论, 令X, Y为两个长度相等的数列, 长度假设为N. 那么: Z=ifft( fft(X)*fft(Y) ); 其中fft(*)表示对*的快速傅立叶变换, ifft(*)表示对*的逆快速傅立叶变换。 这个算法的时间复杂度是N*log(N), 空间复杂度是N. 我不敢确定, 从空间复杂度看来, 似乎比你的rectangle要好。(请楼主交代一下三维数组Rect的具体结构) Su Dongcai suntree4152@gmail.com