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

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

1009A - Покупка игр

Разбор
Решение (Vovuh)

1009B - Минимальная троичная строка

Разбор
Решение (Vovuh)

1009C - Надоевший подарок

Разбор
Решение (PikMike)

1009D - Взаимно простой граф

Разбор
Решение (PikMike)

1009E - Междугороднее путешествие

Разбор
Решение (BledDest)

1009F - Доминирующие индексы

Разбор
Решение (BledDest)

1009G - Разрешенные буквы

Разбор
Решение (Bleddest)
  • Проголосовать: нравится
  • +69
  • Проголосовать: не нравится

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

How did you got that diff_i formulae in E?

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

    Suppose there is a rest site right before the kilometer we are trying to cross. The probability of this is 0.5 (and then we get a1 as the difficulty).

    Ok, suppose then there is no rest site at position i - 1, but it is at position i - 2. The probability of this is 0.25, and then we get a2 as the difficulty.

    Then we consider the option when there is a rest site at position i - 3, but no rest sites at position i - 2 and i - 1. The probability of this is 0.125, the difficulty is a3.

    And so on, until we get to position 0.

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

Is there a reason for bounds on F to be 10^6 instead of something a bit smaller like 2*10^5? The problem ended up being pretty hard to solve in Java even with the intended solution. I understand not wanting more naive solutions to pass though.

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

    Probably to disallow the solution with euler tour and sqrt-decomposition.

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

    why would a linear solution fail? maybe you lost time in reading input just like me.

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

      Java is about 5x slower than C++, so many times, perfectly fine solutions in C++ fail in java. I've had java solutions fail many times because I was using long instead of int, or an arraylist/customdatastructure instead of an array, or recursion instead of iteration. I've also gotten accepted verdicts by rewriting slow java code in c++.

      Though you can lose time reading input with scanner, there are many other ways to lose time, ways that C++ or even python uses don't need to care about. I don't think any accepted solutions in java for this problem used recursion, but many did in C++.

      Case and point: When I made this comment, there were 25 accepted c++ solutions and 6 TLE c++ solutions. Java had 36 TLE solutions (from 6 distinct people) and only 10 accepted solutions (from 6 distinct people).

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

As mentioned by Redux on Announcement page, problem D can be solved by using recurrence relation such that if (a,b) is a co-prime pair then (2a - b, a), (2a + b, a) and (a + 2b, b) are also co-prime where a > b.

Base cases are (2, 1) for even-odd pairs and (3, 1) for odd-odd pairs. The list is also exhaustive as mentioned in this link so there is no overlapping also. The only breaking condition will be a > n or when m co-prime pairs are stored.

In this way, we will always jump to only co-prime pairs rather than checking every other pair.

So time complexity will also be much less than one given in editorial (Correct me if I am wrong).

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

    Lets say you check that m>=n-1 and phi(2)+...+phi(n)>=m. The probability of gcd being 1 is O(1) so we can randomly generate edges and check if gcd is 1. If we use map complexity will be: O(n+mlogm). But one can also prove that without randomizatiom complexity is O(n+m).

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

Problem G is almost same as the problem Poetic Word from ACM-ICPC Amritapuri Regionals 2017.

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

My solution 40344721 for problem E. I think it's easier

