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

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

1512A - Шпион обнаружен!

Автор задачи: MikeMirzayanov

Разбор
Решение

1512B - Почти прямоугольник

Автор задачи: MikeMirzayanov

Разбор
Решение

1512C - A-B палиндром

Автор задачи: MikeMirzayanov

Разбор
Решение

1512D - Испорченный массив

Автор задачи: MikeMirzayanov

Разбор
Решение

1512E - Перестановка по сумме

Авторы задачи: MikeMirzayanov

Разбор
Решение

1512F - Образование

Автор задачи: sodafago

Разбор
Решение

1512G - Короткая задача

Авторы задачи: MikeMirzayanov

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

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

Is there any simpler solution for B?

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

Videos out for E, F, G

E

F

G

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

We can do D in O(n) by observing two cases:

  1. One of the biggest two numbers was choosen as x, and the other one is the sum of all smaller numbers.

  2. Any smaller number was choosen for x and the biggest number is the sum of all other numbers.

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

Can someone point out the mistake in this? tried so many times but cant find it. the case is also not visible to debug. https://codeforces.me/contest/1512/submission/112547774 Verdict: wrong answer jury found answer, but participant did not (test case 88)

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

Is something wrong with the input validator for A? Perhaps I'm being dumb, but I can't find the error in my hacked solution, and it seems strange to me that the top twenty or so participants from the original ranklist have all been hacked. Moreover, a friend of mine reported that the test case 1 3 1 1 1 gives an "unsuccessful hacking attempt" verdict, when it should give invalid input, leading me to suspect that invalid test cases are being used to hack solutions. (UPD: I'm also hearing that the case 1 3 1 2 100 is successfully hacking solutions, which definitely shouldn't be happening, as there are more than two distinct elements in the array.)

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

why its giving TLE for problem G?

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

    Do not use pow, I do not know your intended solution but pow can result in both precision error and linear calculation (which makes your whole algorithm $$$O(n\;maxB)$$$ if you iterate through $$$n$$$ values and use pow to calculate some power $$$a^b$$$ in $$$O(b)$$$ time instead of $$$O(log\;b)$$$ time with binary exponentiation.)

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

    Well, it should TLE. For every query, you are looping from 2 to c to find appropriate n.
    Instead, you can for all values of c store the minimum n and then answer queries in constant time.

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

    Use Binary Exponentiation to calculate the value of a raised to b. pow function has a time complexity of O(n). while Binary Exponentiation has a time complexity of O(log(n)).

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

hold on can u declare an array of size 1e7 ?? wouldn't that give run time error

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

Time limits were too tight for problem G. Java and Python users are bound to get TLE, quite unfair.

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

Problem G with bigger constraint INVDIV

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

Problem C : https://ideone.com/dZeurz can someone please tell me a testcase where my solution fails i am not able to figure it out.

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

Can anybody provide knapsack dp solution for problem E;

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

Hi! Thanks for the editorial! Can anyone here check my submission for problem C and tell me in which test cases, my program failed? I am trying to find a corner case for so long.

If you don't have time to read my code, could you at least give me some counterexamples so I can test my code on them?

Thank you!

Here's my code: https://ideone.com/TZb6bR

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

Solution for C failing. Can someone help?

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

Why should we check whether the sum is below 2e9?

