One Point Solution

  • Subscribe to our RSS feed.
  • Twitter
  • StumbleUpon
  • Reddit
  • Facebook
  • Digg

Friday, 7 September 2012

SRM 555: 5555555555555555555555

Posted on 17:39 by Unknown

Yeah, I posted it in the TCO blog: http://community.topcoder.com/tco12/srm-555/. Let me talk of what was going on to me:

Waking up at 6:30

I had it all planned up, and set things up to wake up at 6:30, so I could register (Really did not expect it to get any close to the registrants limit).

Unfortunately, I spent just about the whole period from 05:00 to 06:00 waking up to the same lame dream: that I missed the SRM. Only to find out that it was 5:00 and was way early. Arrg, I think it happened at least three times.

Div1 255

Meh, it seems that my brain was not awake when I first tried to solve this problem. At first I thought it was about splitting in multiples of 5 instead of powers of 5. My first code even had funny tricks to avoid considering a single "0" as a number that begins with leading zeros... The whole plan was to simply get the results modulo 5 when translating bits to integer. It was so clever...

But it failed examples. So, it needed powers of five... I had to fix stuff, but had some lame bugs and it delayed me. At the end , 215 points. Is way too slow. My only hope was the div1 555...

Div1 555

I actually saw the correct solution (although overcomplicated by dp for the sub-problem instead of even more binomials.) instantly. Of course, after coding everything here were bugs and bugs. I reached a state in which I was passing all examples but the second-last and third-last. Which was very puzzling, because the example that was seemingly the most complicated (last one) was correct. Surely, with 32 minutes left, I would eventually find the mistake, right? ... right?

I tried many things. I noticed the main difference between the examples I was passing and those I was not was that they were the only cases in which Rcount != Ccount, so I was trying to find any bugs related to it. Then I tried other things, and many others. But I effectively had 31 minutes with the same wrong code and no idea what to do. I even doubted the approach, but it really seemed right.

During the challenge phase, I checked out the other solutions and they were using the same logic. After some time off I opened it in the practice room, and could finally see the mistake right away. It is such a stupid mistake that it makes the drop to 18XX rating even more sad. You can see the code I had during the contest. The bug is with subProblem(er/2, Rcount) (and its column analogous), it should be: subProblem(er/2, H), of course, because the first expression makes no sense. I think I quickly replaced a different bug with Rcount and Ccount without putting a lot of thought and then completely missed it. I did not notice that it was also a main trait of the test cases I was failing, that W!=Rcount and H!=Ccount.

Anyway, this whole thing of SRMs made with mixed problem setters that turn out to have very easy div1 250 and div1 500 that everyone solves except for me that take too long or have bugs is really getting old. It is a perfect rating killing storm.

Codes!

div 2 easy, is really easy:

int theMax(vector <string> board) 
{
int h = board.size(), w = board[0].size();
int res = 0;
// pick a row
for (int i=0; i<h; i++) {
// pick a column
for (int j=0; j<w; j++) {
int ones = 0;
// count the ones
for (int a=0; a<h; a++) {
for (int b=0; b<w; b++) {
// the xor operation ^, is great to simlate toggling...
int o = ( (board[a][b]=='1') ^ (a==i) ^ (b==j) );
ones += o;
}
}
res = std::max(res, ones);
}
}
return res;
}

div2 medium / div1 easy: The lack of a c++ library function to convert a integer to binary is really lame.

int getmin(string s) 
{
int n = s.size();
int dp[n+1];
dp[0] = 0;
int INF = 100;

// make the powers of 5:
long long p = 1;
string pow5[100];
int q = 1;
pow5[0] = bit(p); // the bit(x) function returns x in binary.
while (p <= (1LL<<62) / 5) {
p *= 5;
pow5[q] = bit(p);
q++;
}

// dp part:
for (int t=1; t<=n; t++) {
dp[t] = INF;
for (int i=0; i<q; i++) {
// does it end with the i-th power of 5?
int len = pow5[i].size();
if ( (t - len >= 0) && ( s.substr(t-len, len) == pow5[i] ) ) {
//Yes. Yes, it does.
dp[t] = std::min(dp[t], dp[t - len] + 1);
}
}
}
return ( (dp[n] >= INF) ? (-1) : dp[n] );
}

