Блог пользователя Radewoosh

Автор Radewoosh, 11 лет назад, По-английски

A. Soldier and Bananas

We can easily calculate the sum of money that we need to buy all the bananas that we want, let's name it x.

If n >  = x the answer is 0, because we don't need to borrow anything.

Otherwise the answer is x - n.

B. Soldier and Badges

Let's count the number of badges with coolness factor 1, 2 and so on. Then, let's look at the number of badges with value equal to 1. If it's greater than 1, we have to increase a value of every of them except for one. Then, we look at number of badges with value 2, 3 and so on up to 2n - 2 (because maximum value of badge which we can achieve is 2n - 1). It is easy to see that this is the correct solution. We can implement it in O(n), but solutions that work in complexity O(n^2) also passed.

C. Soldier and Cards

It's easy to count who wins and after how many "fights", but it's harder to say, that game won't end. How to do it?

Firstly let's count a number of different states that we can have in the game. Cards can be arranged in any one of n! ways. In every of this combination, we must separate first soldier's cards from the second one's. We can separate it in n + 1 places (because we can count the before and after deck case too).

So war has (n + 1)! states. If we'd do (n + 1)! "fights" and we have not finished the game yes, then we'll be sure that there is a state, that we passed at least twice. That means that we have a cycle, and game won't end.

After checking this game more accurately I can say that the longest path in the state-graph for n = 10 has length 106, so it is enough to do 106 fights, but solutions that did about 40 millions also passed.

Alternative solution is to map states that we already passed. If we know, that we longest time needed to return to state is about 100, then we know that this solution is correct and fast.

D. Soldier and Number Game

Firstly we have to note, that second soldier should choose only prime numbers. If he choose a composite number x that is equal to p * q, he can choose first p, then q and get better score. So our task is to find a number of prime factors in factorization of n.

Now we have to note that factorization of number a! / b! is this same as factorization of numbers (b + 1)*(b + 2)*...*(a - 1)*a.

Let's count number of prime factor in factorization of every number from 2 to 5000000.

First, we use Sieve of Eratosthenes to find a prime diviser of each of these numbers. Then we can calculate a number of prime factors in factorization of a using the formula:

primefactors[a] = primefactors[a / primediviser[a]] + 1

When we know all these numbers, we can use a prefix sums, and then answer for sum on interval.

E. Soldier and Traveling

There are few ways to solve this task, but I'll describe the simplest (in my opinion) one.

Let's build a flow network in following way:

Make a source.

Make a first group of vertices consisting of n vertices, each of them for one city.

Connect a source with ith vertex in first group with edge that has capacity ai.

Make a sink and second group of vertices in the same way, but use bi except for ai.

If there is a road between cities i and j or i = j. Make two edges, first should be connecting ith vertex from first group, and jth vertex from second group, and has infinity capacity. Second should be similar, but connect jth from first group and ith from second group.

Then find a maxflow, in any complexity.

If maxflow is equal to sum of ai and is equal to sum of bi, then there exists an answer. How can we get it? We just have to check how many units are we pushing through edge connecting two vertices from different groups.

I told about many solutions, because every solution, which doesn't use greedy strategy, can undo it's previous pushes, and does it in reasonable complexity should pass.

Разбор задач Codeforces Round 304 (Div. 2)
  • Проголосовать: нравится
  • +58
  • Проголосовать: не нравится

»
11 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Fast editorial!

»
11 лет назад, скрыть # |
 
Проголосовать: нравится +37 Проголосовать: не нравится

Ironically I hacked people using a test case my code fails on

»
11 лет назад, скрыть # |
 
Проголосовать: нравится +8 Проголосовать: не нравится

Is it possible to solve E without using maxflow?

»
11 лет назад, скрыть # |
 
Проголосовать: нравится +23 Проголосовать: не нравится

cin / cout fails and scanf / printf passes.. BAD!

»
11 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

can any one tell what hash function will accept Question — C .

»
11 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится +3 Проголосовать: не нравится

i implement D with same idea !!

but got TLE !!!

my code !! 11222167 can anyone tell my why please :)

»
11 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