// the solution code
if (sum % 2 == 0 && sum <= 2'000'000'000 && have.find(sum / 2) != have.end()) {

I mean that b[i] has a constraint 1 <= b[i] <= 1e9. So if sum > 2e9 then sum / 2 > 1e9, as b[i] <= 1e9, we can't find sum / 2 in b[i]. Thus sum <= 2e9 should be redundant. But I got a wrong answer without it, and passed for adding it.

Appreciate for anyone who could explain it.

My WA submission: 112590439 My AC submission: 112590535

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

Can Someone help me with B , 112546789 I checked the test cases, and its still hard to decipher, perhaps someone has faced the same problem as me ?

»
5 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится
 if (sum % 2 == 0 && sum <= 2'000'000'000 && have.find(sum / 2) != have.end())

Can someone please tell me what is the meaning of this condition?

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

Can someone explain how binary search solution of F works ?

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

Can anyone help me by sharing the code of O(n) complexity sieve of problem G!!

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

112599399 This is my submission of problem F. Please someone tell me whats wrong with this..

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

MikeMirzayanov

I submitted G solution and got TLE in test case 1, but during the contest test, 1 was passed.

After final testing, I submitted Previous G's solution and got Accepted.

Contest time code: https://codeforces.me/contest/1512/submission/112549540

After final testing: https://codeforces.me/contest/1512/submission/112601755

can you check it?

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

In G's code can someone explain this line plz:

s[i] = s[i] * d[i] + 1;

Like how are we calculating when two same prime divisors comes (for eg. 4=2*2) and why/how it works?

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

    I got it. they are at first dividing the number by some prime which divides it. d[x] for the number x. (Remember we got the prime using the sieve.)

    Now, Notice that for $$${prime}^{r}$$$ we will have the divisor function equals

    $$${prime}^{r} + {prime}^{r-1} + {prime}^{r-2}+ ..... + 1.$$$

    If you think about it for some time then you will realize that that's what the expression $$$s[i]=s[i]*d[i]+1$$$ when ran in a loop till it is divisible with "j" is doing.

    the remaining part "j" (which gives us s[j]) has gcd equals one with this prime exponent so we can using the above expresssion simply multiply it.

    and get $$$s[i]=s[i]*s[j].$$$

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

Where does this comes from. Is there some there theorem? Seems like some property like that of phi function but I can not figure out why.

Use the multiplicativity of the function d(n):

d(a⋅b)=d(a)⋅d(b) if gcd(a,b)=1.

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

    Let me explain this...

    suppose some number has prime divisors a, b, c and all are prime so using above formula we can write

    d(a.b.c)=d(a).d(b).d(c)

    Now d(a)=a+1, d(b)=b+1, d(c)=c+1

    so d(a.b.c)=(a+1)(b+1)(c+1)

    If we expand the R.H.S we get,

    d(a.b.c) = a.b.c+a.b+a.c+b.c+a+b+c+1

    and that is what d(a.b.c) would be if you calculate, so both are equal.

    Let's take 30 as an example.

    => 30 = 2*3*5

    so using above formula d(30)=2*3*5+2*3+2*5+3*5+2+3+5+1

    => d(30)=72 & also d(30)=(2+1)*(3+1)*(5+1)=72.

    Ask me if you have any doubt.

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

    because if a decomposition of A of this form is p1 ^ a1 * p2 ^ a2 ... and a decomposition of B of this form is q1 ^ b1 * q2 ^ b2 ... then the sum of divisors of A equals to: (1 + p1 + p1 ^ 2 + .... p1 ^ a1) * (1 + p2 + p2 ^ 2 + .... p2 ^ a2) ... -> because no matter how we take numbers from each the parentheses are divisors of A and they are all present.

    Next-> d (a) = (1 + p1 + p1 ^ 2 + .... p1 ^ a1) * (1 + p2 + p2 ^ 2 + .... p2 ^ a2), d (b) = (1 + q1 + q1 ^ 2 + .... q1 ^ b1) * (1 + q2 + q2 ^ 2 + .... q2 ^ b2) i.e. gcd (a, b) = 1 then no p and q coincide. hence d (a * b) = d (a) * d (b)

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

If java coder use Arrays.sort() then We got TLE in problem D :) , Please solve this Supermagzzz

112527550

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

[Problem G] Same Code getting TLE and AC verdict. TLE Code — link AC Code — link Is it a joke?!

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

Can someone help me find the loophole in this approach for Problem C?

It keeps failing Test 2

112630518

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

how to calculate d(a*b) when a%b==0 and b is a prime by using linear sieve of Eratosthenes? Thanks.

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

Any proof for problem F ? why is it enough to calculate the number of days we should spend on a certain job a_i to get >= c and repeat this process taking the minimum for all jobs taking into consideration how many days will we stay in all the previous jobs ?

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

    Consider a pair of days today and tomorrow.

    If we got enough money to to an exam today it is allways benefical to do it today, never tomorrow.

    This means by induction that for a given number of exams, there is no better solution than doing the exams as early as possible.

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

Hello, i am very new to codeforces(this was my first contest)

My code for problem C works perfectly well on my compiler, running it with valgrind gives no memory errors, but the diagnostics fail multiple test cases(which ran successfully on my compiler). I don't understand this!!

Can someone please help me?

This is my code:

Thank you

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

In problem E, can someone help me understand this:

 ans = min(ans, cur + max(0ll, c — bal + a[i] — 1) / a[i]);
        ll newDays = max(0ll, b[i] — bal + a[i] — 1) / a[i];
        cur += newDays + 1;
        bal += a[i] * newDays — b[i];

I got the idea of what this is trying to do but i can't understand how exactly the mathematical operations are working.

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

In problem G since O(NlogN) is sufficient can someone please tell why can't we do something of type

int fact[1000003];

for(int i=1;i<N;i++)
    for(int j=i;j<N;j+=i)
    fact[j]+=i;

and store the first occurences of every value and rest will be -1.

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

It seems like solution of D from editorial doesn't fit in time.

UPD Oh, I'm wrong

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

in problem G the multiplicativity of the function d(n) : d(a⋅b)=d(a)⋅d(b) if gcd(a,b)=1. is their any proof for it?

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

    Write $$$n=p_1^{a_1} \cdot p_2^{a_2} \cdot ... \cdot p_m^{a_m}$$$ with $$$p_i$$$ primes. Then $$$d(n)=(1+p_1^1+...+p_1^{a_1})\cdot(1+p_2^1+...+p_2^{a_2})\cdot...\cdot(1+p_n^1+...+p_n^{a_n})$$$ (This was a wrong formula first, thanks to put_peace for correcting it!)

    Let {$$$q_i$$$} and {$$$r_i$$$} be sets of primes with empty intersection. Let $$$Q=q_1^{b_1} \cdot q_2^{b_2} \cdot ... \cdot q_m^{b_m}$$$ and $$$R=r_1^{c_1} \cdot r_2^{c_2} \cdot ... \cdot r_k^{c_k}$$$. Then $$$gcd(Q, R)=1$$$ and $$$d(Q \cdot R) = d(Q) \cdot d(R)$$$ using the formula for $$$d$$$.

    This isnt a formal proof, but it outlines the idea for a proof.

    See also https://en.wikipedia.org/wiki/Euler%27s_totient_function the totient function is similar to $$$d$$$ here.

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

    Just to see how it is correct, $$$d(p^{a}).d(q^{b}).d(r^{c}) = (1 +p + p^{2} + ... + p^{a}).(1 +q + q^{2} + ... + q^{b}).(1 + r + r^{2} + ... + r^{c})$$$
    if you expand the RHS, you can verify that it will give $$$d(p^{a}q^{b}r^{c})$$$

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

      yes got it,
      we can even think of it like
      d(p^a) as sigma(p^ai) where 0<=ai<=a
      similarly, d(p^a.q^b.r^c) = sigma(p^ai.q^bi.r^ci) for all 0<=ai<=a 0<=bi<=b 0<=ci<=c
      its like all combination of powers till a,b,c which can be written as
      d(p^a.q^b.r^c) = (1+p+p^2+...+p^a).(1+q+q^2+...+q^b).(1+r+r^2+...+r^c)
      here the rhs its same as choosing power of p and power of q and power of r (i.e same as getting all possible combinations for d(p^a.q^b.r^c)) same as d(p^a).d(q^b).d(r^c)

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

Why is E not doable with knapsack? https://codeforces.me/contest/1512/submission/113093207 Passed sample cases but WA on TC2

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

Can anyone please tell me why I am getting Wrong answer on test 2(test case 392) for problem 1512C (A-B Pallindrome). I tried lot but unable to find a case where it fails.

Here is my code :

#include <bits/stdc++.h>
using namespace std;
#define lli long long int 
#define ff first
#define ss second
#define endl '\n'
int main()
{
    int t;
    cin>>t;
    while(t--)
    {
        int a,b;
        cin>>a>>b;
        string s;
        cin>>s;
        int n=s.size(); int flag=0;
        if((a+b)!=n)
        {
            cout<<"-1"<<endl;
            continue;
        }
        for(int i=0; i<(n/2); i++)
        {
            if(s[i]=='0')
            {
                if(s[n-1-i]=='0')
                a-=2;
                else if(s[i]=='1')
                {
                    flag++;
                    break;
                }
                else
                {
                    s[n-1-i]='0';
                    a-=2;
                }
            }
            if(s[i]=='1')
            {
                b-=2;
                if(s[n-1-i]=='?')
                {
                    s[n-1-i]='1'; 
                }
                if(s[n-1-i]=='0')
                {
                    flag++;
                    break;
                }
            }
            if(s[i]=='?')
            {
                if(s[n-1-i]=='0')
                {
                    s[i]='0';
                    a-=2;
                }
                else if(s[n-1-i]=='1')
                {
                    s[i]='1';
                    b-=2;
                }
            }
        }
        if((n&1)==1)
        {
            if(s[n/2]=='0')
            a--;
            else if(s[n/2]=='1')
            b--;
        }
        if((a<0)||(b<0)||(flag!=0))
        {
            cout<<"-1"<<endl;
            continue;
        }
        for(int i=0; i<n; i++)
        {
            if(s[i]=='?')
            {
                if(((n&1)==1)&&(i==n/2)) // for middle element if n is odd
                {
                    if((a&1)==1)
                    {
                        s[i]='0';
                        a--;
                    }
                    else
                    {
                        s[i]='1';
                        b--;
                    }
                }
                else if(a>1)
                {
                    s[i]='0';
                    s[n-1-i]='0';
                    a-=2;
                }
                else
                {
                    s[i]='1';
                    s[n-1-i]='1';
                    b-=2;
                }
            }
        }
        if((a<0)||(b<0))
        {
            cout<<"-1"<<endl;
            continue;
        }
        cout<<s<<endl;
    }
    return 0;
}
»
5 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится

Can someone help and tell me why my solution is failing at test case 13 for prob D

https://codeforces.me/contest/1512/submission/115064448

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

G is such a great question!

»
5 лет назад, скрыть # |
 
Проголосовать: нравится 0 Проголосовать: не нравится
#include<bits/stdc++.h>
using namespace std;
long long q, j, x, n, a[10000001], f[10000001];
main(){
	n=10000000;
	for (int i=1; i<=n; i++){
		for(j=i; j<=n; j+=i)a[j]+=i;
		if(a[i]<=n&&!f[a[i]])f[a[i]]=i;
	}
	cin>>q;
	while(q--){
	    cin>>x;
	    cout<<f[x]-(!f[x])<<endl;
	}
}

why such a brute force solution could solve problem G? 1e6*1e6=1e12,isn't it?

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

Can someone please explain me the solution for E (editorial one), I have been trying for a long time but unable to understand how this condition came :

(s — sum <= r — l + 1).

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

null

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

Could someone explain for me the logic behind this part in the editorial code for problem G, please?

that part

I knew it was related to the multiplicativity of d(n) but I couldn't fully understand.

And this is

the whole code

Thanks in advance!

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

Hello I am new to programming. Just finished "1512A — Spy Detected!". And now I read the tutorial. I am wondering is my way more efficient than the tutorial and code showed?

My idea was to assume the first element of array is spy. Then compare first element until you hit different element. In that moment we have two options first element is spy or the one we just discovered. if (a[0]!=a[n-1] && a[0]!=[n-2]) is simple way to find out who is spy.

I would like a feedback on this idea, is this good approach or not? **I wrote my code in C

»
4 года назад, скрыть # |
Rev. 6  
Проголосовать: нравится 0 Проголосовать: не нравится
I solved E using simpler approach in O(N)

if we have $$$s$$$ and $$$r-l+1=k$$$, so $$$\frac{(k)(k+1)}{2}$$$must be greater or equal to$$$s$$$

First we will subtract the k Values from s (we will Know later Why) $$$m=s-\frac{(k)(k+1)}{2}$$$ and if we try to distribute m oranges in k boxes, then every box will have m/k orange(floor division), and remainder will be r oranges, r<k

k Boxes: $$$b_1, b_2, b_3, b_4, ....b_{k-1}, b_k$$$

k Boxes: $$$\frac{m}{k},\frac{m}{k}, \frac{m}{k}, \frac{m}{k}, ....\frac{m}{k}, \frac{m}{k}$$$

so we will distribute remainder and each box will take 1 orange until we have 0 remainder(I know that there is some boxes that we won't take orange).we will distribute remainder from right to left

after we distribute remainder we will distribute k values that we subtracted in the first: 1, 2, 3, .....k;

example will clarify:

suppose that we have $$$r-l+1=k=4, s=13, m=13-\frac{(4)(4+1)}{2}=3$$$

we will make each box have $$$\frac{m}{k}=\frac{3}{4}=0$$$ and we have $$$r=3$$$

k Boxes: $$$b_1, b_2, b_3, b_4$$$

distribute quotiont: $$$0,\ 0,\ 0,\ 0$$$

distribute remainders: $$$0,\ 1,\ 1,\ 1$$$

distribute K values that we subtract in the first : $$$1,\ 2,\ 3,\ 4$$$

the summation of three rows above: $$$0+0+1,\ 0+1+2,\ 0+1+3,\ 0+1+4$$$

the summation of three rows above: $$$1,\ 3,\ 4,\ 5$$$ $$$= 13$$$

mysubmission