jinlifu1999's blog

By jinlifu1999, history, 6 years ago, In English
Tutorial is loading...
Code
Tutorial is loading...
Code
Tutorial is loading...
Code
Tutorial is loading...
Code
Tutorial is loading...
Code
Tutorial is loading...
Code
  • Vote: I like it
  • +85
  • Vote: I do not like it

| Write comment?
»
6 years ago, # |
Rev. 2   Vote: I like it +3 Vote: I do not like it

Problem E should have 512MB(or 1GB) of Memory limit :(

My code Improved version

  • »
    »
    6 years ago, # ^ |
      Vote: I like it +8 Vote: I do not like it

    In the author's solution, DP state reduction is used.

    • »
      »
      »
      6 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Yeah, state reduction.

      But in polygon, the system warns if the jury solution solved the problem with more than half of time limit. Why not for memory limit :( I am so sad.

    • »
      »
      »
      6 years ago, # ^ |
      Rev. 3   Vote: I like it 0 Vote: I do not like it

      I am getting wrong answer on the 5th test case. Not sure what I am missing. Could you please help?

      I am using segment trees + lazy propagation for querying the maximum possible weight and d-value at an index and DP over these values.

      Here's my code: 50479788. Help is very much appreciated. Thanks.

»
6 years ago, # |
  Vote: I like it +5 Vote: I do not like it

I believe there is a slight correction in the proof of C. bi and ci form a corresponding grouping only if the following condition holds:

bi = cj if and only if bj = ci.

The proof still holds because each grouping can be mapped to a bi, ci and the optimal bi, ci can be mapped to a grouping.

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Probably, in B, it should be O(n + m + MAXC) instead of O(n + m).

  • »
    »
    6 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Did you use something called "bucket"? Actually it is not necessary to use that. A priority queue or a pointer is sufficient.

    • »
      »
      »
      6 years ago, # ^ |
        Vote: I like it +8 Vote: I do not like it

      Could you please explain how the pointer leads to linear solution? Since dishes are not ordered by cost, we need to sort them first, right? It is already .

      • »
        »
        »
        »
        6 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Oh ... I did make a mistake. I will modify this. Sorry for that.

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

In problem E , instead of the sweep line thing , i used a segment tree to cover the best red envelope in some range , and did the same Dp approach , however i'm still getting Wrong answer.

Any help would be appreciated , if my approach is wrong please let me know.

code : https://codeforces.net/contest/1106/submission/49283930

  • »
    »
    6 years ago, # ^ |
      Vote: I like it +1 Vote: I do not like it
    10 0 5
    4 9 10 1
    7 10 10 1
    3 8 9 1
    6 7 10 1
    6 6 7 1
    
    

    This is pretest 22 and the answer should be 2. However, you output 1.

    • »
      »
      »
      6 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Thanks for the test.

      I read over and over my code and it looks just okay , so im starting to wonder if the approach is just wrong ..

      • »
        »
        »
        »
        6 years ago, # ^ |
          Vote: I like it +1 Vote: I do not like it

        Well I don't know how to use segment tree with the operator max, except that there is a technique called Segment Tree Beats. Hope you figure out your problem. :)

  • »
    »
    6 years ago, # ^ |
      Vote: I like it +2 Vote: I do not like it

    Before assigning a value to your lazy array you should check that you don't have a lazy value in your current node or that the value you want to put is better that your current lazy.

    Something like this 49285040

»
6 years ago, # |
Rev. 4   Vote: I like it 0 Vote: I do not like it
  • »
    »
    6 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Try playing around with the following test:

    4 3
    1 2
    2 4
    1 3
    

    Your solution will print 1 2 4 3 which is not the correct answer.

    • »
      »
      »
      5 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Can you tell the correct order

    • »
      »
      »
      5 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      hey .. can you please tell why sorting doesn't work in this case

      • »
        »
        »
        »
        5 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        Because it doesn't care the path. At node v, you can go back to parent(v) and go to other nodes with smaller number.

»
6 years ago, # |
Rev. 2   Vote: I like it -30 Vote: I do not like it

In my opinion problem F is unreasonably hard for div2 (and not only to come up with a solution but also to implement)

»
6 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

My WA approach for E-
Sort all events Maximum Coins and then Maximum d.
Iterate on all events. I'm assigning a specific event for each moment, which I will select If not blocked and I can select an event.

Made dp states similar to editorial.
Took minimum of -
If blocked n-1(times),d-1(no of times blocked in i<n).
And If I can add specific event of ith moment in n-1,d then added or n-1,d.

Any specifice reason why does this fails?
To me both sweep and my approach look same.

Found reason why this approach will fail. :P

  • »
    »
    6 years ago, # ^ |
      Vote: I like it +13 Vote: I do not like it

    After that, we use a set with a sweep line to deal with it. — How ??
    The transition is trivial and you can take a look at it in the code. — Not for me.

    Please elaborate exactly what you are doing. And why you are updating f[j & 1][a[i].d + 1]

»
6 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

My solution for D is getting MLE. can someone help me? https://codeforces.net/contest/1106/submission/49291115

»
6 years ago, # |
  Vote: I like it +15 Vote: I do not like it

Today I learned the big-step baby-step algorithm, and about primitive roots. Problems E and F are very interesting.

I'm sorry that this round faced technical difficulties, but I enjoyed it nonetheless.

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Can someone explain why i am getting a "runtime error: constructor call on misaligned address" on problem E. Any help would be appreciated. https://codeforces.net/contest/1106/submission/49293426

  • »
    »
    6 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    The diagnostic message is not for your solution. Your solution gave a wrong answer that the checker wasn't prepared to handle, and the checker faced a runtime error. The diagnostic message is for the checker.

    Your verdict is simply WA.

    • »
      »
      »
      6 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      But the diagnostics mentions function<bool (std::pair<std::pair<int, int>, std::pair<int, int> > &, std::pair<std::pair<int, int>, std::pair<int, int> > &)> which i have used in my code.

      • »
        »
        »
        »
        6 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        I am very surprised to see that. I was certain that it might be due to the checker. But now I am not that sure.

        One of the things that is curious is that you asked for GNU C++ compilation, but the diagnostic message mentions clang++ and MSVC. So that's suspicious indeed.

        Please check once if your comparator is well defined.

        • »
          »
          »
          »
          »
          6 years ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          Checked the comparator.Looks alright.

          • »
            »
            »
            »
            »
            »
            6 years ago, # ^ |
              Vote: I like it 0 Vote: I do not like it

            I think there might be something wrong with your solution, I copied it and changed it to this and no more exception but still wrong answer on test 7.

            #include<bits/stdc++.h>
            
            #define mp make_pair
            #define mt make_tuple
            #define x first
            #define y second
            #define all(v) (v).begin(),(v).end()
            #define rall(v) (v).rbegin(),(v).rend()
            #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
            #define ve vector
            #define forn(i,n) for(int i=0;i<(int)n;i++)
            #define for1(i,n) for(int i=1;i<=(int)(n);i++)
            #define ford(i,n) for(int i=(int)(n)-1;i>=0;i--)
            #define fore(i,a,b) for(int i=(int)(a);i<=(int)(b);i++)
            #define pb push_back
            #define print(v) for(int i:v) cout<<i<<" ";cout<<endl;
            #define emp emplace_back
            #define input(v) for(int i=0;i<n;i++) cin>>v[i];
            #define sz(a) (int)(a.size())
            
            using namespace std;
            typedef long long ll;
            typedef vector<int> vi;
            typedef pair<int,int> ii;
            typedef vector<ii> vii;
            typedef vector<vi> vvi;
            
            
            
            
            int main(){
            	IOS
            	int n,m,k;cin>>n>>m>>k;
            	vector<pair<ii,ii>> v(k);
            	forn(i,k){
            		cin>>v[i].x.x>>v[i].x.y>>v[i].y.x>>v[i].y.y;
            		v[i].x.x--;v[i].x.y--,v[i].y.x--;
            	}
            	sort(all(v));
            	// for(auto& p:v){
            	// 	cout<<p.x.x<<" "<<p.x.y<<" "<<p.y.x<<" "<<p.y.y<<endl;
            	// }
            	vii best(n,{0,n}); //money d
            	int ptr=0;
            	auto com = [](const pair<ii,ii>& a, const pair<ii,ii>& b){
            			if(a.y.y==b.y.y)
            				return a.y.x<b.y.x;
            			return a.y.y<b.y.y;
            		
                };
            	priority_queue<pair<ii,ii>,ve<pair<ii,ii>>,decltype(com)> pq(com);
            
            	for(int i=0;i<n;i++){
            		for(;ptr<k;ptr++){
            			if(v[ptr].x.x>i) break;
            			// cout<<"ptr"<<ptr<<" "<<v[ptr].y.x<<" "<<v[ptr].y.y<<endl;
            			pq.push(v[ptr]);
            		}
            		// best[i].x=0;best[i].y=i;
            		while(!pq.empty()&&pq.top().x.y<i){pq.pop();}
            		// cout<<pq.empty()<<endl;
            		if(!pq.empty()){
            			// cout<<(pq.top()).y.x<<(pq.top()).y.y<<endl;
            			best[i].x=(pq.top()).y.y;
            			best[i].y=(pq.top()).y.x;
            		}
            		// cout<<i<<" "<<best[i].x<<" "<<best[i].y<<endl;
            	}
            	// cout<<"ptr"<<ptr<<endl;
            	int iter=0;
            	// cout<<"==========="<<endl;
            	// cout<<n<<" "<<m<<endl;
            	vector<vector<ll>> DP(n+1,vector<ll>(m+1,-1));
            	// cout<<"s"<<endl;
            	function<ll(int,int)> sol=[&](int i,int j){
            		// cout<<i<<" "<<j<<endl;
            		if(i>=n) return 0LL;
            		if(DP[i][j]!=-1) return DP[i][j];
            		DP[i][j]=sol(best[i].y+1,j)+(ll)best[i].x;
            		if(j){
            			DP[i][j]=min(DP[i][j],sol(i+1,j-1));
            		}
            		// cout<<i<<" "<<j<<" "<<DP[i][j]<<endl;
            		return DP[i][j];
            	};
            	cout<<sol(0,m)<<"\n";
            
            
            
            
            }	
            
            • »
              »
              »
              »
              »
              »
              »
              6 years ago, # ^ |
                Vote: I like it 0 Vote: I do not like it

              Figured already.I missed a condition in the DP part of code,which in no way could have caused byte misalignment.The diagnostic was incorrect. Thanks anyway.

            • »
              »
              »
              »
              »
              »
              »
              6 years ago, # ^ |
                Vote: I like it +3 Vote: I do not like it

              Your defines are awful.

»
6 years ago, # |
Rev. 3   Vote: I like it +5 Vote: I do not like it

Can anyone explain the solution of problem E in detail,please ? The "SWEEP LINE" term mentioned in the editorial isn't clear to me.I understood the second part but i'm stuck with the first part. Thank you.

  • »
    »
    6 years ago, # ^ |
    Rev. 5   Vote: I like it +16 Vote: I do not like it

    I'll try to explain, but my words might be unclear. Perhaps you could take a look at my submission. It might help.

    • In my code dp[t][dist] = minimum possible money that can be earned by Bob when starting from time t, and dist disturbances are remaining with Alice. The answer that we are looking for is dp[1][m].
    • The recurrence for this would be as follows: If best letter at time t is (starti, finishi, delayi, rewardi), then the recurrence would be as follows:
    dp[t][dist] = min(dp[t + 1][dist - 1], dp[delayi + 1][dist] + rewardi)
    • The first case corresponds to Alice using up a disturbance, and the second case is when she doesn't. If there is no letter that Bob can open at time t, then the dp recurrence is:
    dp[t][dist] = min(dp[t + 1][dist], dp[t + 1][dist - 1])
    • You can work out the base case of this dp yourself.
    • A more fundamental question is that why does this DP work? Especially because we are not keeping any state of what are the envelopes used up or remaining at a particular time.
    • One insight to have is this: If we could ignore the start and finish times of the envelopes, then there is a preference order amongst all the envelopes. (Basically prefer the ones that give more prize, and choose the ones that will delay more).
    • However, at a time instant t, you can only choose amongst the envelopes that belong to that time instant, i.e. starti ≤ t ≤ finishi. You don't need to worry about any other envelopes. In particular you don't have to worry that these envelopes are already chosen, because if it had been chosen already, then in the recurrence, it will check only after the delay.
    • So if we are iterating through time backwards and keeping a Priority Queue of the valid envelopes, then we need to insert and remove envelopes at the correct times (at finishi and starti) when they become valid and invalid. This is what is being referred to as SWEEP LINE. Maybe the phrase is used a bit incorrectly used in this case (but there are some strong similarities in the ideas).
»
6 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

I tried Problem E.
I let dp[i][j] be the coins Bob can obtain in time j with Alice already disturbed i times.
Let w[j] be the coin from the next envelope Bob can get if he is free at time j.
Let d[j] be the next time Bob can be free after getting this envelope.
then dp[m][j]=dp[m][d[j]]+w[j]
dp[i][j]=min(dp[i+1][j+1],dp[i][d[j]]+w[j])
But I got a Wrong Answer at test 5, which is very hard to find out the wrongs in my code.
I'll keep trying...

  • »
    »
    6 years ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    Please notice that f[ * ][m] might not be the optimal policy. Alice may not use all m chances to disturb Bob, which might reach a smaller answer.

    • »
      »
      »
      2 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Hi! It has been a long time, but if you could help me it'll be great. I don't get why my solution fails though the dp seems to be same as above.

      https://codeforces.net/contest/1106/submission/157647999

      Failing at TC 10. Seems to be some error in base condition. Don't get how.

      • »
        »
        »
        »
        2 years ago, # ^ |
        Rev. 2   Vote: I like it 0 Vote: I do not like it

        See your output on TC 10. The answer could be larger than 2147483647, so initiate the answer to be some larger number instead of INT_MAX.

»
6 years ago, # |
  Vote: I like it +5 Vote: I do not like it

In 1106A, scanf("%s", a[i] + 1);, why a[i]+1 is used? Why it is needed to add 1 there?

  • »
    »
    6 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    If you do not add 1, the variable j should start from 0.

»
6 years ago, # |
  Vote: I like it +13 Vote: I do not like it

There's something worthy to note about problem F that I came up with while testing. After reducing the problem to finding an integer x such that xp = m using matrix expo, we can find x in O(log(n)) but if and only if gcd(p, mod - 1) = 1. Let inv be the modular inverse of p with respect to mod - 1:

  • »
    »
    6 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    It was this line of thinking that led me in quite a wrong direction in the contest.

    For the case where gcd(p, mod - 1) > 1, then we can reduce it to the case where p divides mod - 1 by the following steps:

    g = gcd(p, mod - 1) = ap + b(mod - 1)

    Then,

    Because mod - 1 has only three prime factors 2, 7 and 17, I was trying to find a special solution for those three powers, and entered into the number theory rabbit hole of quadratic residues.

  • »
    »
    6 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Is there a way to solve the equation when gcd(p, mod-1) != 1 or this solution works only when gcd = 1?

»
6 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

I also did the same thing mentioned in the editorial for B problem . Just used a set and simulated the process . I am getting a runtime error on the first case but in my local machine I am getting a right output . Can someone help me with the error ?

Link for the submission is : 49266362

»
6 years ago, # |
  Vote: I like it +5 Vote: I do not like it

At first I think D is very similar to NOIP2018 D2T1,but at last, it turns out that I am wrong.The code is same as the wrong code I wrote at NOIP2018.

  • »
    »
    6 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    same, took me a long time to find out that it is not NOIPD2T1 with extra edges :(

    • »
      »
      »
      6 years ago, # ^ |
        Vote: I like it -8 Vote: I do not like it

      真的是两个中国人说话讲缨语

      然后我也是D2T1写挂/px

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Why am I getting runtime error on pretest 2 in Problem D? It is running fine on other online ide like gfg. https://codeforces.net/contest/1106/submission/49308034 Pls help!

  • »
    »
    6 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Do not use set, use priority_queue instead, it will work fine. It's with the erase function of set which is showing undefined behavior. I also faced same issue.

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Is there anyone who can explain the problem E in details? I'm trying to figure it out, but it doesn't work. I hope someone do me a favor please.

I understood what the problem requires, but I can't understand the way 1) calculating max coin 2) what structure you are using in calculating max coin (Why use map(set)?, what's the purpose of it?)

Because of my understandings, I also don't know about DP definition. After understanding the way above, I will try to understand DP approach.

Thanks in advance...

  • »
    »
    6 years ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    Perhaps you could take a look at my comment above.

    Comment Link

  • »
    »
    6 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    For DP part you can look at proofbycontradiction's comment above.

    We use map because we need to get the max event of each 'time' on timeline effectively.

    Map is a balanced binary search tree, which means elements in it is sorted.

    Therefore, we can get the max event(equals to the max coin) among all events in the tree by cur.begin()->first.

    In order to calculate the max coin of every 'time' on the timeline effectively, we introduce two vector array add[N] and remove[N]. At any 'time' i, if there're some events in add[i], just insert them into the tree(map). If there're some events in remove[i], remove every of them from the tree.

    By doing this, it is guaranteed that at all 'time' i, for all events in the tree, the event's range includes i, and there doesn't exist such an event in the tree such that this event's range doesn't include i. Also, all events whose range includes i are in the tree.

    When reading data from input, just add[s].push_back(event), remove[t + 1].push_back(event). We use t + 1 because s and t are inclusive, so you should remove it from tree at t + 1.

    You can calculate the max event of current 'time' during dp, or calculate them before dp. Both works.

    Sorry for bad English, hope this helps! :D

    Please correct me if there's any mistake, thanks!

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

I'd like to ask about C problem.

I came to know that we want to minimize sum bi * ci.

But, I can't know , why we sort ai(bi, ci), when we come to know we want to minimize sum bi * ci. Is there provement?

  • »
    »
    6 years ago, # ^ |
      Vote: I like it +3 Vote: I do not like it

    Here is the proof written by AwakeAnay

    1: Groups must be formed as pairs of 2.

    Proof: As (a+b)^2 > a^2 + b^2 for positive a, b, we want to minimize group size. As size has to be greater than 1, and N is even, all group sizes must be 2.

    Now, we have to pair each number with another such that sum is least. We double this sum. This doubled sum can now be represented as summation (ai + bi)^2 where ai and bi are both permutations of the given numbers.

    2: ai and bi must be oppositely ordered sequences

    Proof: summation (ai + bi)^2 = summation (ai)^2 + summation (bi)^2 + summation (2*ai*bi); We want to minimize this sum. We observe that the first two terms are constants (sum of squares of all given numbers). So, we want to minimize the 3rd term. This is done when ai and bi are oppositely ordered, proof is a direct application of rearrangement inequality.

    Now, observe that this doubled sum corresponds to the pairing where ai is paired with a(N-i+1), with i <= N/2. Thus, we are done

    • »
      »
      »
      6 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Thank you, ChevishaUchiha.

      but, I'd like to know, why when we want to minimize sum a_i * b_i , we sort?

      For example, a=(2, 3, 5, 7) we have to pair(2, 7), (3, 5). But I can't understand why is this correct?

      • »
        »
        »
        »
        6 years ago, # ^ |
        Rev. 4   Vote: I like it 0 Vote: I do not like it

        Because you want to pair the largest one with the smallest one, next largest with next smallest, and so on. Since we are squaring each group having one group even slightly larger than it needs to be costs us much more than if we weren't squaring.

        e.g. (2+7)^2+(3+5)^2 == 9^2+8^2 == 145

        or try (7+3)^2+(5+2)^2 == 10^2+7^2 == 149

        See how we have an average of 8.5 in the first example terms and 8.5 in the second example terms, but because we are squaring its much better to have terms 8 and 9 to square as opposed to 7 and 10 to square.

        The terms have to be paired according to the question, makes no sense to make them groups of three or higher because every time you increase the square geometrically. So we know we are looking for pairs, and its best to keep the pair sums as close as possible.

        Think 10 and 1 vs 5 and 6, both sum to 11, but their sum of squares 5 and 6 is 61 and sum of squares 10 and 1 is 101. Do that for any sum you can think of, you will see the closer they are the better.

        Take 11:

        10^2 + 1^2 == 101

        9^2 + 2^2 == 85

        8^2 + 3^2 == 73

        7^2 + 4^2 == 65

        6^2 + 5^2 == 61

        So how do you take an array of numbers and make their pairs as close as possible? You sort them, two pointers, one in front i and one in back j. Sum and square front plus back, increment front, decrement back, while i<j.

        I guess really it comes down to intuition, I don't have any hard mathematical proof that this gives you the closest pairs, but it feels that way.

      • »
        »
        »
        »
        6 years ago, # ^ |
        Rev. 2   Vote: I like it +3 Vote: I do not like it

        Search for rearrangement inequality. It's simple to prove, intuitive and quite useful.

        Basically, it states that if ai and bi are permutations of 2 sequences of real numbers, then the sum ai*bi will be minimized when ai and bi are oppositely sorted and will be maximized when they are similarly sorted.

        Intuitively, if you want to purchase some items from a store, such that you want ai objects having price pi, then to minimize the total cost, you want to buy the costliest item as less times as possible and so on. So, if prices pi are increasing, then ai must be decreasing.

    • »
      »
      »
      6 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      I understood the second proof by reading about proof og Rearrangement Inequality. But couldn't understand your proof for 1st part — having only groups of size 2. Can you please elaborate why only groups 2 will lead to optimal solution?

»
6 years ago, # |
Rev. 2   Vote: I like it 0 Vote: I do not like it

In the last part of problem F, there is no need to use extended euclidean algorithm to find a solution. Let's assume we want to find X such that

Such a number X exists only if y is divisible by gcd(n,  p  -  1). If yes, then we can reduce the fraction to . Obviously now, is invertible mod(P  -  1) since they are co-prime, and n - 1  =  nphi(p  -  1)  -  1

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

For problem E,which should be the times at which Alice will disturb for test 3? I can't find any combination in which he only gets 11 coins.

  • »
    »
    6 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Bob collects 4 coins at time 1, collects 7 coins at time 6, Alice disturbs at time 11 and 12.

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Can anybody explain how is answer for testcase3 = 11 for Div2E?

  • »
    »
    6 years ago, # ^ |
      Vote: I like it 0 Vote: I do not like it

    Bob collects 4 coins at time 1, collects 7 coins at time 6, Alice disturbs at time 11 and 12.

    • »
      »
      »
      6 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Thanks missed "after that" in statement :
      If Bob chooses to collect the coins in the i-th red envelope, he can do it only in an integer point of time between si and ti, inclusive, and he can't collect any more envelopes until time di (inclusive) after that.

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Can someone please help me fix my solution for problem E 49335045 I get WA on test 10

  • »
    »
    6 years ago, # ^ |
    Rev. 3   Vote: I like it +5 Vote: I do not like it

    You shouldn't use LONG_MAX because LONG_MAX = 2 ^ 31 — 1. Try using 1e18.

    UPD AC with #define LONG_MAX 1e18 — 49360125

    • »
      »
      »
      6 years ago, # ^ |
        Vote: I like it +1 Vote: I do not like it

      Or use LLONG_MAX instead of LONG_MAX.

    • »
      »
      »
      6 years ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Thank you very much! I am new to c++ so this helped me a lot.

»
6 years ago, # |
Rev. 2   Vote: I like it +3 Vote: I do not like it

Hi, everybody! Please allow me to advertise my unofficial Chinese editorial of this round: Codeforces Round #536(Div.2)

For problem C, I think author's proof may be incomplete (or just lack of detailed explanations):

If bi and ci are settled, we can simply use Rearrangement Inequality to pair them.

But why must bi and ci be {a1, a3, a5, ..., an - 1} and {a2, a4, a6, ..., an} ?

My proof to this detail:

Any such pairing ({bi}, {ci}) can be "doubled" and be come a complete pairing between {ai} and {ai} .

For example: for 1 2 3 4 5 6, a pairing (3,1) (2,4) (5,6) can be seen as

1 2 3 4 5 6
3 4 1 2 6 5

so a pairing can be represent as A , a complete pairing can be represent as B = f(A) (note that B = f(A) always exists but not A = f - 1(B)).

Then

Thus

cost(Ai) ≥ cost(A * )

Q.E.D.

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

For 1st part of problem E, how to include the condition for d(till when other envelopes can't be collected)?

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

In Problem F , Can anyone explain more about h_k ? and how can I obtain it from matrix exponentiation using linear recursive equation ?

»
6 years ago, # |
Rev. 6   Vote: I like it 0 Vote: I do not like it

Does problem F have a simpler solution?

Let's first say that f_i is a product of some powers of f1, f2,..., fk. This is true for all i.

We can then consider the recurrence as an additive recurrence over k dimensional vectors f_i = b1*f_i-1 +... +bk*f_i-k. We are basically considering a "logarithmized" version of the recurrence.

Then we can have a transition matrix M of size k x k to get from f_i, f_i+1,...,f_i+k to f_i+k+1,f_i+k+2,...,f_i+2k.

We then use fast exponentiation with matrix multiplication to get from f1...fk to fn (mod p-1 for values). The result will be that we will have fn expressed as a product of powers of f1...fk. We will use again fast exponentiation on numbers now to compute the values for the powers of f1..fk-1. We multiply fn by the inverse of this product and we get fk^b = a (mod p). Finally we are left with just computing the b-th root of a (mod p).

Final complexity should be O(k^3*log n + finding discrete b-th root).

Does this make any sense to you?

LATER EDIT: I just realized that this is more or less what the Tutorial also proposed.

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Hi,can you tell me the test of 7 on Problem B? i wrong answer on it long time

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it
  • Solve this linear equation in or if you apply Fast Fourier Transform.

In this equation, the mod number is 998244352 which doesn't have primitive root. You can't optimize it with NTT. I think the fastest solution should be instead of .

  • »
    »
    6 years ago, # ^ |
    Rev. 7   Vote: I like it 0 Vote: I do not like it

    In fact, for arbitrary modulus (not very large) NTT, there is a trick called three-modulus-NTT (translated from Chinese). Suppose that we want to get . The intuition is that the coefficients for the answer polynomial C(x) = A(x)B(x) won't exceed nP2, where n is . Thus we choose 3 NTT-friendly modulus (whose multiplication is greater than nP2) to get the results and apply Chinese Reminder Theorem to retrieve the original answer back. The details can be found in some Chinese blogs (I'm sorry that I haven't found any in English).

    • »
      »
      »
      4 years ago, # ^ |
      Rev. 2   Vote: I like it 0 Vote: I do not like it

      I got what you mean, but I have a question bro. As well as I know about the trick using NTT that you mention it's alright (you can obtain the convolution for any modulo), but when you use NTT to solve the lineal recurrence, at some point you need the rest of the division of two polinomials. Well if its true, to get the rest you need the division of two polinomials (quotient), and to obtain the quotient its necesary that the first term of one of the reverted polinomials have to have inverse. So, if your modulo its not prime, then you have a problem because there is not necessarily inverse for all numbers. So, I think what MAOoo_Love_Molly said its alright. Am I missing something? correct me if I'm wrong bro, I would really apreciate it.

      • »
        »
        »
        »
        4 years ago, # ^ |
          Vote: I like it +3 Vote: I do not like it

        The idea is that we can get rid of this "disgusting" number $$$P = 998244352$$$ and use 3 NTT friendly numbers (primes) to solve this problem. As is easy to check, if two polynomials of degree no greater than $$$n$$$ have coefficients no greater than $$$P$$$, then the coefficients of the multiplication of these two polynomials will not exceed $$$nP^2$$$. In this way, if we choose 3 NTT friendly primes $$$Q_1, Q_2, Q_3$$$ such that $$$Q_1Q_2Q_3 > nP^2$$$, we can uniquely recover the coefficients with modulus $$$P$$$ by Chinese Reminder Theorem. In this way we apply NTT with NTT friendly primes. Should you have any questions, I would be happy if you could send me an email to discuss it. Hope you find this useful :)

        • »
          »
          »
          »
          »
          4 years ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          first at all, thanks a lot for reply bro!

          Yes, as I said in my last comend, I know how to obtain the convolution for two polinomials for any arbitrary modulus. Well, my question was about how to obtain the lineal recurrence for any modulo (to get the complexity of O(Klog(K)log(n)). I mean, at some point you need the division of two polinomials, well and for this, one of them, have to have inverse, but with any arbitrary modulo, is it possible? I mean, at some point you need the inverse modular of a number, and as far as I know it doesn't necesary exists for any modulo.

          Sorry for my bad english.

          • »
            »
            »
            »
            »
            »
            4 years ago, # ^ |
              Vote: I like it 0 Vote: I do not like it

            Do you know the standard solution with complexity $$$O(k^2 \log n)$$$? This approach is simply by multiplication of polynomials for $$$O(\log n)$$$ times. We aim to optimize the multiplication to $$$O(k \log k)$$$, and this is what I am talking about in the previous comment. :) Feel free to comment if you have further questions.

          • »
            »
            »
            »
            »
            »
            4 years ago, # ^ |
            Rev. 2   Vote: I like it 0 Vote: I do not like it

            It seems that the above approach is wrong.

            Thanks for giving me such a reminder ... I checked the method of solving the linear recurrent equation and found that we do need the inverse of a polynomial. Then I am not sure if this works. Sorry for that inconvenience and you are right :) If you find any approach to this problem I will be looking forward to seeing it. Thanks again!

»
6 years ago, # |
Rev. 2   Vote: I like it +5 Vote: I do not like it

I think maybe the complexity of BSGS could be reduced to .

In number-theory part, if we search for an x satisfying instead of , we have , so we don't need to get inversion modulo p which takes time.

In data-structure part, maybe we can use unordered-map instead of map, then the total complexity is (although I have never implemented it before)

»
6 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Could someone explain why in problem D sorting all the adjacency list and then using Dfs as it will always traverse the lowest nodes first gets a wrong answer? here is my code https://codeforces.net/contest/1106/submission/53541920

  • »
    »
    5 years ago, # ^ |
    Rev. 3   Vote: I like it 0 Vote: I do not like it

    You will understand your mistake if you try this case

    4 3
    1 2
    1 3
    2 4
    

    The mistake is we do not want to specifically use dfs or bfs, just a pq will do.

»
5 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Can anyone please explain how the state has been reduced in problem E.

»
7 months ago, # |
  Vote: I like it 0 Vote: I do not like it

For problem D, why I am getting wrong answer? Can any one please find out the error? thanks in advance._. 257438260