#include <cstdio>
#include <vector>
using namespace std;
typedef long long int ll;
const ll mod = 998244353;
int main()
{
	ll pow2 = 1, p=0, curp;
	int n;
	scanf("%d", &n);
	vector<ll> cost(n);
	for (int i = n-1; i >= 0; i--) scanf("%lld", &cost[i]);
	p += cost[0];
	for (int i = 1; i < n; i++) {
		curp = (pow2*(i + 2)) % mod;
		curp = (curp*cost[i])%mod;
		p = (p + curp) % mod;
		pow2 = (pow2 * 2) % mod;
	}
	printf("%lld", p);
	return 0;
}
  • »
    »
    8 лет назад, скрыть # ^ |
     
    Проголосовать: нравится -8 Проголосовать: не нравится

    I also use this method.

    But I don't know how to prove that

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

      Sequence from https://oeis.org/A001792.

      My proof: (n-segment means segment of length n).

      a[i] = 1, 3, 8, 20, 48 ... a[0] = 1, because a n-segment can be paved with a n-segment in one way. a[i] is equal the number of ways to pave the free parts of the n-segment for all possible positions of the segment of length >= n-i. (statement 1)

      System: a[0] = 1 a[i] = a[i-1]+2*2^(i-1) + Σ 2^(k-1)*2^(i-k-1) (from k=1 to i-1) = a[i-1]+(i+3)*2^(i-2), for every i>=1.

      Clarification: a[i-1] — because we count for all segments of length >=n-i.

      2*2^(i-1) is equal the number of ways to pave the free parts in case 1 in pic.

      Σ 2^(k-1)*2^(i-k-1) (from k=1 to i-1) is equal the number of ways to pave the free parts in case of movement (n-i)segment, where 2^(k-1) — left part, 2^(i-k-1) — right part. (case 2 in pic)

      Proof of "The number of ways to pave the n-segment by k segments is ((n-1)!/(k-1)!)/(n-k)!" you can find here http://kvant.mccme.ru/pdf/1999/05/kv0599levin.pdf after 11-task (it is on russian lang, but i cant find this in english). The number of ways to pave the n-segment by different segment is sum and equals 2^(n-1).

      Sorry for my bad english, i used google translate.

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

      Sorry, i forgot to attach the pic

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

        I got a more intuitive prove!

        define F(n,k) as number of distance notless than k. So the final result is Σ(k=1~n) F(n,k)*ak.

        F(n,n) = 1

        F(n,n-1) = 3

        F(n,n-2) = 8

        F(n,n-3) = 20

        ...

        It's easy to see that F(n,k) = F(n-x,k-x) when k>x.

        F(1,1) = F(n,n) = 1

        F(2,1) = F(n,n-1) = 3

        F(3,1) = F(n,n-2) = 8

        F(4,1) = F(n,n-3) = 20

        ...

        So, rewrite the result as Σ(k=1~n) F(k,1) * ak

        F(1,1) =1

        for F(k,1) , we calculate F(k+1,1): insert a new gap 1' between 0 and 1, it can be a stop , or not stop, and this covers all situations,

        if the 1' is a stop, it contribute F(k,1) + 2^k , 2^k from 0 to 1', the others remain F(k,1)

        if the 1' is not a stop, it contribute F(k,1) also, because, F(k,1) means count all gap nums, insert a nonstop don't change the gap nums.

        Together we get F(k+1,1) = F(k,1)*2 + 2^k.

        ✿✿ヽ(°▽°)ノ✿

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

What does " "pull" our depth arrays upwards" means? Thanks in advance..

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

Problem G , author's solution . We can just rebuild the graph with brute force and run dinic on it .

This could pass all tests if we do not use vector .

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

I have a question about the memory complexity of F: since every node's memory is its max "height", when the tree becomes a chain,it will rise to n(n+1)/2, And I can't run the data on my computer,so I wonder if it will get MLE

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

    If the tree is a chain, I believe you will actually only have 1 (mutable) array. All the nodes will point to that same array. Each time you go up the tree you just copy that array and add 1 to it. This works since you never need to use arrays lower down in the tree again

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

I am a little confused by the D solution given in the tutorial.In my opinion the O(N^2*log⁡(N)) is not going to fit in time because, as i know the time formula = TimeComplexity(MAXN)/10^9 which is O(N^2*log(N))/10^9 and converting N to MAXN(10^5) is (10^5)^2*log(10^5)/10^9 seconds, 10^10*16/10^9 = 160 seconds. Correct me if i am wrong(i am 100% wrong but i would like an explanation why). As i know from my little experience a O(N^2) solution for a TimeLimit of 2 second a MAXN of 10^5 is way to big.

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

For problem F, first example, why is the output 3 2 1 0 wrong? There may be several dominant indices for given node, right?

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

For problem 1009 D, how is it sufficient to prove that graph has m valid edges in order to make sure that graph is connected? (Like what I can't get is when is the program making sure that graph that it prints is "connected"?)

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

In this hacked solution, I used long double for the answer, and added up the intermediate means. The editorial says 10 byte float will pass. So, how many bytes does long double uses? 8? If so, how can we use a 10 byte or larger float in C++? I know I could've done this by first calculating sum using long long int, but just want to know out of curosity, it may help in some future contests. Thanks :)

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

My submission for E: http://mirror.codeforces.com/contest/1009/submission/40471843 Can someone tell me where the overflow is happenning and how to fix it?

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

Problem B : I never understood this line.Even till now

String a is lexicographically less than string b (if strings a and b have the same length) if there exists some position i ( 1 ≤ i ≤ | a | , where | s | is the length of the string s ) such that for every j < i holds a [j] = b [j] , and a [i] < b [i] .

What is string a ans string b, and indices i and j. What I understood was that string a is the output, and string b is the input string..!!!`` I cant get the relation between editorial explanation with the above statement given in the question. Help me pls regards ;)

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