My solution for C is more tricky! I thought about using map/unordered_map with hashes but if this will fit in the TL then we will fit in TL without using them. So if the elapsed time is greater than 1.80 I just print -1 stop working.

»
11 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится +15 Проголосовать: не нравится

" It's easy to count who wins and after how many "fights" "

Does this sentence means that we can find number of fights without simulation ?

»
11 лет назад, скрыть # |
 
Проголосовать: нравится +38 Проголосовать: не нравится

How to prove that the longest path in the state-graph for n = 10 has length 106 in problem C ?

»
11 лет назад, скрыть # |
 
Проголосовать: нравится +3 Проголосовать: не нравится

Problem C has tag "dfs and similar"; does this only refer to calculating longest path or is there real way to solve with dfs?

  • »
    »
    11 лет назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    The alternative solution can be viewed as something similar to DFS. A vertex represents a state (queues of players). The only neighbour of a vertex is the result of a fight. Keep traversing the graph until there's no neighbours (game ended) or you visit a vertex twice (cycle).

»
11 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

8 successful hacking attempts on 2nd question and my own code failed! Feeling meh..!!

»
11 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I did figure out that Problem E needs Max Flow and build the graph. But I got Wrong Answer for grid printing on pre test. My Code: http://mirror.codeforces.com/contest/546/submission/11223671

Can anyone help me that where and what I am doing wrong ?

»
11 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Can someone explain what's the mathematical thought behind this statement?

primefactors[a] = primefactors[a / primediviser[a]] + 1

  • »
    »
    11 лет назад, скрыть # ^ |
     
    Проголосовать: нравится +7 Проголосовать: не нравится

    the number of prime factors of a number a is equal to the number of prime factors of the number a divided by any of its prime factors, plus one (the one you are removing).

    For example: primeFactors[8] = primeFactors[8 / 2] + 1.

    If "a" is a prime itself: primeFactors[a] = primeFactors[a / a] + 1 = 1

    Hope this helped

  • »
    »
    11 лет назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    if you choose a factor X such X=A*B (X is a composite number) you couldn't get the maximum score (you must select first A, and then B).

    That's why you must use a prime numbers:

    primefactors(a) = primefactors(a / PRIMEDIVISOR_OF[a]) + 1

    I hope this help you.

»
11 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

I solved C using four queues and comparing them each time with their original ones . I got AC. But what is the Hash thing most of the coders are talking about it in the comments?

»
11 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

In question B, how do you know that the max coolness is 2n — 1?

»
11 лет назад, скрыть # |
Rev. 2  
Проголосовать: нравится 0 Проголосовать: не нравится

I have written the solution for Soldier and Bananas and it works on my computer. But when I submit the solution I get a compilation error — "program.cpp:1:21: fatal error: iostream.h: No such file or directory #include<iostream.h> " I tried even #include but still the same result :(

I am new here and not sure how to submit the solution. I have written the solution in c++.

Thanks

»
11 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Can anyone suggest me some exercises about max flow in order to solve the problem E? (I am newbie in max-flow problems...) Thanks in advance.

»
11 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

It would be great if you could add solution code links (at least for problems D and E).

Thanks

»
11 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

