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

Автор tourist, история, 9 лет назад, По-русски

Разбор задач Hello 2018 доступен на русском и английском языках. Приятного прочтения!

Возведение в степень по модулю
Новогодняя елка
Лимонад для вечеринки
Слишком простые задачи
Логическое выражение
Сильносвязный турнир
Степенная подстрока
Не превышай
Разбор задач Hello 2018
  • Проголосовать: нравится
  • +463
  • Проголосовать: не нравится

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

Not a single tutorial is available ? Only solutions ?

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

I'm liking this new trend.

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

why did this code accepted for Problem A?

cin>>n>>m; int a=pow(2,n); cout<<m%a<<endl;

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

why this solution worked? cin>>n>>m; int a=pow(2,n); cout<<m%a<<endl; what about overflow?!

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

I think on F, that ans[0] = ans[1] = 0..

Thanks for the contest, it was fun!

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

I used a greedy solution for question C .First I found out the bottle which has least value of cost per litre .Then I used a formula to give maximum possible number of bottles of this category(i.e. L/(volume of bottle of the category). Then i brute forced my solution ( i bought the best possible bottle at the moment by using a formula and then subtracted the volume from L, then repeat and repeat till L becomes <= 0 ). However, my solution failed on test case 14 of system testing. Here is my solution link My solution Any help would be appreciated. Thanks :)

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

    It fails for the case:

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

    I used quite similar greedy approach for problem C with one little modification and I got AC. Maybe someone will find it interesting. Firstly, let's sort the bottles by the cost per litre. Let's start from the cheapest of them going to the most expensive. Lets denote the size of the bottle i as V[i]. We have two different cases:

    • V[i] <= L. Here we have the best price because we going down from the cheapest bottle and we will buy all the V[i] litres. Then we can add to the answer this price and multiply that by the number of times. Of course we should subtract this V[i] multiplied by the number of times from the L. Then we can buy one more bottle with this size and it will be more litres than we need, but maybe it has the cheapest price and it will be the best variant? Otherwise, we can continue going down to the more expensive bottles per litre, but we will not buy the extra litres as it was before. We need just to update our realAns as you can see it in the code below:
    if(a[i].V <= L) {
        ll times = (L / a[i].V);
        ans += times * a[i].cost;
        L -= a[i].V * times;
        realAns = min(realAns, ans + a[i].cost);
    }
    
    • V[i] > L. We can buy this bottle and it will be our ans. Here we go:
    realAns = min(realAns, ans + a[i].cost);
    

    This solution seems nice to me. Here the it is 34026125 . Feel free to ask questions if something is not clear for you.

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

Great set of problems.I liked the 3rd one most.Credits goes to @tourist, my man. Thumbs up for all the problem setters too.

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

Is there different kind of solution for problem C?

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

E problem uses the same concept as F Problem of this contest.

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

Misread problem D. How to solve the problem optimally when k >= a pj instead of k ≤ apj in problem D?

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

    I think that the beauty of problem D comes when you think of it geometrically. Imagine that you have a point in the plane with coordinates .

    Then, for a fixed k, they are asking you a question about the points in a semiplane that lies to the right of the vertical line x = k, including the line (this is in the original statement with k ≥ apj, it lies to the left of that line if we choose k ≤ apj).

    With this in mind, I think that both problems look more like a "Sweep Line" kind of task, doesn't it? (this is how I approached problem D during the contest, and why I believe that solution 2 in the editorial is more natural, although this last part is clearly subjective)

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

      "it lies to the left of that line if we choose ....", not exactly. When we count problems in score such that k >=  apj, we can choose problems on either side of the line. The points for which k >=  apj are added to the score, however, we can choose points on other side of the line which don't satisfy this condition which will increase value of k (not increasing the score though), however this also moves the line and we can add new points with lesser ti such that k >=  apz increases and we can replace few points.

      Basically, when k >=  apk, the final points/score will not always be equal to the number of problems we choose.

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

        I misread it too and used the following idea. Let's sort our arrays in non-descending order of ai. Run a binary search on the value of x result points, and then say that the total amount of solved problems should be at least ai. Then for any j <  = i j-th element can give us a point if we will take it and we will suppose now that all the elements right to the i-th will not give us a point(if they actually will we'll consider it later). So we should take x elements from the i - th prefix with minimum total time, and then we should take ai - x elements from all the array with minimum total time(except the taken ones). It also can be noticed(but it is not neccesary) that this check procedure for a given x is to find out whether there exists such i that the sum of minimum x elements from the i-th prefix plus sum of minimum ai - x elements from the i + 1-th suffix is no more than T. It can be implemented with two segment trees(for prefix and suffix elements) (where trees are build on the array sorted by the time now and making a transition from i to i + 1 we do some changes, and find the sum of some first values).

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

