USACO: Section 1.4 -- PROB Arithmetic Progressions
2008-07-03 13:44
621 查看
Source Code
Lesson Learned:
1. Just as the sorting, searching algorithms, the sequence search(O(N)) < binary search(O(logN)) < bitmap algorithm(O(1)).
2. To boost performance a program, the most possible bottleneck is the nested loops. For deep nested loops, the time is: d1*d2*d3...(di stands for the loop count of level i). So, we should optimize at different levels to boost the whole nested loops performance.
For example, we first caculate the MaxB(using fomular a+(N-1)*b <=p^2+q^2 ) for variable "b" in level1; then, we pre-calculate the possible value array for variable "a" in level2("a" must be in the square array set. Note: this greatly cuts level2 loop count). Then, we check if the a+(N-1)*b exceeds the max square value to filter even more level2 values(This at least cuts the level2 loop by half). Finally, in the level3, we use the bitmap search for optimization.
Lesson Learned:
1. Just as the sorting, searching algorithms, the sequence search(O(N)) < binary search(O(logN)) < bitmap algorithm(O(1)).
2. To boost performance a program, the most possible bottleneck is the nested loops. For deep nested loops, the time is: d1*d2*d3...(di stands for the loop count of level i). So, we should optimize at different levels to boost the whole nested loops performance.
For example, we first caculate the MaxB(using fomular a+(N-1)*b <=p^2+q^2 ) for variable "b" in level1; then, we pre-calculate the possible value array for variable "a" in level2("a" must be in the square array set. Note: this greatly cuts level2 loop count). Then, we check if the a+(N-1)*b exceeds the max square value to filter even more level2 values(This at least cuts the level2 loop by half). Finally, in the level3, we use the bitmap search for optimization.
相关文章推荐
- 【USACO 1.4】Arithmetic Progressions
- USACO 1.4 Arithmetic Progressions
- [USACO 1.4] Summary of Section 1.4
- USACO Section 1.4 Arithmetic Progressions 解题报告
- USACO-Section1.4 Wormholes(枚举法)
- USACO-Section 3.3-PROB Shopping Offers
- USACO Section 1.4 Arithmetic Progressions
- USACO Section 1.4 Packing Rectangles
- USACO Section 1.4 Arithmetic Progressions
- USACO 之 Section 1.4 More Search Techniques (已解决)
- USACO Section 3.2 PROB Magic Squares
- USACO Section 1.4 Mother's Milk - 输出错了好几次..
- USACO-Section 4.1-PROB Beef McNuggets
- USACO-Section 1.4 Arithmetic Progressions(枚举)
- USACO Section 1.4 - Arithmetic Progressions (暴力)
- USACO Section1.4 Arithmetic Progressions
- USACO Section 2.1: Prob The Castle
- USACO Section 1.4 Mother's Milk
- USACO-Section1.4 Arithmetic Progressions[其他][暴力枚举]
- USACO Section 1.4 Mother's Milk