0w1

Entries from 2016-07-01 to 1 month

UVA 12105 - Bigger is Better ( DP )

UVa Online Judge maxl[ i ][ j ] = maximal number of digits of a possible number x, where x is constructible with not more than i sticks and remains j for % M maxd[ i ][ j ] = having maxl[ i ][ j ], the maximal digit that could be used maxl…

CFR 615 D. Multipliers ( Math )

Problem - D - Codeforces The number that some prime would be involved in the multiplication, is the cumulative product of the count of power for all other primes, again multiplied by ( 1 + 2 + .. + c ), where c is the count of power of the…

CFR 177 B2. Rectangular Game ( DP )

Problem - B2 - Codeforces Having done so many ( not really many though ) problems, I always warn myself to think of an easier solution. Anyways, this could be solved with DP. Since MAXN = 1e9, and 2*3*7*11*13*17*19*31*37 ≥ 2e9, there will …

CFR 82 C. Buns ( DP, Knapsack )

Problem - 106C - Codeforces I do seriously doubt the tag is nonsense. Anyways, for the buns that do not require stuffings, we will do infinite knapsack. For the buns that need stuffings, simply do multiple knapsacks. const int MAXN = 1e3 +…

CFR 453 1A. Little Pony and Expected Maximum ( Math, Expectancy )

http://codeforces.com/problemset/status The number of situations such that the face x is of the maximum, is ( x^n - ( x - 1 )^n ). So the expectancy we are looking for can be calculated as However, simply implementing this, you will find i…

CFR 599 D. Spongebob and Squares ( Math )

Problem - D - Codeforces Without losing generality, let n ≤ m, we are to find all such pairs ( n, m ) that satisfy: Do a little math and we will conclude that Since n ≤ m, the upper bound of n is of some constant multiple of X^( 1 / 3 ). T…

隨機算法快速判斷質數 O( lg V )

對因數分解困惑的時候剛好經過這個文章: 如何快速验证一个超大的数为质数 - SegmentFault 以前是聽過麻煩的 Miller-Rabin偽素數檢驗法,倒是沒聽過除此之外有這麼簡潔的隨機算法可以快速判斷是否質數。原理是根據 Fermat's little theorem: a ^ ( p - 1 ) =…

JOI 2007 春合宿 Fermat ( Fastpow )

fermat: フェルマー方程式 (Fermat) - 2007年 日本情報オリンピック春合宿OJ | AtCoder http://www.ioi-jp.org/camp/2007/2007-sp-tasks/2007-sp-day2_21.pdf Although it is a known fact that no integer solution exists for x^n + y^n = z^n, for n ≥ 3,…

JOI 2007 春合宿 Mall ( Prefix sum )

http://www.ioi-jp.org/camp/2007/2007-sp-tasks/2007-sp-day1_20.pdf mall: ショッピングモール (Mall) - 2007年 日本情報オリンピック春合宿OJ | AtCoder Get column prefix sum for each individual row, and enumerate top left corner to reduce unnece…

JOI 2007 春合宿 Factorial ( Binary Search )

http://www.ioi-jp.org/camp/2007/2007-sp-tasks/2007-sp-day1_20.pdf factorial: 階乗 (Factorial) - 2007年 日本情報オリンピック春合宿OJ | AtCoder First we will decompose N into powers of prime factors. Anything left after decomposing the first…

CFR 697 C. Lorenzo Von Matterhorn ( Tree Ad hoc )

Problem - C - Codeforces Well, I came up with an algorithm to look at every previous operations in each operation, and splitting bifurcating paths into two unique paths. That gave me a TLE though I believe is good to go in terms of time co…

CFR 697 B. Barnicle ( Ad hoc )

Problem - B - Codeforces Silly I really failed this one in the real test. Should have calmed down and check all cases manually since there weren't many to come up. void solve(){ string s; cin >> s; int dec_pos = s.size(); for( int i = 0; i …

CFR 606 D. Lazy Student ( MST )

http://codeforces.com/contest/606/problem/D Having MST, we will first want to sort the edges according to their weight. If we assign for each edge from which has smaller weight, if it is to be part of the MST, we will have to have it conne…

CFR Educational 14 D. Swaps in Permutation ( Union Find )

http://codeforces.com/contest/691/problem/D Being able to swap as many times, that means swappable positions are in the same disjoint set, where in each disjoint set values in those positions could be arranged arbitrarily. We have to manag…

CFR 606 C. Sorting Railway Cars ( Ad hoc )

http://codeforces.com/contest/606/problem/C Imagine if we decided exactly which cars are going to be moved, and of course each of them will be moved no more than once( quite obvious ). The ones left, putting them into a separate sequence, …

CFR Educational 14 C. Exponential notation ( Implementation )

http://codeforces.com/contest/691/problem/C Well, I thought it was not quite easy, some details need to be wary of. void solve(){ string s; cin >> s; int _t; // cut off leading zeros for( _t = -1; _t + 1 < s.size(); ++_t ) if( s[ _t + 1 ] …

