pedrohlf's blog

By pedrohlf, history, 8 years ago, In English

I noticed that only status="OK" responses come with the HTTP header "Access-Control-Allow-Origin: *", which is required for CORS requests. But if you request, for example, http://codeforces.net/api/user.rating?handle=fakehandle, the response comes with comment="user not found", which is a very useful information.

How should I ask/suggest this feature? Tagging MikeMirzayanov in this post? Will he receive a notification? Sending a private message?

Full text and comments »

  • Vote: I like it
  • +9
  • Vote: I do not like it

By pedrohlf, history, 8 years ago, In English

Hello, I am trying to solve a problem about probability, but I keep getting WA. The problem is from the brazilian contest IV Maratona Mineira. I can only find the problem in portuguese, so I will try to translate. I explained my idea and posted my code. My english is not that good, so any doubts, just ask me.

Problem:

Player A and B are playing a game of War(based on the board game Risk). A has NA armies in his territory and is in a war against a neighbor territory that B is defending with NB armies. The war between two territories is made up of several battles.

The rules of a battle are:

  • The attacker can attack a territory with the maximum of 3 armies.
  • The attacker must leave at least 1 army in his territory. Basically, the attacker can attack with min( NA -1,3) armies.
  • The defender can protect his territory with the maximum of 3 armies.
  • The defender can use all of his armies to protect his territory. The defender can defend with min( NB ,3) armies.

After each player has chosen a valid amount of armies to use in the battle (player A with XA armies and player B with XB armies), XA dices representing the attacker are thrown and XB dices representing the defender are thrown. The dices are numbered from 1 to 6. Then, the dices thrown by player A are sorted, as well as B's dices. After that, the min( XA , XB ) highest values obtained by each player are compared pairwise. For each comparison where the number obtained by attacker is higher than the number obtained by the defender, the defender loses an army. Otherwise, the attacker will lose an army.

For example: player A is battling with 3 armies against 2 armies of B. Player A gets numbers {1,4,2}, player B gets numbers {2,3}. In this case, player A loses one army and player B loses one army (because 4 > 3 and 2 <= 2).

Given the two numbers NA and NB, Player A wants to know the probability that he wins the war against player B, if both players play optimally.

Remember that a war is a sequence of battles. Player A loses the war if only one of his armies remains. Player A wins the war if there is no armies of player B left.

Input: The input has two numbers NA NB, where 1 <= NA, NB <= 10000.

Output: Probability that player A wins the war.

Sample test case (which my code is already failing):
Input: 5 4
Output: 0.2554

Time limit: 1.5s
Memory limit: 256MB

My solution:

code: http://pastebin.com/MdpHGjZT

code with memory optimization: http://pastebin.com/0A9NJf99

I created a vector called prob, where prob[x][y][z] is the probability that X armies attacking against Y armies will beat Z armies. (Ex: prob[3][2][1] is the probability that 3 armies are going against 2 armies and beat 1 of them). I'm pretty sure those probabilities are correct, since I checked them at least 5 times each.

The rest is just dynamic programming, where I test all possibilities. The base cases are:

  • a == 1 : return 0 (because the war is lost);
  • b == 0 : return 1 (because the war is won);

Can someone give some ideas on how to solve this problem? Thanks :D

Full text and comments »

  • Vote: I like it
  • +10
  • Vote: I do not like it