awoo's blog

By awoo, history, 15 months ago, translation, In English

2125A - Difficult Contest

Idea: FelixArg

Tutorial
Solution (awoo)

2125B - Left and Down

Idea: BledDest

Tutorial
Solution (ifive)

2125C - Count Good Numbers

Idea: BledDest

Tutorial
Solution 1 (BledDest)
Solution 2 (awoo)

2125D - Segments Covering

Idea: BledDest

Tutorial
Solution 1 (Neon)
Solution 2 (Neon)

2125E - Sets of Complementary Sums

Idea: FelixArg

Tutorial
Solution (FelixArg)

2125F - Timofey and Docker

Idea: FelixArg

Tutorial
Solution (awoo)
  • Vote: I like it
  • +91
  • Vote: I do not like it

| Write comment?
»
15 months ago, hide # |
 
Vote: I like it +24 Vote: I do not like it

Return of the MathForces

»
15 months ago, hide # |
 
Vote: I like it +100 Vote: I do not like it

It is not just a gap between D and E. It is a GAAAAAAAAAP.

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

Can anyone help me with why I'm getting it wrong for C?

long long solve2(long long x) {

  return (x/2) + (x/3) + (x/5) + (x/7) - (x/6) - (x/10) - (x/14) - (x/15) - (x/21) - (x/35) + (x/30) + (x/42) + (x/105) - (x/210);
}

void solve() {
  long long l, r;
  cin >> l >> r;
  l--;

  long long a = solve2(r);
  long long b = solve2(l);

  long long ans = (r - a) - (l - b);

  cout << ans << endl;

}

int main() {

  int t;
  cin >> t;
  while(t--) {
    solve();
  }
}
»
15 months ago, hide # |
 
Vote: I like it 0 Vote: I do not like it

I had a question. How many question should I try to upsolve. As I failed in C. I went through the theory and still it is little tough for me. Will it be any good if I upsolved problems > D?

  • »
    »
    15 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    Personally, I think D is a good problem for practicing modular arithmetic & DP. At your rating, I wouldn't worry too much about upsolving EFs since those are at a CM/M level.

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