40500124,(AC),40499958,(MLE)

For the problem 1009F's solution, and in the Struct state, why i used "vector a" and got the MLE while i used "vector *a" and got the AC? Thanks in advance...

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

I would like to share another approach to solve D, though it runs slower and harder to code. (Sorry for the poor English beforehand.)

Let rp[i] be a set storing all j <= n, such that gcd(i, j) = 1.
When constructing rp[i], we can count total number of pair (i, j) such that j > i and j is in rp[i], and terminate when this number >= m.
It is clear that the total number of found numbers won't exceed 2m + n when we stop.

The basic case, rp[1], is all integers in [1, n].
To construct rp[i], first we pick maximum prime factor p (can be found by sieve) of i, "remove" it from i. Formally, let i = p1k1 * p2k2 * ... * pzkz * pk, pi are primes, then we calculate the value temp = p1k1 * p2k2 * ... * pzkz, it can be found in O(logpn) by dividing i by p while i % p == 0.

Since gcd(i, j) = 1 if and only if (i, j) share no common prime factor, we have
rp[i] = rp[temp]  -  set of all numbers in rp[temp] which has prime factor p.

We can simply iterate through rp[temp] from small to large, when we found a number x, check whether it can be divided by p, and add it to rp[i] if it cannot.

By doing so, we may fail (found a number x but doesn't add it to rp[i]) many times, but for at most O(logpn) failures there must be one success (add x to rp[i]) before all those failures, because when we fail for some x = p1k1 * p2k2 * ... * pzkz * pk, we must have add y = p1k1 * p2k2 * ... * pzkz to rp[i] before. Since x <= n, k can only be numbers picked from [1, floor(logpn)], thus each success corresponds to at most O(logpn) failures.

Because p must be greater than 2, and success always comes before failures, this algorithm runs in O((n + m) * log2n).

Here is my submission

Please correct me if I am wrong.

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

Question 1009C: Annoying Present
I am getting a relative error = '0.0000206' in test case 9. I am using a double type variable to store the mean values for each new transformation. I have no clue how to reduce the error for the result. My code is here : https://mirror.codeforces.com/contest/1009/submission/40511515.
Any help is appreciated.

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

    Every single floating point operation causes precision error. Try to replace them by integer operations as many as possible.

    For example, you can sum up all x by integer operations, and add it to mean at last. In this way we use just a single floating point operation rather than O(m) floating point operations, therefore the precision error can be reduced.

    The same trick can also be applied to d.

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

My solution for Prob.E. Maybe it's better??

#include <cstdio>
#include <cstring>
#include <iostream>
#define P 998244353
using namespace std;

typedef long long LL;
LL n, a[1000005], ans, p = 1;

int main()
{
	scanf("%I64d", &n);
	for(int i = 1; i <= n; i++)
		scanf("%I64d", a + i);
	for(int i = n - 1; i > 0; p = (p << 1) % P, i--)
		ans = (ans + (n + 2 - i) * p % P * a[i] % P) % P;
	printf("%I64d\n", (ans + a[n]) % P);
	
	return 0;
}//Rhein_E

One Way to prove this:

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

my solution of F says Stack Overflow can someone suggest me some way to manage it on Test 131

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

Can anyone give a more intuitive explanation to the solution of problem G? I understand what's being done, but not why it's being done. Such as why are we decreasing the flows in the path and stuff.

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

Another (probably harder) way to solve E: We need to consider all possible partitions of a sequence of length N. We know that each chunk of length X in each partiton will add prefix_sum(X) to the answer, so we need to calculate number of chunks of length X for all possible Xs in all possible partitions. This is https://oeis.org/A045623 . There are a lot of different formulas to caluclate this (but I don't know how to derive them to be honest).

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

Can anyone tell me why I got wa in first solution but ac in second solution in problem C?The difference between these two is the variable type.

first solution:https://ideone.com/qEkY5S

second solution:https://ideone.com/vuRdGW

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

Can anyone tell me why I got wa in first solution but ac in second solution in problem C?The difference between these two is the variable type.

first solution:https://ideone.com/qEkY5S

second solution:https://ideone.com/vuRdGW

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

In the solution code the cur is calculated by multiplying a[i] with power of 2. But in the tutorial it was a[i]multiplied by 1/power of 2. Can anyone please explain this?

Also I didn't understand the last line of the tutorial where diff[i+1] was derived with the help of diff[i].

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

Why O(N2LOGN) is fast enough in D? i need explanation