有的時候總會去想如何才能用更少的時間複雜度來解決問題,但是有的時候 ACM需要快速的敲出代碼,要利用暴力得到一個簡潔的解決方案。A : http://codeforces.com/contest/252/problem/A暴力int data[110];int main(){int n;cin>>n;for(int i = 1 ; i <= n ; i ++ ) cin>>data[i];int maxn = -1;int x;for(int i = 1 ; i
The flood fill algorithm is somewhat tricky to implement. I use a iterative method instead of recursive flooding. I first scan through the west and east points to the current point until a different colored one is encountered. Then, the points
I convert all the calculations from floating-point to integral to avoid all the messes in floating-point calculation. The method is straight forward: I simulate the exchanging progress and use a 'budget' to record the extra cents that should be
Let f(n) denote the number of ways to make the sum of n. When n > 3, the number of digits is greater than 1. The first digit d used must be one of 1, 2, 3 or 4. Remove the first digit, then the remaining sum is n-d. There are f(n-d) ways to make
Let n denotes the length of the expression, d denotes the depth at most, and the count is f(n,d). Obviously, f(n,d) = 0 when n is odd. For other conditions, let us consider where we can find the corresponding parenthesis of the leftmost one. It can
Let f(k, d) denote the number of labelings of the k-ary tree of depth d. Moreover, let n(k, d) denote the number of nodes of the tree. The tree contains k subtrees, each contains n(k, d-1) nodes. There are totally (n(k, d) - 1)! / (n(k, d-1)!)^k
I develop a script to post my code to the UVa online judgeautomatically. This tool will also replace the local #include's withits real content when uploading the code to the server. So, I haven't to paste the code all around any more.Curl and
I believe sorting the poker at first may be helpful. You should determine the kind of the poker hand from the highest rank down to the lowest. Comparing the poker hands with the same kind is tricky to implement due to the complex rules.Code:/********
Let f(n) denote the furthest distance we can go with n steps, then we can derive this formula easily: f(n) = ((n+1)/2)^2 if n is odd, (1, 2, 3, 4, 3, ,2, 1) f(n) = (n/2)^2 + n/2 if n is even. (1, 2, 3, 3, 2, 1)Give the distance of f(n), we can
First, convert each package p = (s1, s2, s3) to a vector (s2 - s1, s3 - s1). All the packages forms convex hull. If point (0, 0) is in this convex hull, then there will be a solution, otherwise not. I can not tell the proof of this now, if you have
The final state of the last bulb is determined by the number of factors of n. Only the number which is the square of a number has an odd number of factors, thus it has a state of "yes".Code:/**********************************************************
Output related problem. I use a predefined bitmap of all the numbers and enlarge them when displaying.Code:/************************************************************************* * Copyright (C) 2008 by liukaipeng
From the description of this problem we can easily draw the recurrence of f(n) = 2*f(n-k) + 2^k - 1and the base cases of f(0) = 0 and f(1) = 1However, how can we determine which k would give the minimum value of f(n)?Suppose k is giving the
Compute the remainder with the following equation: (x*y)%n = (x%n)*(y%n)%nCode:/*************************************************************************** * Copyright (C) 2008 by Liu Kaipeng * * LiuKaipeng
The close form of fibs does not help in the problem. The input range is too large that the precision of long double won't be sufficient to handle it correctly. Just generate all the fibs up to 100 digits and use string compare to find the
For the detail explanation of the algorithm, refer to Programming Challenges.Code:/*************************************************************************** * Copyright (C) 2008 by Liu Kaipeng * * LiuKaipeng
According to the Goldbach's Conjecture, every even number that is not less than 4 is the sum of two primes. So we can just reduce the number to an even number by subtracting 4=2+2 or 5=2+3 from it, and then test every possible summation of the
SIGIR-20102010.1.15 : Abstracts for full research papers due2010.1.22 : Full research paper submissions due2010.2.12 : Posters, demonstration, and tutorial proposals due2010.3.24 : All other acceptance notificationsKDD-20102010.2.2 : Abstract due 201