My rating is now fibonacci sequence (2358) :)

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

Can someone find out why this code for D.Too Easy Problems (based on binary search) get runtime error? http://codeforces.me/contest/913/submission/34026112 Thanks in advance.

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

Thank You [user:tourist]for nice and pretty problem. Now I am a Blue Coder :'(. I am Very Happy For That. :)

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

ууу матан, дайте задачки которые возможно решить 10-ти классникам :(

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

Its my first time seeing an Integral being applied in competitive programming (in problem H)! :3

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

Solution to C using Binary Search: http://codeforces.me/contest/913/submission/34035650

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

In problem C purpose of doing the following is? L -= need << i;

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

Пытался решить задачу E (Логическое выражение) поиском в ширину (или даже больше на Левита похоже):

1) создаем массив string-ов formulas длинны 256, где по "вектору значений" функции будем хранить функцию ответ на задачу

2) существует всего 3 функции длинны 1 ("x", "y", "z"), добавляем их в очередь

3) пока очередь не пуста:
* вычисляем val — значение текущей функции на всех возможных аргументах
* если для formulas[val] еще не вычислен или мы нашли более удачную функцию, то обновляем значение в массиве и пытаемся "расширить"/"дописать" данную функцию, для нахожения остальных формул

формулу f я расширял по следующим правилам: !f, f&x, f&y, f&z, f|x, f|y, f|z, !(f), f&!x, f&!y, f&!z, f|!x, f|!y, f|!z, (f)&x, (f)&y, (f)&z, (f)|x, (f)|y, (f)|z, (f)&!x, (f)&!y, (f)&!z, (f)|!x, (f)|!y, (f)|!z

таким образом мне удалось найти только 218 функций.

видит кто-нибудь ошибку? или просто я побрел не в ту степь?:)

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

    Надо расширять до f & g и f | g для всех пар функций f и g, найденных к текущему моменту — тогда решение заработает.

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

      спасибо, я так тоже сделал, тогда находятся все функции, но не обязательно оптимальные по длинне / лексикографическому порядку

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

Can anyone tell why my code for C is failing for last sample input. The logic is similar to editorial. Link to my solution : My Solution

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

