Sicily1020-大数求余算法及优化
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。
核心代码如下:
for (i = ; i < n; i++) {
z = b[i];
remember_arr[] = ; temp = % z;
for (j = ; j < length; j++) {
remember_arr[j] = (temp * remember_arr[j - ]) % z;
} result[i] = ;
count = length - ;
for (j = ; j < length; j++) {
result[i] += (((x[j] - ) % z) * remember_arr[count--]) % z;
}
result[i] %= z;
}
然后,博主google发现有一种更快的写法,即用代码模拟人手算的过程,核心代码如下:
for (j = ; j < length; j++)
result = (result * + big_number[j]) % b;
用这种写法跑的是0.03s。
最后我们来思考一下,人手算的过程的借位都是10,为什么? 因为如果借位太多计算不过来(99乘法表内的计算对人来说我们认为是可以快速计算的),但是计算机不一样,对计算机来说,xx/b的计算和xxxxxxxxxxx/b的计算是一个原子性的操作,就是说,计算机有能力借更多的位。
我们看int最大大概10^10,那么如果一次借位10^9是不是大大减少了这个运算呢?
比如说,现在有长度为36的字符串,如果按原本的写法,要借位35次,换句话说,有35个求余计算,如果把这个字符串切成4个长度为9的字符串,然后以这些碎片为单位进行运算和借位的话,只需要借位运算4次。
更改了求余运算的基本运算长度之后,性能将大幅提高,最终结果为0.00s。
核心代码如下:
num_pieces = length / + ;
pieces[] = str_to_int(p, length % );
p += length % ;
for (i = ; i < num_pieces; i++)
pieces[i] = str_to_int(p, ), p += ;
for (i = ; i < n; i++)
r[i] = calculate_result(b[i]);
函数实现如下:
int str_to_int(char *str, int l) {
result = , j = ;
while (j < l)
result = result * + str[j++] - '';
return result;
} int calculate_result(int b) {
t = % b;
result = ;
for (j = ; j < num_pieces; j++)
result = (result * t + pieces[j] % b) % b;
return result;
}
Sicily1020-大数求余算法及优化的更多相关文章
- POJ 2635 The Embarrassed Cryptographer(大数求余)
题意:给出一个大数,这个大数由两个素数相乘得到,让我们判断是否其中一个素数比L要小,如果两个都小,输出较小的那个. 分析:大数求余的方法:针对题目中的样例,143 11,我们可以这样算,1 % 11 ...
- Project Euler 48 Self powers( 大数求余 )
题意: 项的自幂级数求和为 11 + 22 + 33 + - + 1010 = 10405071317. 求如下一千项的自幂级数求和的最后10位数字:11 + 22 + 33 + - + 100010 ...
- POJ2635-The Embarrassed Cryptographer 大数求余
题目链接:http://poj.org/problem?id=2635 题目分析: http://blog.csdn.net/lyy289065406/article/details/6648530
- (大数 求余) Large Division Light OJ 1214
Large Division Given two integers, a and b, you should check whether a is divisible by b or not. We ...
- Large Division (大数求余)
Given two integers, a and b, you should check whether a is divisible by b or not. We know that an in ...
- 大数求模 sicily 1020
Search
- 2016中国大学生程序设计竞赛 - 网络选拔赛 1001 A water problem (大数取余)
Problem Descripton Two planets named Haha and Xixi in the universe and they were created with the un ...
- 如何运用同余定理求余数【hdoj 1212 Big Number【大数求余数】】
Big Number Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total ...
- 高效求幂取余 算法,复杂度 log(n)
做TopCoder SRM 576 D2 L3 题目时,程序有个地方需要对一个数大量求幂并取余,导致程序运行时间很长,看了Editoral之后,发现一个超级高效的求幂并取余的算法,之前做System ...
随机推荐
- iOS 此证书的签发者无效
1.先检查Apple Worldwide Developer Relations Certification Authority Intermediate Certificate证书是否过期,该证书过 ...
- C蛮的全栈之路-node篇(二) 实战一:自动发博客
目录 C蛮的全栈之路-序章 技术栈选择与全栈工程师C蛮的全栈之路-node篇(一) 环境布置C蛮的全栈之路-node篇(二) 实战一:自动发博客 ---------------- 我是分割线 ---- ...
- 水题(素数表)NYOJ素数距离
描述 现在给出你一些数,要求你写出一个程序,输出这些整数相邻最近的素数,并输出其相距长度.如果左右有等距离长度素数,则输出左侧的值及相应距离. 如果输入的整数本身就是素数,则输 ...
- createjs 下雪 实例
demo: http://output.jsbin.com/davixatona <!DOCTYPE html> <html> <head> <meta c ...
- Java学习----日期函数
Date Calendar public class TestDate { private Date date; Calendar calendar; public TestDate() { // ...
- window.frameElement属性
比如有一个iframe的src是xxx.htm frameElement的作用就是在xxx.htm中获得这个引用它的iframe objet 这样你就可以在xxx.htm改变iframe的大小,或是边 ...
- 移动web页面使用微软雅黑字体的问题
很多前端工程师在开发手机页面的时候,发现视觉设计师们喜欢用微软雅黑作为中文字体进行设计,于是写页面的时候也定义 font-family 为微软雅黑,后来发到线上后,细心的产品经理发现页面的字体不是微软 ...
- python基础之列表常用操作及知识点小结
列表(list) List(列表) 是 Python 中使用最频繁的数据类型.列表可以完成大多数集合类的数据结构实现.它支持字符,数字,字符串甚至可以包含列表(所谓嵌套).列表用[ ]标识,是pyth ...
- 学学Whatsapp,如何让自己挣160亿美金,然后退休?开发个J2ME应用。
facebook用160亿美元收购了Whatsapp,要知道这是facebook市值1600亿美元的十分之一,而Whatsapp是一个只有50名员工的小公司,这个价格让硅谷各种科技公司大佬跌破镜框.其 ...
- ARM920T系统总线时序分析
一.系统总线时序图 二.分析 第一个时钟周期开始,系统地址总线给出需要访问的存储空间地址. 经过Tacs时间后,片选信号也相应给出,并且锁存当前地址线上地址信息. 再经过Tcso时间后,处理器给出当前 ...