0

Codeforces round 883 Editorial

 6 months ago
source link: https://codeforces.com/blog/entry/118044
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
By natalina, 8 months ago, translation, In English

1846A - Rudolph and Cut the Rope

Author: Sasha0738

Hint 1
Hint 2
Tutorial
Solution
Rate the problem

1846B - Rudolph and Tic-Tac-Toe

Author: Sasha0738

Hint 1
Tutorial
Solution
Rate the problem

1846C - Rudolf and the Another Competition

Author: vladmart

Hint 1
Hint 2
Tutorial
Solution
Rate the problem

1846D - Rudolph and Christmas Tree

Author: vladmart

Hint 1
Hint 2
Hint 3
Tutorial
Solution
Rate the problem

1846E1 - Rudolf and Snowflakes (simple version)

Author: natalina

Hint 1
Hint 2
Hint 3
Tutorial
Solution
Rate the problem

1846E2 - Rudolf and Snowflakes (hard version)

Author: natalina

Hint 1
Hint 2
Hint 3
Hint 4
Tutorial
Solution
Rate the problem

1846F - Rudolph and Mimic

Author: Sasha0738

Hint 1
Hint 2
Tutorial
Solution
Rate the problem

1846G - Rudolf and CodeVid-23

Author: vladmart

Hint 1
Hint 2
Hint 3
Tutorial
Solution
Rate the problem

8 months ago, # |

HacksForces

  • 8 months ago, # ^ |

    Hahaha

    • 60 minutes ago, # ^ |

      Hahaha Hahaha

8 months ago, # |

Rev. 3  

+3

For E2 there is a simpler solution to check if there exists a such that .

You can rearrange the equation to . It follows that the only 's you need to check are and . This is a modified version of the official solution : code

EDIT : as davi-v pointed out,there is no need to check for .

  • 8 months ago, # ^ |

    Great!

  • 8 months ago, # ^ |

    can you explain why are checking the equation for k=2 and why we only floor(sqrt(n — 1)) and floor(sqrt(n — 1)) — 1 ?

    • 8 months ago, # ^ |

      Sure! I assume you meant ,not . If you deal with all (for example by precalculating a set of valid numbers),for all other the exponent must be exactly . That is because the minimmum exponent is and if the exponent was the sum would be larger than .Now we have to check that there exists a such that . This is the same as checking if there is a such that . Now, let . It is easy to see that . However, must be as large as possible so is either or when is a perfect square.

      • 8 months ago, # ^ |

        I want to say like for p=2 the equation would be 1+k+k^2. So why we have taken for p=2 if the no. Is not found in precalculated array.

        • 8 months ago, # ^ |

          Because for n <= 1018 and p = 2 the value of k could be approximately sqrt(n) = 109. But we can precalc values only for k <= 106

          • 8 months ago, # ^ |

            1+k+k^2=n how do we get to this ?

            • 8 months ago, # ^ |

              This is formula for the number of vertexes in minimal snowflake. 1 vertex at the initial value, k vertexes at the second layer and k^2 vertexes at the third one.

  • 8 months ago, # ^ |

    Very inefficient approach. It can be done with binary search sir !!!

    • 8 months ago, # ^ |

      Rev. 2  

      0

      I did think about BS in the cts, but I couldn't go with that, can you tell me that solution ?

  • 8 months ago, # ^ |

    can u explain "if (p > (long long)(1e18) / k) break;" Why this statement is necessary?

    • 8 months ago, # ^ |

      Rev. 2  

      0

      The if statement is necessary because otherwise the sum could overflow. If you have to check if , a neat way of doing this and avoiding overflow is checking if . Hope this helps!

      • 8 months ago, # ^ |

        Thank u for your reply, i got it:)

  • 7 months ago, # ^ |

    You don't need to check . Let . For to be , . Let . The question is: which numbers of the form , do we need to check?

    Substituting in , we get . Note that must be , as . We need to show that if , , so can't equal .

    Note , otherwise would be negative. Let . Substituting, we get

    If , , as and .

    • 7 months ago, # ^ |

      You are correct! Thank you for pointing out my mistake, I will make sure to correct it shortly. Also, I found a simpler proof to prove we don't need to check :

      I claim that for any number such that . When and when . Because is either or than ,we can conclude that .