тот момент, когда твое решение по D получило TL, потому что ты создал массив не на 5*10^6, а на 5*10^7 элементов :(

»
11 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

It's really disheartening when one's code fails only due to cin/cout. the D problem. TLE : 11222951 AC : 11234082

»
11 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Can anyone tell me why does this solution give AC? Problem D, I think worst case complexity in still N*sqrt(N).SOLUTION

»
11 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Please help me optimize this code — 11218347. I checked all the states to find the result.

  • »
    »
    11 лет назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    Because of n is too small , you can set a condition like if(step>1000) than the game will never end .

  • »
    »
    11 лет назад, скрыть # ^ |
     
    Проголосовать: нравится 0 Проголосовать: не нравится

    You compare with first state. It's wrong. Test 29:

    2 4 3 1 and 5

    4 3 1 and 2 5

    3 1 2 4 and 5

    1 2 4 and 3 5

    2 4 and 5 1 3!!!

    4 and 1 3 2 5

    1 4 and 3 2 5

    4 and 2 5 1 3

    2 4 and 5 1 3!!!

    You need to compare with all states.

    Combinatorics says that 11! ~ 4*10^7 states exists. So you can make 4*10^7 "fights". In optimize, you can use queue instead of vector. 11239957

    Another way — use set<pair<queue, queue>>. 11226550

    P.S. Sorry for bad English

»
11 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Can anyone find the bug of this code 11240132 for problem C? It looks completely ok, but can't even pass the 1st sample case!! I spoiled my 1 hour during the contest but couldn't understand why :(

»
11 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

For problem E, I guess you missed adding V links, from i (in the left subset) to i+V (on the right subset), this would means the soldiers which started on vertex i can stay there.

»
11 лет назад, скрыть # |
 
Проголосовать: нравится -14 Проголосовать: не нравится

The contest is too easy.

»
11 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Can someone please help. I am getting this as the flow for the first test-case in Problem E. This seems to be a valid flow. But I know this is not the final answer. Where am I going wrong? YES 3 -2 0 0 0 1 1 0 0 3 3 0 0 3 -1 1

P.S. https://ideone.com/bGtOyZ

»
11 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Не думал, что будет такая дискриминация по Задаче D. В реализации на C++ надо переделывать cin&cout на scanf&prinf, иначе не заходит, что очень грустно =(

»
11 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Can someone please tell me why this solution for problem E gives TLE ? :\ I used Edmonds Karp’s algorithm

12995795

»
11 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Problem D has tag "dp".If anyone can solve it using dp can you please explain the logic?

»
9 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

C question is a good one!

»
8 лет назад, скрыть # |
 
Проголосовать: нравится -8 Проголосовать: не нравится

Can anyone explain how they got length 106 in C.

»
8 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Can anyone tell how in Question C the total number of states cannot exceed 106?

»
7 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

In D, normal System.out.println(f[a]-f[b]) for each test case will result in a TLE, if you're using Java. So, it's better to use a StringBuilder, append test results to it, and output the composite result after processing all the test cases.

»
7 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Problem C is fun. I firstly solve it using unordered_map to track card 1 with card 2 and i received wrong answer on test 32, i have realized it's wrong hash, so I give 2 players play at most 1e6 turns, if no one wins, it means they draw. Although 1e6 is much greater than 106, it's the best way i can handle in this situation because I think the time limit is 1s, so i pass 1e6 turns =))

»
5 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Easy and Detailed solution for Problem D : https://mirror.codeforces.com/gym/328681/submission/116502457

»
4 года назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Why is my code giving TLE? can someone please check?

this is the code —

include <bits/stdc++.h>

using namespace std; typedef long long ll; vector prime_divisor(5000005); vector prime_factors(5000005);

void sieve(){ for(ll i=2;i<=5000000;i++){ ll curr = i; for(ll j=2;j<=curr;j++){ if(curr % j == 0){ prime_divisor[curr] = j; break; } } prime_factors[curr] = prime_factors[curr/prime_divisor[curr]] + 1; } }

int main() { // your code goes here prime_factors[0] = 0; prime_factors[1] = 0; sieve(); for(ll i=2;i<=5000000;i++){ prime_factors[i] += prime_factors[i-1];
}

int t;
scanf("%d",&t);
while(t--){
    int a,b;
    scanf("%d",&a);
    scanf("%d",&b);

    int ans = prime_factors[a] - prime_factors[b];
    printf("%d",ans);
    printf("\n");
}
return 0;

}

»
4 года назад, скрыть # |
 
Проголосовать: нравится +1 Проголосовать: не нравится

I am performing the same thing in D, but it is showing TLE, I am doing O(k), k = constant, for pre-calculating my prime factors sum and then O(T) for all the test cases.

Problem is my code runs on Test case 3 and 4 which are having same number of test cases as in Test case 5 and it runs on Test case 3 and 4 but shows TLE on Test Case 5..

Why this is happening if it is taking same time (as before taking test cases input operations are same for Test case 3 and 5, and after taking test cases, no. of test cases are same for Test case 3 and 5)

My solution : https://mirror.codeforces.com/problemset/submission/546/157507503

A Help would really be appreciated!