Slightly different derivation of the sufficient and necessary condition for question E. Consider any (sorted) output set x = {x(1),x(2),...,x(n)} that we want to make a input set for for. Consider the difference array {d(2),d(3),..d(n)} where d(i) = x(i)-x(i-1) we know there must be some term z after which the following terms come, z+d(n), z+d(n)+d(n-1), z+d(n)+d(n-1)+d(n-2).., in the input set. Thus let us only have those terms for now. Note that it is optimal to make z=1, since any higher z would only make the input sum higher raising all our output terms by a constant, which we can just do instead by adding more 1 terms should we need to. Now we have the input array {1,1+d(n),1+d(n)+d(n-1),...,1+(sum of all d(i)}. Lets find the sum of this array, note that 1 adds to n terms, (x(n)-x(n-1)) adds to n-1 terms, (x(n-1)-x(n-2)) adds to n-3 terms, etc. It is not hard to see that each x term will be subtracted once except for x(n), thus the final sum is just n+x(n)*(n-1)-(sum of all x(i) s.t i<n). Now, from the construction, we know that this corresponds to an output array that is off by the original set x by some constant, i.e it has the same difference array when sorted. The only question now is, is the highest term, and thus all the terms, small enough? Remember that too small is ok since we can raise the sum, but too big is not ok. We know that the largest term will be equal to the sum-1, since the lowest term is 1, so we know it is just n+x(n)*(n-1)-(sum of all x(i) s.t i<n)-1 and this must be less than or equal to x(n). Thus we have n+x(n)*(n-1)-(sum of all x(i) s.t i<n) — 1 <= x(n). Rearranging this we get x(n)*(n-1)+(n-1) — (sum of all x(i) s.t i<n) <= x(n) and then we get (x(n)+1)*(n-1) — (sum of all x(i)) <= 0, exactly the condition found in the editorial.

»
15 months ago, hide # |
 
Vote: I like it +14 Vote: I do not like it

This contest felt like there was a huge gap between ABCD and EF. I solved ABCD in the first 20 minutes, spent 1 hour on F, and ultimately couldn't pass E.

However, I think E is still a good problem. I really didn't notice that with $$$s=\max(Q)+1$$$, you can construct a solution (if one exists).

But does anyone know why I got penalty time for WA on test 2 in problem F? Isn't the second test case the sample?

»
15 months ago, hide # |
← Rev. 2  
Vote: I like it +13 Vote: I do not like it

i solved E simply by looking for patterns.

outputing the first 5x30 values then removing the zeroes before gives me this dp:

dp[i][j] = dp[i - 1][j] + dp[i][j - i + 1]

which can be precomputed in o(nsqrt) time and instant o(1) per testcase, making the sum of n and x over all testcases irrelevant.

heres the code: link

edit: this is actually just an offline version of the official edit im sorry guys

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

Can anyone explain C? I don't understand.

»
15 months ago, hide # |
← Rev. 3  
Vote: I like it 0 Vote: I do not like it

Why the $$$1 \leq l_i, r_i \leq 10^9$$$ constraints on F? It follows directly from the statement that values that are bigger than $$$n$$$ don't matter. This just gaslights you into thinking you read the problem wrong :/

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

Why TLE on Systest 31

I think this is efficient enough

Code
  • »
    »
    15 months ago, hide # ^ |
     
    Vote: I like it +1 Vote: I do not like it

    You’re using dp[v] == 0 to mean “not computed yet”. But 0 is also a valid value for dp[v]. So every time you touch a vertex whose true answer is 0, you call dfs(v) again, recomputing it over and over. I guess it's why got TLE.

    • »
      »
      »
      15 months ago, hide # ^ |
       
      Vote: I like it 0 Vote: I do not like it

      I seriously can't thank you enough. Fixing this solved the problem. I couldn't think of this and was stuck.

»
15 months ago, hide # |
← Rev. 3  
Vote: I like it 0 Vote: I do not like it

Can someone please help me with this, I have been trying to solve this question for hours. I don't understand why is it failing testcase 8 always. I don't know what i missed. My solution seems similar to the idea discussed in the edutorial. I have added comments to make it more clear. Any discussion would be helpful, Thanks.


~~~~~ #include<bits/stdc++.h> #define lli long long int #define MOD 998244353 using namespace std; lli gcd(lli a, lli b); lli binary_exp(lli a, lli b, lli mod); priority_queue<lli, vector<lli>> MAX_HEAP; priority_queue<lli, vector<lli>, greater<lli>> MIN_HEAP; void solve(){ lli n, m; cin >> n >> m; vector<tuple<lli, lli, lli, lli>> segment; for(int i=0;i<n;i++){ lli l, r, p, q; cin >> l >> r >> p >> q; segment.push_back({r, l, p, q}); // note i put {r, l} rather than {l, r} } sort(segment.begin(), segment.end()); // sorting the segments based on r vector<lli> dp(m+1, 0); // dp[i] = probablity of filling 1 -> i exactly once with currently seen segments vector<lli> not_take(n, 0); // not_take[i] = prod of probablity of not taking segments 0 -> i dp[0] = 1; for(int i=0;i<n;i++){ auto seg = segment[i]; lli l = get<1>(seg), r = get<0>(seg), p = get<2>(seg), q = get<3>(seg); lli not_p = ((q-p) * binary_exp(q, MOD-2, MOD))%MOD; // just getting the probablity of the segment not existing lli take_p = (p * binary_exp(q, MOD-2, MOD))%MOD;// probablity of segment existing if(i == 0){ // special case, first segment not_take[i] = not_p; dp[r] = take_p; continue; } // probablity of filling 1-> r if current segment doens't exist dp[r] = (dp[r]*not_p)%MOD; not_take[i] = (not_p * not_take[i-1])%MOD; // updating not_take // exlude represents the probablity of not taking segments which overlap with the current segment. lli exclude = not_take[i-1]; auto it = lower_bound(segment.begin(), segment.begin() + i, make_tuple(l, 0, 0, 0)); if(it != segment.begin()){ // 0 -> idx should index all the segment which do not overlap with the current segment int idx = distance(segment.begin(), it);idx--; // removing the probablity of non overlapping segment not existing exclude = (exclude * binary_exp(not_take[idx], MOD-2, MOD))%MOD; } // updating dp[r] dp[r] = (dp[r] + (((take_p * exclude)%MOD)*dp[l-1])%MOD)%MOD; } cout << (dp[m]%MOD) << endl; } bool multitestcase = false; int main(){ ios_base::sync_with_stdio(false); cin.tie(NULL); int testCase = 1; if(multitestcase) cin >> testCase; while(testCase--){ solve(); } return 0; } lli gcd(lli a,lli b) { if(b == 0) return a; return gcd(b, a%b); } lli binary_exp(lli a, lli b, lli mod){ lli res = 1; while(b > 0){ if(b&1){ res = (res*a)%mod; } a = (a*a)%mod; b = b/2; } return res; }

~~~~~

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

It is better to introduce the "alien tricks" in the solution of F.

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

In the editorial for E, is $$$\mathrm{dp}[st]$$$ really equal to 1? From what I understand we should have $$$\mathrm{dp}[st+n] = 1$$$ instead (with $$$\mathrm{dp}[st]=0$$$).

$$$\mathrm{dp}[i]$$$ is defined as "the number of different sets of complementary sums such that the maximum in these sets equals $$$i$$$ and equality in condition (1) holds". As $$$st$$$ is defined as the number of additions of ones to all of the elements of the set {$$$1, 2, \dots, n$$$} needed to make it satisfy the condition (1), it can be zero (with $$$n=2$$$). With $$$n=2$$$, $$$\mathrm{dp}[0] \neq 1$$$, because every set of complementary sums with more than one element* has only positive values inside, so the maximum of those values can't be zero. On the other hand, $$$\mathrm{dp}[st+n] = 1$$$ makes sense -- the set {$$$1, 2, \dots, n$$$} after the additions will have its maximum equal to $$$n+st$$$, thus $$$\mathrm{dp}[n+st] = 1$$$.

*in the editorial it is not specified that in the $$$\mathrm{dp}$$$ we only care about sets of size $$$n$$$, but I don't see how that wouldn't be true.

»
14 months ago, hide # |
← Rev. 2  
Vote: I like it 0 Vote: I do not like it

The Problem E is good, but the tutorial is difficult to understand for me.

We can represent the operations of adding to the suffix of length j as an item

Since j is smaller than n, why enumerate from 0 to x?

What is the connection between prefix sum and +1 operation?

We start from {1, ..., n}, why dp[0] = 1;

It's too difficult.

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

Thanks for the editorial. Can someone explain in the editorial for E, how is f(1) = n-2. As per my calculation I am getting f(1) as n. I am referring to:

we define f(i) as the change in the sum on the left side of condition (1) if we added 1 to the suffix of length i. We can easily calculate all f(i).

  • »
    »
    13 months ago, hide # ^ |
     
    Vote: I like it 0 Vote: I do not like it

    I got it. I swapped the sign of 1 in the summation from negative to positive, and hence the delta of 2.