div2 hard: This guy was completely not standard and not easy (at least not for me.) I think I needed a lot of thought to reach the good method to do a dp program. Like I said in the TCO blog, it is all about what starting point you use for the analysis. I was trying bottom-up dp instead of top-down and it was a true nightmare. (For me, this is the hardest div2 hard in a while).

const int MOD = 555555555; 
typedef long long int64;
#define long int64
long dp[555][554][2][2];
struct MuddyRoad2
{
int theCount(int N, int muddyCount)
{
memset(dp, 0, sizeof(dp) );
dp[0][0][1][1] = 1;
dp[0][0][0][0] = 1;

//dp[i][m][p1][p2] returns the total number of ways
// to set muddy segments if:
// * We have already assigned segments higher than i
// * p1 is the parity of the number of paths for i+1
// * p2 is the parity of the number of paths for i+2
// * There are m muddy roads left to mark.
//
for (int i=1; i<=N-1; i++) {
for (int p1 = 0; p1 < 2; p1++) {
for (int p2 = 0; p2 < 2; p2++) {
for (int m=0; m<=muddyCount; m++) {
long & res = dp[i][m][p1][p2];
// do nothing
res = dp[i-1][m][ (p1 ^ p2) ][p1];
// place a 0
if ( (m > 0) && (i < N-1) ) {
res += dp[i-1][m-1][0][p1];
res %= MOD;
}
}
}
}

}
// this way p1^p2 = 1 for i=N-1 :)
return dp[N-1][muddyCount][0][1];
}
};
#undef long

Div1 medium: This dreaded problem was easy but that's not a good thing when you do it wrong for a lame reason and everyone else solves it.

 
long C[3200][3200];
int count(int H, int W, int Rcount, int Ccount, int S)
{
long res = 0;
memset(C, 0, sizeof(C));
// Pascal's triangle is a combinatorics problem coder's best friend
for (int i=0; i<=3199; i++) {
C[i][0] = 1;
for (int j=1; j<=i; j++) {
C[i][j] = (C[i-1][j-1] + C[i-1][j]) % MOD;
}
}
for (int r = (Rcount % 2); r <= Rcount && r <= H; r += 2) {
for (int c = (Ccount % 2); c <= Ccount && c <= W; c += 2) {
int tem = (H - r) * c + (W - c) * r;
if (tem == S) {
cout << r << ", "<<c<<endl;
int er = (Rcount - r)/2; //extra
int ec = (Ccount - c)/2;
long prod = 1;
prod = (prod * C[H][r]) % MOD;
prod = (prod * C[W][c]) % MOD;
prod = (prod * C[H-1 + er][H-1]) % MOD;
prod = (prod * C[W-1 + ec][W-1]) % MOD;
res += prod;
}
}
}
return (int)(res % MOD);
}
Email ThisBlogThis!Share to XShare to Facebook
Posted in algorithm, badday, srm, topcoder | No comments
Newer Post Older Post Home

0 comments:

Post a Comment

Subscribe to: Post Comments (Atom)