for problem G,the tutorial says "In this problem n ≤ 11. m = 6 is enough for such n",why 6 is enough?

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

    m = 6 is enough because if n ≤ 11 then 10m ≥ 2·2n + m.

    Let x = a·10m. If x is not divisible by 2n + m, we add to x. After that if x is divisible by 5 we add 2n + m to x.

    After such transformations x is divisible by 2n + m, x is not divisible by 5 and x ≤ a·10m + 2n + m - 1 + 2n + m. We know that 10m ≥ 2·2n + m. Therefore b = x - a·10m < 10m. That's exactly what we want.

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

      thank you! I didn't understand clearly why "10^m ≥ 2·2^(n + m)" <=> "there exists k satisfying equation (1) and (2)" earlier, and after read your tutorial more carefully, I got that "Consider equation . Every number in the equation is divisible by 2^(n + m), lets divide all the numbers by this. The result is — equation (3) where . Use the following lemma: Lemma For every positive integer t number 2 is primitive root modulo 5^t." shows why "10^m ≥ 2·2^(n + m)"<=>"there exists k satisfying equation (1) and (2)", and we just need to find any k under these assumes. In this way the range of an available m can be determined, very nice problem!

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

      when 10^m>=2^(n+m+1), then b can choose from [0,10^m-1], since 2^(n+m)-1<2^(n+m+1)-1<=10^m-1, so the values of b can cover [0,2^(n+m)-1], thus there exists some b satisfying ai*10^m+b=0(mod 2^(n+m)) and let x=ai*10^m+b, so x/2^(n+m)=2^(k-n-m)(mod 5^(n+m)), since 2 is a primitive root of 5^(n+m), so there exists t in range[0,4*5^(n+m-1)) satisfying 2^t=x/2^(n+m)(mod 5^(n+m)), then let k=n+m+t>=n+m, this k is the answer to the problem

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

    a way to prove 2 is primitive root of 5^k (k>=1) using induction: assuming (I) 2 is primitive root of 5^k, and (II) 2^phi(5^k)≠1(mod 5^(k+1)) first, about (II) 2^phi(5^(k+1))-1=2^5phi(5^k)-1= (2^phi(5^k)-1)*(2^4phi(5^k)+2^3phi(5^k)+2^2phi(5^k)+2^phi(5^k)+1) since 2^phi(5^k)=1(mod5^k) (using euler's theorem) and 2^phi(5^k)≠1(mod5^(k+1)) (by assuming),therefore gcd(5^(k+2),2^phi(5^k)-1)=5^k if 5^(k+2)|2^phi(5^(k+1))-1 then there must be 5^2|2^4phi(5^k)+2^3phi(5^k)+2^2phi(5^k)+2^phi(5^k)+1 but 2^phi(5^2)=1(mod 25), thus 2^(sphi(5^k))=(2^phi(5^2))^(s*5^(k-2))=1(mod 25) so 2^4phi(5^k)+2^3phi(5^k)+2^2phi(5^k)+2^phi(5^k)+1=5(mod25) so 2^phi(5^(k+1))≠1(mod 5^(k+2))

    then about (I) for t in range [0,phi(5^(k+1))), 2^t=1(mod5^(k+1))=>2^t=1(mod5^k)=>phi(5^k)|t let t=phi(5^k)*s, since 0<=t=phi(5^k)*s<phi(5^(k+1)) so 0<=s<5, consider 2^t-1=(2^phi(5^k)-1)(2^(s-1)phi(5^k)+2^(s-2)phi(5^k)+....+1) so 5^(k+1)|2^t-1 <=> 5|2^(s-1)phi(5^k)+2^(s-2)phi(5^k)+....+1 since 2^phi(5^k)=(2^4)^(5^(k-1))=1(mod5) so 2^sphi(5^k)=(2^phi(5^k))^s=1(mod5) so 2^(s-1)phi(5^k)+2^(s-2)phi(5^k)+....+1=s(mod5) thus 5|s and 0<=s<5 => s=0 thus for t in range [0,phi(5^(k+1))),2^t=1(mod5^(k+1))=>t=0, implying that 2 is primitive root of 5^(k+1)

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

I made two mistakes in Problem C during the contest. 34026884 First doesn't deal with a empty tail at last. Second fail to score the status and made a lot of unnecessary calculation which made TLE. My solution is too complex in all. Any suggestion in shorten the code?34038638

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

can't we slv B. Christmas Spruce with c ?

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

For problem C fourth example, why i get 44981600797903355?

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

Somebody please explain for Problem E,How the number of functions are 256 and number of states are 3256?

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

i use dfs to solve the problem C 34018956

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

Можно пояснить про E?

Заметим, что выражение однозначно задает то, какой функции оно равно и из какого нетерминала оно раскрывается.

Что подразумевается в данном случае под нетерминалом и почему количество возможных функций умножается на всего лишь на 3?

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

for problem 913D- Too Easy Problems

"The first observation is that if we solve a problem which doesn't bring us any points, we could as well ignore it and that won't make our result worse. Therefore, there exists an answer in which all problems bring us points. Let's consider only such answers from now on."

I don't think this is true,

Test Case

4 4
4 1
4 1
3 2
1 100

We solve 1,2,3 to make 1 points, Problems 1,2 don't bring us points but help increase the number of problems solved thus helping us solve problem 3 to earn 1 point.

Please let me know if I am stupid and have got it all wrong

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

In Problem D what if the score was only added when you have solved a[i] problems . Can anybody suggest a solution for that ?

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

can we solve E using Karnaugh maps??

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

In problem F, Strongly Connected Tournaments, can someone tell me how the law of total probability applies in each of the cases?

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

Why my solution failed on the 18th test case. On my machine, it's giving the right output. Why so?

34011797

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

    I'm guessing it's floating-point precision errors. Probably the calculation of log_2(m) is not exact when you do log_10(m)/log_10(2) as floating-point numbers, so the comparison against n may be wrong.

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

      But in this test case log_2(m) is giving 25 . Hence if the condition would fail in this case and the output would be m%pow(2,n) would give 0 . Where is the mistake here ?

      25 33554432

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

        yes, but you're storing all of these as floating-point numbers. Thus, the actual representation may not be exact, and doing equality comparisons or comparisons when the values should be equal to each other may be wrong. This might be why it failed on that test case.

        It may also be some weird C++ memory thing. I also discovered that if you change both variables to float (instead of one double and one float), then it passes all testcases. See the modified submission: 34077845

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

Has anyone solved E using Sum of Products(SOP) form, then minimizing the terms using K maps or algebraic minimization and then finally combining terms to get lexicographically smallest function ?

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

i didn't understand problem E. can anyone please explain me that problem a little bit.

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

can someone explain me please what does he consider by states in the editorial of E and what does nonterminals from the right part of the rule mean ? Help would be appreciated !

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

I have a question about the problem F.

When we compute cp(s, i), if we think that we add a new player with the largest index, then the following equation holds: cp(s, i) = cp(s - 1, i) × (1 - p)i + cp(s - 1, i - 1) × ps - i.

But I thought that I add a new player with the smallest index. The equation below is what I made.

cp(s, i) = cp(s - 1, i) × pi + cp(s - 1, i - 1) × (1 - p)s - i

I cannot find why I was wrong... I've spent almost two days in order to solve this problem...

Could you tell me what I am doing wrong?

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

    I explain about my equation.

    I'm going to add a new player whose index is the smallest. There have already been s - 1 players. After adding a new player, the number of players should be s.

    The first term, cp(s - 1, i) × pi, describe the case when a new player is not in the set of i players who lose to the others. The 'new added player' must win all the i players who are in the set. Thus, the probability is pi.

    The second term, cp(s - 1, i - 1) × (1 - p)s - i, describes the case when a new player is in the set. The 'new added player' must lose to all the s - i players who are not in the set. Thus, the probability is (1 - p)s - i.

    Would you tell me what is wrong?? Why should I add a largest-index-player like the official solution?

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

I have a binary search solution for C, I did binary search on cost available and checked if maximum volume that can be bought is greater than volume required and in this way calculated minimum price. I passed the pretests but got wrong on 14 test case, it was due to overflow. After correcting overflow I got it accepted. Here is my submission.

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

In Solution of 913C ,in the following line

// ans = min(ans, sum + (L > 0) * c[i]); //

what is significance of (L>0) ????

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

Can someone explain E clearly? I fail to understand what a state represents!

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

"It can be shown that the answer can be represented as , where P and Q are coprime integers. Print the value of P·Q - 1 modulo 998244353."

P·Q - 1 == , so it is a fractional number, isn't it?

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

Hey can someone tell me where I am wrong...this code is giving wrong answer on 17th test case... Problem B

http://codeforces.me/contest/913/submission/34023515

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

Wow, problem G was really great! I also really liked problem F. Good work YakutovDmitriy :)

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

The solution of these problems was very clear! Thanks for the analysis and solution.

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

Could someone check this submission, it turns AC into WA if I do not stop after solving more than "m" problems.

This is the binary search on the number of points. I check if I can solve at least "m" problem p_i such that m<=a_{p_i}. If I uncomment the following line, it will be accepted.

//if (ret>=m)             break;

https://codeforces.me/contest/913/submission/104032702

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

Good problems,I like it!