POJ 1741 Tree( Centroid D&C )

#include <iostream> #include <algorithm> #include <vector> #include <queue> #include <stack> #include <set> #include <utility> #include <functional> using namespace std; typedef long long ll; typedef unsigned long long ull; typedef pair< int, int > pii; typedef vector< int > vi; typedef vector< vi >…</functional></utility></set></stack></queue></vector></algorithm></iostream>

HR Liars ( System of difference constraints )

https://www.hackerrank.com/challenges/liars 差分约束系统 - 维基百科,自由的百科全书 Well, it is a well known trick. For any inequality, take a[ u ] + W ≥ a[ v ], it will be equivalent to having a unidirectional edge with weight W, from u t…

CFR 688 E. The Values You Can Make ( DP )

Problem - E - Codeforces dp[ i ][ j ][ k ]: possible or not to make value k, out of a set with sum j, examining the first i coins in the array. dp[ i ][ j ][ k ] = dp[ i - 1 ][ j ][ k ] or dp[ i - 1 ][ j - C[ i ] ][ k ] or dp[ i - 1 ][ j -…

CFR 688 D. Remainders Game ( Chinese Remainder Theorem )

Problem - D - Codeforces According to the CRT, we have the conclusion that x % M = resolute value, if all values x % m[ i ] are known, where m[ i ] are all co-prime, and PRODUCT( m[ * ] ) = M. It is obvious that knowing x % p, where p % q …

CFR 688 C. NP-Hard Problem ( Bipartite Coloring )

Problem - C - Codeforces It's not difficult to see that only a bipartite graph will meet the requirements. bool dfs( int u, const vvi &G, vi &col ){ for( int v : G[ u ] ){ if( col[ v ] && col[ u ] == col[ v ] ) return false; if( !col[ v ] …

HR Find Median ( Priority Queue )

https://www.hackerrank.com/challenges/find-median-1 The classical two heap technique.. Don't forget to add ( int ) before calling size for container.. void solve(){ int N; cin >> N; vi A( N ); for( int i = 0; i < N; ++i ) cin >> A[ i ]; pr…

HR 2's complement ( Digit DP )

https://www.hackerrank.com/challenges/2s-complement It's easy to count the number of 1s that will be written in positive range with digit DP, simply remember the number of ways to reach some state will do. Let's think about cases where it'…

HR Quicksort 2 - Sorting ( Easy )

https://www.hackerrank.com/challenges/quicksort2 第一次寫 quickSort,只是想記錄一下。 void quickSort( int ql, int qr, vi &A ){ if( ql == qr ) return; int pivot = A[ ql ]; queue< int > small, large; for( int i = ql + 1; i <= qr; ++i ){ if( …

IOI '96 A Game ( Easy DP )

PEG Judge - IOI '96 - A Game 經典問題 就不多說了。 好啦,dp[ i ][ j ] 就是 [ i, j ] 的情況下先手最高得分,轉移的時候使對手最小,min-max問題。 void subSolve( int ql, int qr, const vi &A, const vi &pA, vvi &dp ){ if( dp[ ql ][ qr ] != -1 ) r…

HR Maximize Sum ( std::set )

https://www.hackerrank.com/challenges/maximize-sum 所有區間連續和都能表達成兩個前綴和的差,因此可以利用 set尋找適合的另一個前綴。 debug了一個小時,原來第一次插入 set的時候從 0開始了,然後就 WA半天摸不著頭緒。。。 void solve(){ int N; ll M;…

HR Absolute Element Sums ( lower_bound )

https://www.hackerrank.com/challenges/playing-with-numbers 差不多就是把不會變符號的先處理掉,然後把會變號的區間找出來做特別處理。做幾個 case就會發現會變號的部分造成的影響是 區間長度 * x + 2 * 區間和 注意 long long 然後我寫了快兩個小時,low…

HR Bike Racers ( Binary Search + Maximum Matching )

https://www.hackerrank.com/challenges/bike-racers 參考了這篇:用于二分图匹配的匈牙利算法 | Comzyh的博客 第一次寫二分圖最大匹配,原來沒有想像中那麼難。我想二分圖最大匹配基本上可以想成是求婚問題,一群男士和一群女士有一些關聯,這些關聯可以用…

HR Square Subsequences ( DP + Common Subsequence )

https://www.hackerrank.com/challenges/square-subsequences Sum up all the number of different common subsequences, for a = s[ 0, x ), b = s[ x ] + s( x, s.size() ) Ɐ 1 ≤ x with an additional restriction where s[ x ] must be taken in b. // n…

HR Vertical Sticks ( Expectation )

https://www.hackerrank.com/challenges/vertical-sticks I really consider this one quite tough. It took me literally an hour to understand how someone else's idea worked. algorithm - How to approach Vertical Sticks challenge? - Stack Overflo…