Tony's Log

Algorithms, Distributed System, Machine Learning

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::

02 2014 档案

摘要:By property of mod operations , we can simply use Divide and Conquer + Recursion to solve it. Reference:https://www.khanacademy.org/math/applied-math/cryptography/modarithmetic/a/modular-exponentiationMy Ruby version is:DIV = 20def ferma(x, y, n) c = 1 for i in 0..y-1 c = (c * x) % n ... 阅读全文
posted @ 2014-02-20 03:39 Tonix 阅读(195) 评论(0) 推荐(0)

摘要:Another palindrome related problem. Actually nothing too theoretical here, but please keep following hints in mind:1. How to check whether a natural number is Palindrome Not sure whether there's closed form to Palindrome, I simply used a naive algorithm to check: log10() to get number of digits, 阅读全文
posted @ 2014-02-19 13:38 Tonix 阅读(201) 评论(0) 推荐(0)

摘要:A typical flood-fill algorithm application (BFS). Not very complex, except only 1 tip: instead of searching for new space, I keep all spaces(occupied or not) in a hash_map that gets updated in a flood-fill.#include #include #include #include #include #include #include #include using namespace std;#i 阅读全文
posted @ 2014-02-18 16:02 Tonix 阅读(211) 评论(0) 推荐(0)

摘要:The default answer to LCS(consecutive or non-consecutive) is DP. Recurrence formula can be found athttp://en.wikipedia.org/wiki/Longest_common_subsequence_problem . Calculation is O(mn) and printing is O(m+n).But this typical DP algorithm triggers TLE, as many others:#include #include #include #incl 阅读全文
posted @ 2014-02-18 14:19 Tonix 阅读(288) 评论(0) 推荐(0)

摘要:1 line in Ruby(2.0), 36B:p (1..n=gets.to_i).reduce(:*)+2**n-n 阅读全文
posted @ 2014-02-13 15:40 Tonix 阅读(139) 评论(0) 推荐(0)

摘要:Coding a Dijkstra is not hard. %70 of my time spent on tackling TLE, as my last post.Dijkstra works in BFS manner, but at each step, it picks the shortest child greedily and then relax all other neighbors.Data Structure: since there could be up to 10000 cities, storing distance for all pairs of citi 阅读全文
posted @ 2014-02-13 14:35 Tonix 阅读(343) 评论(0) 推荐(0)

摘要:As a solo warrior in OJ, I spent about nearly 50% of my time on tackling TLE - that is innumerous hours. I almost lost my courage to OJ.But thanks to this post:http://www.spoj.com/forum/viewtopic.php?f=3&t=7968. I didn't use all of the hints, but getchar_unlocked works like charms.As advised 阅读全文
posted @ 2014-02-13 14:28 Tonix 阅读(252) 评论(0) 推荐(0)

摘要:Since number could be 10^100, we have to use large number processing method, in string. Asimpler method is to pre-calculate all Fibonacci numbers up to 101 digits, which is already fast enough.// 536#include #include #include #include using namespace std;vector dict;string plus_str(string &a, st 阅读全文
posted @ 2014-02-10 14:43 Tonix 阅读(216) 评论(0) 推荐(0)

摘要:Sorting is not an out-dated topic. My own in-place qsort got TLE... so, I simply called stl::sort() to get AC.This thread explains everything:http://stackoverflow.com/questions/5038895/does-stdsort-implement-quicksortBasic qsort has a worst case of O(n^2) and could result in too deep stack. The late 阅读全文
posted @ 2014-02-09 16:37 Tonix 阅读(165) 评论(0) 推荐(0)

摘要:It is a small fun problem to solve. Since only a max sum is required (no need to print path), we can only keep track of the max value at each line. Basically it is still a human labor simulation work. To be more specifically, we need keep track of a line of max sums.But there are 1000*100 input, so 阅读全文
posted @ 2014-02-09 13:37 Tonix 阅读(424) 评论(0) 推荐(0)

摘要:Just CS rookie practice on DFS\BFS. But details should be taken care of:1. Ruby implementation got TLE so I switched to C++2. There's one space after each output node, including the last one. But SPOJ doesn't clarify it clearly.// 442#include #include #include #include #include using namespa 阅读全文
posted @ 2014-02-09 11:36 Tonix 阅读(313) 评论(0) 推荐(0)

摘要:This is simply a human work simulation - exactly reproducing how you do it by hand. Nothing special. You'd better put each step on a paper to make everything clear. Reference:http://blog.csdn.net/rappy/article/details/1737671My code passed all 6 test cases. Yay..// 429#include #include #include 阅读全文
posted @ 2014-02-06 14:20 Tonix 阅读(205) 评论(0) 推荐(0)

摘要:"not more than 1000000 digits" means an efficient in-place solution is needed. My first solution was stringint conversion based. It worked for small ints, but got TLE with large numbers.Thanks tohttp://www.bytehood.com/spoj-the-next-palindrome/418/that shed lights on me. The key idea is th 阅读全文
posted @ 2014-02-06 02:43 Tonix 阅读(225) 评论(0) 推荐(0)

摘要:Not hard to know it is simply transform from in-order to post-order.My first idea is to build a tree from in-order string and then traverse the tree by post-order - naive so slow.Subtle stack manipulation solves it - stack only for operators:http://cs.nyu.edu/courses/Fall12/CSCI-GA.1133-002/notes/In 阅读全文
posted @ 2014-02-04 14:59 Tonix 阅读(182) 评论(0) 推荐(0)

摘要:Counting trailing 0s of n! It is not very hard to figure out how to count it - simply count how many 5s. Since even numbers are always more than 5s, we don't have to consider even numbers.But counting 5, is tricky. Naive method is quite slow - I got 12+s for 1000000000. And after reading below a 阅读全文
posted @ 2014-02-04 12:54 Tonix 阅读(161) 评论(0) 推荐(0)

摘要:My first idea wasSieve of Eratosthenes, too. But obviously my coding was not optimal and it exceeded 6s time limit. Then I googled it. Sounds like Miller-Rabin Testing is a much more serious solution.http://www.cnblogs.com/feature/articles/1824667.htmlIt is based on Fermat Little Theory and property 阅读全文
posted @ 2014-02-03 14:02 Tonix 阅读(162) 评论(0) 推荐(0)

摘要:200: simple seconds to h:m:s. 2consecutive mod 60 and div 60. No big deal.550: simple DP-like process. A for loop: deducting one bit each time. Point is corner cases.1100: special backtrace on a tree. Direct thought like Greedy backtrace is not optimal - because it only needs a final optimal cost - 阅读全文
posted @ 2014-02-03 10:33 Tonix 阅读(187) 评论(0) 推荐(0)

点击右上角即可分享
微信分享提示