One Point Solution

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

Saturday, 17 March 2012

SRM 537: oh my

Posted on 10:25 by Unknown
I am starting to write this as the intermission phase begins. I have submitted solutions for the 275 and the div1 500. I am fairly sure my div1 500 will survive (it is dynamic programming). But I'd say the probability my 275 fails is around 95%. I am currently first in my room, but all I need is somebody in my room to figure out the 250 is a good challenge mine and I will lose the money prize.

Div1 275
Awful stuff, I knew I was going to have issues with this problem the second I read it.

Anyway, note that eventually the distance between every two reachable numbers will be gcd(A,B). And that's really all I could get. I tried to do something that does simulations and does tons of hacks. Let's see if it works.

Div1 500
This was nice. You have some numbers. And there are K random steps. With 0.5 probability, the numbers are permuted using a permutation array. With 0.5, the numbers in each index are xor-ed against numbers with the same index in another array. Return the expected value for the first element after the K moves.

The constraint for K is at most 50, the limit on the number of ducks and the xor values (the elements of the array) is at most 10^9

There is a difference between quickly starting to code the solution and spending a lot of time wondering what to do. Permutation and Xor each can be seen as applying to only a single bit positions on all the numbers. Now, something funny about expected value. The expected value of the number in element 0 can be seen as (Expected value of the first bit) + 2 * (Expected value of the second bit) + .... etc. This is because the expected value is a linear application.

So, if you change the problem to finding the expected value of each of the (At most 30) bits of element 0. It becomes all about finding the probability that it will be 1. To find the probability that the x-th bit of element 0 will be 1, you can do a dynamic programming based on the following recurrence: F(v, bit, pos, K): The probability that the pos-th element will have a bit v (1 or 1) in the bit-th bit after K moves.

struct KingXMagicSpells 
{

double dp[2][30][50][51];
bool seen[2][30][50][51];
vector<int> ducks;
vector<int> spellOne;
// probability to get a bit in this position after K moves
double rec(int v, int bit, int pos, int K)
{
double & res = dp[v][bit][pos][K];
if (! seen[v][bit][pos][K]) {
seen[v][bit][pos][K] = true;
res = 0;
if (K == 0) {
// no moves
if ( v == ( (ducks[pos] >> bit) & 1 ) ) {
res = 1.0;
} else {
res = 0.0;
}
} else {
// one move...
// was there a permutation?
res = 0.5 * rec(v, bit, prev[pos], K-1);
// was there a xor?
res += 0.5 * rec(v ^ ( (spellOne[pos] >> bit) & 1 ), bit, pos, K-1);
}
}
return res;
}

int prev[50];
int n;

double expectedNumber(vector <int> ducks, vector <int> spellOne, vector <int> spellTwo, int K)
{
this->ducks = ducks;
this->spellOne = spellOne;
n = ducks.size();
for (int i=0; i<ducks.size(); i++) {
prev[ spellTwo[i] ] = i;
}
memset(seen, 0, sizeof(seen));
double res = 0;
for (int i=0; i<30; i++) {
res += ( (1<<i) * rec(1,i,0, K) );
}

return res;
}
};



Outcome
I finished typing this when the challenge phase ended. It seems 275 was not as much of a challenge fest as I thought. Even if I fail 275, I will be first place, and I am very sure that I will pass 500, unless I do something very lame somewhere that causes it to have random bugs.
Email ThisBlogThis!Share to XShare to Facebook
Posted in explanation, 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)
    • ►  August (6)
    • ►  July (3)
    • ►  June (5)
    • ►  May (8)
    • ►  April (10)
    • ▼  March (20)
      • Topcoder Open 2012 round 1A
      • Codeforces round #114. 167C: Wizards and numbers
      • Official TCO 2012 blogger
      • Codeforces round #114 (div1)
      • 5 reasons editorial votes in Topcoder are useless
      • Codeforces VK Cup round 2: (ouch)
      • Codeforces round #113 div2 (unofficial)
      • Writing SRM 538
      • SRM 537: oh my
      • Codeforces round #112
      • Language features: Rants and wishes
      • Google code jam registration - Just note something...
      • Codeforces: VK Cup round 1: Problem C: Abracadabra
      • Codeforces: VK Cup round 1 (update 1)
      • SRM 536: heh
      • SRM 535 editorial: The making of
      • Codeforces round #111 (div2 only)
      • SRM 535 : lame
      • A note about SRM 537 and 540 money prizes
      • The March of Destruction begins!
    • ►  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