Popular Posts

  • TopCoder SRM 557 - finally
    SRM 557 Explanation for division 1 Easy and match recap. Explanations for div2 easy and div2 medium. It feels like it has been ages since t...
  • SRM 589 Editorial
    I have finished writing the editorial for TopCoder SRM 589: http://apps.topcoder.com/wiki/display/tc/SRM+589 . As you most likely noticed. L...
  • SRM 590 recap and editorial
    Another week another Topcoder match. Not a great day. I had a bad flu and still do. Div1 500: The one with Xor Given a list of cards with nu...
  • SRM 546: relief
    I figured I should post something about this SRM. I've been very busy these weeks because the semester is ending and I tried to win a t-...
  • SRM 526: The killing wait for results
    While I wait for results, here is my perspective on this algorithm contest. It began with issues, it had to be postponed 15 minutes. TC has ...
  • SRM 554 div1 hard: TheBrickTowerHardDivOne
    Link to problem statement We got infinitely many bricks of dimensions 1x1x1 and C different colors. Count the number of towers of size 2x2...
  • SRM 533: Div1 500 MagicBoard explanation
    Finally solved it. It is a nice problem that is worth explaining in a post. You have a grid/board of at most 50x50 cells. Some cells contain...
  • Member SRM 505: Part 1
    So, let me explain a couple of problems from a Topcoder Member SRM that I wrote and never got an editorial. BTW, it was the last member SRM....
  • ListedLinks 2012-02-10
    Saturday Morning Breakfast Cereal comics: Grace Hopper's ghost That Oracle engineer blog post Oracle would really not like anyone to se...
  • Codeforces "Good bye 2013" round
    So it was a special round for coders of both divisions, problems ranged from the super easy problem A to the super difficult problems E,F,G....

Categories

  • acm
  • algorithm
  • answers
  • arenaplugin
  • badday
  • behindthescenes
  • bugs
  • c++
  • censorship
  • codechef
  • codeforces
  • contests
  • crocchamp
  • editorial
  • editorial.srm
  • embarrassing
  • explanation
  • gcj2013
  • gmp
  • goodday
  • google
  • googlecodejam
  • greed
  • groklaw
  • health
  • html
  • httpseverywhere
  • implementation
  • ipsc
  • ispc
  • java
  • kawigiedit
  • kindagoodday
  • lamebook
  • languages
  • lego
  • listedlinks
  • marathon
  • nasa
  • offtopic
  • ouch
  • postmortem
  • postportem
  • practical
  • probably_not_a_good_tip
  • problemsetting
  • programming
  • python
  • quora
  • rant
  • recap
  • slightlygoodday
  • snippet
  • srm
  • stl
  • strategy
  • swerc
  • tco
  • tco12
  • tco13
  • tco2012
  • tco2013
  • ternarysearch
  • topcoder
  • tricks
  • ubuntu
  • uva
  • vjass
  • vkcup
  • wc3
  • zinc

Blog Archive

  • ►  2014 (1)
    • ►  January (1)
  • ►  2013 (141)
    • ►  December (14)
    • ►  November (8)
    • ►  October (13)
    • ►  September (11)
    • ►  August (14)
    • ►  July (15)
    • ►  June (13)
    • ►  May (13)
    • ►  April (12)
    • ►  March (11)
    • ►  February (11)
    • ►  January (6)
  • ▼  2012 (94)
    • ►  December (5)
    • ►  October (6)
    • ▼  September (8)
      • Surprise test SRM the 25-th (Tomorrow!)
      • Codeforces #140 (Div 1)
      • Learning to find bugs
      • SRM 556 : #@!$%!
      • SRM 554 div1 hard: TheBrickTowerHardDivOne
      • Codeforces #137 (div2) C. Reducing Fractions - update
      • SRM 555: 5555555555555555555555
      • SRM 554
    • ►  August (6)
    • ►  July (3)
    • ►  June (5)
    • ►  May (8)
    • ►  April (10)
    • ►  March (20)
    • ►  February (16)
    • ►  January (7)
  • ►  2011 (51)
    • ►  December (7)
    • ►  November (12)
    • ►  October (5)
    • ►  September (1)
    • ►  August (3)
    • ►  July (4)
    • ►  June (3)
    • ►  May (7)
    • ►  April (3)
    • ►  March (2)
    • ►  February (1)
    • ►  January (3)
  • ►  2010 (9)
    • ►  December (4)
    • ►  October (1)
    • ►  June (1)
    • ►  May (1)
    • ►  January (2)
  • ►  2009 (1)
    • ►  December (1)
Powered by Blogger.

About Me

Unknown
View my complete profile