Idea: FelixArg
Tutorial
Tutorial is loading...
Solution (awoo)
#include<bits/stdc++.h>
using namespace std;
int main(){
int tc;
cin >> tc;
while (tc--){
string s;
cin >> s;
sort(s.begin(), s.end());
reverse(s.begin(), s.end());
cout << s << '\n';
}
}
Idea: BledDest
Tutorial
Tutorial is loading...
Solution (ifive)
from math import gcd
for _ in range(int(input())):
a, b, k = map(int, input().split())
g = gcd(a, b)
print(1 if a // g <= k and b // g <= k else 2)
Idea: BledDest
Tutorial
Tutorial is loading...
Solution 1 (BledDest)
def good(x):
return x % 2 > 0 and x % 3 > 0 and x % 5 > 0 and x % 7 > 0
def get_naive(x):
ans = 0
for i in range(x):
if good(i):
ans += 1
return ans
def get(r):
return (r // 210) * get_naive(210) + get_naive(r % 210)
t = int(input())
for i in range(t):
l, r = map(int, input().split())
print(get(r+1) - get(l))
Solution 2 (awoo)
for _ in range(int(input())):
l, r = map(int, input().split())
def f(x):
res = 0
ps = [2, 3, 5, 7]
for mask in range(16):
m = 1
sg = 1
for i in range(4):
if (mask >> i) & 1:
m *= ps[i]
sg = -sg
res += sg * (x // m)
return res
print(f(r) - f(l - 1))
Idea: BledDest
Tutorial
Tutorial is loading...
Solution 1 (Neon)
#include <bits/stdc++.h>
using namespace std;
using pt = pair<int, int>;
const int MOD = 998244353;
int add(int x, int y) {
x += y;
if (x >= MOD) x -= MOD;
if (x < 0) x += MOD;
return x;
}
int mul(int x, int y) {
return x * 1LL * y % MOD;
}
int binpow(int x, int y) {
int z = 1;
while (y) {
if (y & 1) z = mul(z, x);
x = mul(x, x);
y >>= 1;
}
return z;
}
int divide(int x, int y) {
return mul(x, binpow(y, MOD - 2));
}
int main() {
ios::sync_with_stdio(false); cin.tie(0);
int n, m;
cin >> n >> m;
vector<vector<pt>> a(m + 1);
for (int i = 0; i < n; ++i) {
int l, r, p, q;
cin >> l >> r >> p >> q;
a[r].emplace_back(l - 1, divide(p, q));
}
vector<int> pr(m + 1);
pr[0] = 1;
for (int i = 1; i <= m; ++i) {
int cur = 1;
for (auto [_, p] : a[i]) cur = mul(cur, add(1, -p));
pr[i] = mul(pr[i - 1], cur);
}
vector<int> dp(m + 1);
dp[0] = 1;
for (int i = 1; i <= m; ++i) {
for (auto [l, p] : a[i]) {
int cur = divide(pr[i], pr[l]);
cur = divide(cur, add(1, -p));
cur = mul(cur, p);
dp[i] = add(dp[i], mul(dp[l], cur));
}
}
cout << dp[m] << endl;
}
Solution 2 (Neon)
#include <bits/stdc++.h>
using namespace std;
using pt = pair<int, int>;
const int MOD = 998244353;
int add(int x, int y) {
x += y;
if (x >= MOD) x -= MOD;
if (x < 0) x += MOD;
return x;
}
int mul(int x, int y) {
return x * 1LL * y % MOD;
}
int binpow(int x, int y) {
int z = 1;
while (y) {
if (y & 1) z = mul(z, x);
x = mul(x, x);
y >>= 1;
}
return z;
}
int divide(int x, int y) {
return mul(x, binpow(y, MOD - 2));
}
int main() {
ios::sync_with_stdio(false); cin.tie(0);
int n, m;
cin >> n >> m;
vector<vector<pt>> a(m + 1);
vector<int> dp(m + 1);
dp[0] = 1;
for (int i = 0; i < n; ++i) {
int l, r, p, q;
cin >> l >> r >> p >> q;
int val = divide(p, q);
a[r].emplace_back(l - 1, val);
dp[0] = mul(dp[0], add(1, -val));
}
for (int i = 1; i <= m; ++i) {
for (auto [l, p] : a[i]) {
int cur = dp[l];
cur = divide(cur, add(1, -p));
cur = mul(cur, p);
dp[i] = add(dp[i], cur);
}
}
cout << dp[m] << endl;
}
2125E - Sets of Complementary Sums
Idea: FelixArg
Tutorial
Tutorial is loading...
Solution (FelixArg)
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int MOD = 998'244'353;
void solve(){
int n, x;
cin >> n >> x;
// need sum(mx + 1 - ai) <= mx + 1
// start with 1, 2, 3, ... n
// min sum + 1 is n + 1
if (n == 1){
cout << x << '\n';
return;
}
int s_cur = n * (n + 1) / 2;
int need = s_cur - (n + 1);
// leads to n is O(sqrt(s))
if (n + need > x){
cout << 0 << '\n';
return;
}
// so we can do dp[sum] knapsack
vector<int> dp(x + 1);
dp[0] = 1;
// for -1 and 0
for (int j = 0; j < 2; j++){
for (int i = 0; i < x; i++){
dp[i + 1] = (dp[i + 1] + dp[i]) % MOD;
}
}
for (int i = 1; i <= n - 2; i++){
for (int j = 0; j < x; j++){
// increase sum by i -> mx increase by 1 + i
if (j + i + 1 <= x){
dp[j + i + 1] = (dp[j + i + 1] + dp[j]) % MOD;
}
}
}
int ans = 0;
for (int i = 0; i <= x; i++){
int cur = n + need + i;
if (cur > x){
break;
}
ans = (ans + dp[i]) % MOD;
}
cout << ans << '\n';
}
signed main()
{
#ifdef FELIX
auto _clock_start = chrono::high_resolution_clock::now();
#endif
ios_base::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
int t = 1;
cin >> t;
while(t--){
solve();
}
#ifdef FELIX
cerr << "Executed in " << chrono::duration_cast<chrono::milliseconds>(
chrono::high_resolution_clock::now()
- _clock_start).count() << "ms." << endl;
#endif
return 0;
}
Idea: FelixArg
Tutorial
Tutorial is loading...
Solution (awoo)
#include <bits/stdc++.h>
using namespace std;
#define forn(i, n) for(int i = 0; i < int(n); i++)
const int INF = 1e8;
const string al = "docker";
int main(){
cin.tie(0);
ios::sync_with_stdio(false);
int tc;
cin >> tc;
while (tc--){
string s;
cin >> s;
int n = s.size();
int mx = n / 6;
int m;
cin >> m;
vector<int> dx(mx + 2);
forn(i, m){
int l, r;
cin >> l >> r;
r = min(r, mx);
if (l > r) continue;
++dx[l];
--dx[r + 1];
}
forn(i, mx + 1) dx[i + 1] += dx[i];
int cur = 0;
vector<int> dif(max(0, n - 5));
forn(i, n - 5){
forn(j, 6)
dif[i] += s[i + j] != al[j];
cur += dif[i] == 0;
}
int mxcnt = *max_element(dx.begin(), dx.end());
int k = -1;
int ans = INF;
forn(i, mx + 1){
if (dx[i] != mxcnt) continue;
if (i <= cur){
ans = min(ans, cur - i);
continue;
}
k = i;
break;
}
if (k == -1){
cout << ans << '\n';
continue;
}
auto calc = [&](int d){
vector<pair<long long, int>> dp(n + 1, {(long long)1e18, -1});
dp[0] = {0, 0};
forn(i, n){
dp[i + 1] = min(dp[i + 1], dp[i]);
if (i + 6 <= n)
dp[i + 6] = min(dp[i + 6], {dp[i].first + dif[i] + d, dp[i].second - 1});
}
return dp[n];
};
int l = -INF, r = 0, sv = INF;
while (l <= r){
int m = (l + r) / 2;
auto [val, cnt] = calc(m);
if (-cnt >= k){
sv = val - k * 1ll * m;
l = m + 1;
}
else{
r = m - 1;
}
}
ans = min(ans, sv);
cout << ans << '\n';
}
}








Return of the MathForces
It is not just a gap between D and E. It is a GAAAAAAAAAP.
No. E is very easy. All you need to do is to write a brute-force for small n and x, and find some rules on the answers to small n and x (the rule is really easy to find), finally apply the rule to every n and x.
Could you explain the brute-force approach?
I managed to solve it myself.Thanks anyway.
Can anyone help me with why I'm getting it wrong for C?
You forgot +(x/70)
You missed x / (2 * 5 * 7);
You're better off using a
forloop instead of enumerating all the conditions manually, as it makes your code clearer and less likely to miss some elements.thanks Dear, i learn something new approach from your solution
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?
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.
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.
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?
Only error on test 1 (or compile error) doesn't give penalty. Error on test 2 and onwards always gives penalty (assuming you solve), regardless if it is sample or not.
Looks like this has been around for a while.
thank you very much!
i solved E simply by looking for patterns.
outputing the first 5x30 values then removing the zeroes before gives me this dp:
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
Could you tell me what the bruteforce code was?
here it is:
brute force code on ideone
Thanks so much that is a crazyyy solution. Also you have very good function naming practices.
what does sus array calculates?
sus[i]denotes the number of zeros at the beginning ofdp[i], which explains why there is an if statement insolve():then i proceed to subtract
sus[n]fromxin order to get the correct dp explained above.Can anyone explain C? I don't understand.
https://cses.fi/problemset/task/2185 Similar but tight constraints.
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 :/
Why TLE on Systest 31
I think this is efficient enough
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.
I seriously can't thank you enough. Fixing this solved the problem. I couldn't think of this and was stuck.
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.
~~~~~
It is better to introduce the "alien tricks" in the solution of F.
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.
You're right, thanks. Now fixed.
The Problem E is good, but the tutorial is difficult to understand for me.
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.
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).
I got it. I swapped the sign of 1 in the summation from negative to positive, and hence the delta of 2.