8 months ago, # |

E2 is a superb binary search question, liked it

  • 8 months ago, # ^ |

    I did think about BS in the cts, but I couldn't go with that, can you tell me that solution ?

    • 8 months ago, # ^ |

      If you still haven't got the idea a more simpler bs solution is consider 1+k+k^2+..k^p, observe that this is less than k^(p+1) and as well observe that the maximum value of p can be 63 since any power greater than p and the sum>1e18, so from here we can bruteforce for all powers and for this power we can upper bound the value of k by n^(1/p), and binary search which value of k satisfies 1+k+...k^p==n, if any confusion feel free to ask.

      • 8 months ago, # ^ |

        Can you please explain the concept of n^(1/p). I am not getting this part.

        • 8 months ago, # ^ |

          So if it's something like if 1+k+k^2+..+k^p=n then k^(p+1)>n, you can easily deduce that I think(correlate it with binary recall that bit set at position i is bigger than all combined from i-1 to 1), so from k^(p+1)>n, we get got any k greater than n^(1/(p+1)) we definitely can't get the answer so that is the upper bound, hope it helps.

          • 8 months ago, # ^ |

            got it,thanks.

8 months ago, # |

Rev. 2  

0

Hi, can anyone help me debug my submission for E2? Don't know why I am getting WA.Thanks in advance.212818653

  • 8 months ago, # ^ |

    I don't know why I can't pass test9 :(((

  • 8 months ago, # ^ |

    I tried the similar approach in python (no overflow) and got WA on tc 16 after increasing the max value upto 1e25... (was AC initially and finally got hacked :(( )

    Further increment gave TLE... maybe the constraints weren't designed for the binary search solution... (unless I'm wrong, in which case I would appreciate if someone pls provided me with an AC solution)

    P.S: My initial approach in C++ was returning LLONG_MAX in the binpow function whenever overflow occured (used a check_overflow() function...) which gave WA on tc 5... Idk why...

8 months ago, # |

why is the D in editorial AC without setprecision? what differs from my submission . ik i messed up the setprecision in my code but still why

  • 8 months ago, # ^ |

    Precision is set before the solution code at the beginning of the main function

    • 8 months ago, # ^ |

      wow didn't notice. nice info ty

8 months ago, # |

At the time of contest 1846C is accepted so I have done 4th but next day it is showing wrong on test case 10. it's your fault not mine.

  • 8 months ago, # ^ |

    it's your fault

    • 8 months ago, # ^ |

      It's plateform fault because it is showing correct how can I know it fails on some test case

      • 8 months ago, # ^ |

        After contests end more tests are added

      • 8 months ago, # ^ |

        From the announcement: "The round will be hosted by rules of educational rounds (extended ICPC). Thus, solutions will be judged on preliminary tests during the round, and after the round, it will be a 12-hour phase of open hacks."

        This is part of typical D3 rounds: you will not know whether your solution passes all tests during the contests, and this challenges you to think carefully about special and corner cases and thus helps to improve your skills.

8 months ago, # |

Rev. 6  

+23

For E2, there is a much simpler code arising from the result of an inequality.

We can just iterate through the values of p. For each p, we need to check for only a single value of k which is floor(n^(1/j)). This is the only possible value of k that may satisfy the condition for a given value of p. This is because:

k^p < 1+k+(k^2)+....+(k^p) < (1+k)^p implies k < (1+k+(k^2)+....+(k^p))^(1/p) < 1+k

If n is equal to 1+k+(k^2)+....+(k^p), then k should be less than n^(1/p) and as per the above inequality k should be floor(n^(1/p)).

Here's the code.

#include <bits/stdc++.h>
using namespace std;

long long int sumpow(long long int x, int y) {
    long long int p=1;
    for(int i=1;i<=y;i++) {
        p=1+x*p;
    }
    return p;
}

int main() {
    int t;
    cin>>t;
    for(int i=1;i<=t;i++) {
        long long int n;
        cin>>n;
        int decider=0;
        for(long double j=2;j<(log(n)/log(2));j++) {
            long long int c=powl(n,1/j);
            if(sumpow(c,j)==n) {
                decider=1;
                cout<<"YES"<<endl;
                break;
            }
        }
        if(decider==0) {
            cout<<"NO"<<endl;
        }
    }
    return 0;
}
  • 8 months ago, # ^ |

    A very effective and simple approach, many thanks !!

8 months ago, # |

e2 is yituodabian

  • 8 months ago, # ^ |

    e2 hao nan

8 months ago, # |

So we can take one medicine multiple times in G?

  • 8 months ago, # ^ |

    Rev. 9  

    +20

    It doesn't matter if it's allowed or not since it would never actually be useful to take the same medicine multiple times anyway.

    I claim that if there is a sequecne of medicine that removes all symptoms and medicine is used multiple times, we can keep the last occurrence of and remove all earlier ones, and the sequence stays valid.

    Proof: Consider the symptoms medicine cures. It doesn't matter if these symptoms are cured or not before the last occurrence of since the last time of taking medicine cures these symptoms anyway. Thus, taking medicine also earlier has no effect.

    Now consider the symptoms medicine gives. It doesn't matter if these symptoms are cured or not before the last occurrence of since taking this medicine will give these symptoms anyways, and they need to be cured later. Thus, taking medicine also earlier has no effect.

    This means that taking medicine also earlier than the last occurrence has no effect on any symptoms.

    • 8 months ago, # ^ |

      Thanks a lot for the proof, its crystal clear!

8 months ago, # |

this round has really weak testcases. It's not only for B and C, but also for G. This is my submission for G, in no way should it pass but still it passes.

  • 8 months ago, # ^ |

    Your solution is fine (why I don't know), check the announcement page someone explained why this works

    • 8 months ago, # ^ |

      Rev. 2  

      0

      ummm... the solution takes at most 20 medicine.

      • 8 months ago, # ^ |

        Yes, but your solution is still correct. It can be shown that we never need more than 10 medicine to get fully cured, here is a short explanation (without proof). I can provide a proof if you can't see why that is true.

8 months ago, # |

I failed five times on E2. But G is a piece of cake,because UVA658 is quite similar to it.

8 months ago, # |

My Solution Pretty sure , I have the same solution as that of the editorial . Dunno why I got the TLE though

  • 8 months ago, # ^ |

    use c++

  • 8 months ago, # ^ |

    Use fast input. Passed in 202 ms. Code: 215151784

8 months ago, # |

Any idea why this submission is giving a WA for D? I have just subtracted the overlapping area instead of using the trapezium formula.`

https://codeforces.com/contest/1846/submission/212715188

  • 8 months ago, # ^ |

    setprecision() goes before output we set precicion for

8 months ago, # |

In problem G, what if Rudolf cannot transition from the initial state to a specific state using a particular medicine, but can still alleviate symptoms one by one?

8 months ago, # |

Rev. 2  

0

1846-C - Rudolf and the Another Competition

Any idea why my submission gets a TLE here. Time complexity is O(n*mlogm). 212937677

Spoiler

8 months ago, # |

Rev. 2  

0

can python sqeeze through C and D without TLE?

here's my code and they seem to not work even tho the complexity is exactly identical to the editorial

https://codeforces.com/contest/1846/submission/212613024 (C)

https://codeforces.com/contest/1846/submission/212645926 (D)

an explanation or help would be wonderful.

  • 8 months ago, # ^ |

    For D you can see my submission here 212968791 (I just change your code a little and get accepted)

    • 8 months ago, # ^ |

      thanks man. Apparently storing it in an array doesn't work as fast as I thought

  • 8 months ago, # ^ |

    Rev. 3  

    0

8 months ago, # |

My solution in B does not require a review of 4 cases.

8 months ago, # |

well , i am still a beginner, but in problem 3 ... i don't get how did we store the solution times for each student by this :

for(int i = 0; i < n; i++){ vector cur(m); for(int j = 0; j < m; j++){ cin >> cur[j]; }

while this should only store the last student only since it overwrites the existing values in each new outer loop ?

  • 8 months ago, # ^ |

    You're correct; this only stores the solution times for the last student. But notice that right after reading this, we already handle this student by calculating the optimal number of problems and minimum time penalty for this student. Thus, we don't need to store the problem-specific times for any longer and they can be overwtitten by new data.

    • 8 months ago, # ^ |

      if (i){
           if (make_pair(-task_cnt, penalty) < rud) ans++;
      } else rud = {-task_cnt, penalty};
      

      how does this work in C solution given in the editorial?

      • 8 months ago, # ^ |

        make_pair(-task_cnt, penalty) is used to create a pair with two values: -task_cnt and penalty. The negative sign before task_cnt is used to ensure that when comparing pairs, the pair with a smaller task_cnt value takes higher priority. If the -task_cnt is equal for two pairs, the comparison will be based on the penalty value. This pair is used for comparison in the following line:

8 months ago, # |

212951522 For problem F, why does this code give Idleness Limit Exceeded

  • 8 months ago, # ^ |

    You output mimic position before reading full response from OJ

8 months ago, # |

In E2, can somebody tell me the value of k that satisfy 29th test case i.e. n = 64000160000400001, I think this test case is wrong.

  • 8 months ago, # ^ |

    400,000

8 months ago, # |

Dial's algorithm can be used in G to achieve complexity. 212963777

  • 8 months ago, # ^ |

    Cuz the range of weight of a edge is 10^3(d) and there is 1024 vertices at max, so Dial's algorithm work fine but not more effective than Dijkstra's algorithm with STD in C++.

8 months ago, # |

Amazing problemset. Anyone easily can hacks.(hahaha)

8 months ago, # |

I did think about BS at E2 in the cts, but I couldn't go with that, someone please tell me the editorial of that solution, I really need it. Thank you.

  • 8 months ago, # ^ |

    Rev. 2  

    0

    Assume that we have an array {} satisfied , then we can see is increasing when k increases. So BS works on this array. We can use BS to check whether the given is in {}.Pick =2 and =minimum satisfied and BS.Then we can check , ... till .If the given doesn't stay in the arrays mentioned above , the answer is "NO".

    You don't really need to construct these arrays and store the value of them (MLE) , just calculate and compare with the given .

    • 8 months ago, # ^ |

      Thank you so much, I wish you best in the next contest <3

8 months ago, # |

In problem G, you should have mentioned that you can use a medicine several times because without it the problem seems quite complicated. By the way, does anyone know if it's possible to solve the problem if we can use each medicine only once?

  • 8 months ago, # ^ |

    Rev. 2  

    +8

    this is my submission for this problem submission. When we use one medicine at a time then we just have to make the following change in the code

            for (int i = 0; i < m; ++i) {
                for (int mask = 0; mask < 1 << 10; ++mask) {
                // cost[mask] = cost for getting this much protection
                    if ((damage[i] & mask) == damage[i]) {
                        minSelf(cost[save[i] | mask], cost[mask] + days[i]);
                    }
                }
            }
    

    I just swapped the two loops of dp just like we do in coin change dp problem.

8 months ago, # |

For problem E? How do we obtain the maximum possible value for k

  • 8 months ago, # ^ |

    Rev. 2  

    0

    Line 11: i<62

    It should be i<64 i think

8 months ago, # |

Rev. 4  

0

this solution is accepted in e1 but not in e2,provide me some reason, let n=1+x+x^2......+x^i ------ eq(1) now, we know, x^i<n<(x+1)^i (by binomal theorm) that, means x<n^(1/i)<x+1, now x can be gif(n^(1/i)), we will satisfy eq(1) with x, if it is true ans exist. we will do it for i between 2 to 61.

  • 8 months ago, # ^ |

    Rev. 2  

    0

    Hey your solution is too slow, the bound for the largest power is incorrect as well, check for i<=63, since 2^63~1e18, it will still tle with the current solution though, check this for an alternate approach my approach

    • 8 months ago, # ^ |

      Rev. 4  

      0

      it would be 60, and now it's tle, but complexity for each case woould be logn*logn*60

      • 8 months ago, # ^ |

        By your approach right, mine is a simple binary search that's just logn*63

        • 8 months ago, # ^ |

          my solution is giving tle now :(

        • 8 months ago, # ^ |

          your apporach is logn*logn*60 too

          • 8 months ago, # ^ |

            Rev. 2  

            0

            How is this 212715312 logn*logn*60?

            • 8 months ago, # ^ |

              leave it i have used inbuilt pow function in python

            • 8 months ago, # ^ |

              import sys
              input=sys.stdin.readline        
              for _ in range(int(input())):
                  n=int(input())
                  for i in range(2,61):
                      a=int(n**(1/i))
                      if a==1:
                          print("NO")
                          break
                      c=1
                      ae=0
                      t=0
                      while ae<n:
                          ae+=c
                          c=c*a
                          t+=1
                      if ae==n and t>2:
                          print("YES")
                          break
                  else:
                      print("NO")

8 months ago, # |

Oooh, F was sooo easy, why I didn't read it during the contest:(

8 months ago, # |

I'm not sure about this, but for E1 (and maybe E2 for some not too large), since , we would have be a divisor , and we can iterate all such k, noting that for some integer (if it exists):

We only need to check if $log_k{(n(k - 1) + 1)} - 1$ is an integer and if it is larger to equal to using repeated division, since the float log function can be expensive:

int t = 0; //we use this variable as a switch to check 

(iterate over all divisors k of n-1){
    int a =  n*(k - 1) + 1;
    int x = 0; (since we need p >= 2, x >= 3
    while (a % k == 0) {
        a /= k; x++; //we need x to count the exponent, which is p
    }
    x -= 1;
    if (a == 1 && x >= 2 ) t = 1; break; //if we reach here we can finish early
}
if (t == 1) cout << "YES";
else cout << "NO";

8 months ago, # |

if (i){
     if (make_pair(-task_cnt, penalty) < rud) ans++;
} else rud = {-task_cnt, penalty};

how does this work in C solution given in the editorial?

8 months ago, # |

8 months ago, # |

Rev. 3  

0

为什么使用等比数列预处理会WA?

#include <bits/stdc++.h>

#define int long long

using namespace std;

unordered_map <int, int> mp;
int n;

signed main() {
	
	// n == (k^(t+1) - 1) / (k - 1),等比数列
	// 下边生成在t >= 3时的所有n值
	for (int k = 2; k <= 1000100; k ++) {
		for (int t = 3; ; t ++) {
			if ((pow(k, t + 1) - 1) / (k - 1) > 1e18)
				break;
			__int128 fz = pow(k, t + 1) - 1;
			__int128 fm = k - 1;
			if (fz % fm == 0) {
				mp[fz / fm] = 1;
			}
		}
	}
	
	int t; cin >> t;
	
	while (t --) {
		cin >> n;
		// 如果在t >= 3的时候可以得出n,输出
		if (mp[n]) {
			cout << "YES" << endl;
			continue;
		}
		// 判断在t == 2时是否能够得出n
		// 即判断1 + k + k^2 = n是否有解,(2 <= k <= x, 其中x约等于1e9)
		int l = 2, r = 1e9 + 1000;
		while (l < r) {
			int mid = (l + r + 1) / 2;
			if (mid * mid + mid + 1 > n) {
				r = mid - 1;
			} else {
				l = mid;
			}
		}
		if (l * l + l + 1 == n) {
			cout << "YES" << endl;
			continue;
		}
		cout << "NO" << endl;
	}
	
	return 0;
}

8 months ago, # |

In E2 , for n greater 1e12 we can directly use Shree Dharacharya formula to know whether a k exist or not. And for k from 1 to 1e6 we can make a set of sum of G.P . I got this intuition in the contest but was getting TLE while calculating the sum of G.P . :( Got accepted in 1 try post contest.

8 months ago, # |

need help of E2 , don't know why can't pass test 9 :(( please help , thx a lot! 213588968

8 months ago, # |

if (p > (long long)(1e18) / k) break; Why this statement is necessary in E2

8 months ago, # |

Why there is Rudolph and Rudolf?

8 months ago, # |

Can somebody help me to spot why is giving Idleness Limit Exceeded?

https://codeforces.com/contest/1846/submission/213728138

I've tried several things, but still can't find the issue.

  • 7 months ago, # ^ |

    Same bro, did you find the issue?

    • 7 months ago, # ^ |

      No, I haven't found it yet. I've stopped debugging it for some days as well

8 months ago, # |

my very easy to understand solution for E2,#213938261

8 months ago, # |

Rev. 2  

0

I think this thing is simpler than editorial in G problem. Call the current status is S, the good effect is A, the bad effect is B. So when you take a medicine, the status transform S & (~A) | B

8 months ago, # |

I am getting WA on test case 2 for n = 7 in my submission for E1. I have tried my solution on my local also on the online available gnu c++ compiler it is giving correct answer that is "YES" while when I run it on here it give WA. I am not able to get why so? If anyone here can help me, I will be very thankful to him.

8 months ago, # |

Rev. 2  

0

Sorry for posting this late, can someone please tell why this gives wrong answer in problem E1 test 2, my approach instead of searching for the exponent I used log to find it, any help would be appreciated, Thanks in advance.

8 months ago, # |

Rev. 2  

0

Hi, could anyone help debug my submission? This is for 1846B. My submission is https://codeforces.com/contest/1846/submission/214305302

Thanks in advance!

  • 8 months ago, # ^ |

    Note that after finding a winner when you check rows and columns, you just break for loop, meaning you are checking diagonals even if you found the winner.

    • 8 months ago, # ^ |

      Yup, got it, thanks so much!

8 months ago, # |

What a deceitful problem C!

8 months ago, # |

what is k for 1000015000057 in 11th test in E2?

  • 5 months ago, # ^ |

    K=1000007 level=1

    • 5 months ago, # ^ |

      thanks

7 months ago, # |

Why do I get a Idleness Limit Exceeded on my code for F? [Code] Please help

  • 7 months ago, # ^ |

    Your code didn't link

7 months ago, # |

Hello guys, in 1846F - Rudolph and Mimic, continously getting time limit exceeded, even though I have verified my solution with editorial, and have tried to match it to the author's solution. Can anyone please let me know what could be the issue here :( -> 216464518

7 months ago, # |

Can anyone tell me why my Code is giving an output of 3 whereas the required output is supposed to be 2 for the following test case(Test Case number 2 — Problem 13).

217202106

Test Case:

7 2 9 10 3

5 3 2 9 7

6 1 10 5 7

2 6 9 10 4

10 5 7 8 9

6 months ago, # |

why this 221034056 isn't working for c am i missing something

6 months ago, # |

Rev. 3  

0

I did solve G without using Dijkstra is it hackable?

my idea to use dp is either i take the element either not i know sometimes you'll be forced to take medicine two times or 3 ...

but what i did that i did push the element n*m times so that max number we take that element is n times no more that that is that true? https://codeforces.com/contest/1846/submission/223492698

5 weeks ago, # |

alternate dp solution for G

code

Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK