Idea: adedalic
Tutorial
Tutorial is loading...
Solution (BledDest)
t = int(input())
for i in range(t):
n = int(input())
if(n % 2 == 1):
print(7, end='')
n -= 3
while(n > 0):
print(1, end='')
n -= 2
print()
Idea: Roms
Tutorial
Tutorial is loading...
Solution (adedalic)
#include<bits/stdc++.h>
using namespace std;
typedef long long li;
int n, x;
string s;
inline bool read() {
if(!(cin >> n >> x >> s))
return false;
return true;
}
inline void solve() {
int ans = 0;
bool infAns = false;
int cntZeros = (int)count(s.begin(), s.end(), '0');
int total = cntZeros - (n - cntZeros);
int bal = 0;
for(int i = 0; i < n; i++) {
if(total == 0) {
if(bal == x)
infAns = true;
}
else if(abs(x - bal) % abs(total) == 0) {
if((x - bal) / total >= 0)
ans++;
}
if(s[i] == '0')
bal++;
else
bal--;
}
if(infAns) ans = -1;
cout << ans << endl;
}
int main() {
#ifdef _DEBUG
freopen("input.txt", "r", stdin);
#endif
ios_base::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cout << fixed << setprecision(15);
int tc; cin >> tc;
while(tc--) {
read();
solve();
}
return 0;
}
Idea: Roms
Tutorial
Tutorial is loading...
Solution (Roms)
#include<bits/stdc++.h>
using namespace std;
const int N = int(2e5) + 99;
const int INF = int(1e9) + 99;
int tc;
string s, t;
int nxt[N][26];
int main() {
cin >> tc;
while(tc--){
cin >> s >> t;
for(int i = 0; i < s.size() + 5; ++i)
for(int j = 0; j < 26; ++j)
nxt[i][j] = INF;
for(int i = int(s.size()) - 1; i >= 0; --i){
for(int j = 0; j < 26; ++j)
nxt[i][j] = nxt[i + 1][j];
nxt[i][s[i] - 'a'] = i;
}
int res = 1, pos = 0;
for(int i = 0; i < t.size(); ++i){
if(pos == s.size()){
pos = 0;
++res;
}
if(nxt[pos][t[i] - 'a'] == INF){
pos = 0;
++res;
}
if(nxt[pos][t[i] - 'a'] == INF && pos == 0){
res = INF;
break;
}
pos = nxt[pos][t[i] - 'a'] + 1;
}
if(res >= INF) cout << -1 << endl;
else cout << res << endl;
}
return 0;
}
Idea: adedalic
Tutorial
Tutorial is loading...
Solution (adedalic)
fun gcd(a : Long, b : Long) : Long {
return if (a == 0L) b else gcd(b % a, a)
}
fun phi(a : Long) : Long {
var (tmp, ans) = listOf(a, a)
var d = 2L
while (d * d <= tmp) {
var cnt = 0
while (tmp % d == 0L) {
tmp /= d
cnt++
}
if (cnt > 0) ans -= ans / d
d++
}
if (tmp > 1L) ans -= ans / tmp
return ans
}
fun main() {
val t = readLine()!!.toInt()
for (tc in 1..t) {
val (a, m) = readLine()!!.split(' ').map { it.toLong() }
println(phi(m / gcd(a, m)))
}
}
1295E - Permutation Separation
Idea: adedalic
Tutorial
Tutorial is loading...
Solution (adedalic)
#include<bits/stdc++.h>
using namespace std;
const int N = int(2e5) + 99;
int n;
int p[N];
int rp[N];
int a[N];
long long b[N];
long long t[4 * N];
long long add[4 * N];
void build(int v, int l, int r){
if(r - l == 1){
t[v] = b[l];
return;
}
int mid = (l + r) / 2;
build(v * 2 + 1, l, mid);
build(v * 2 + 2, mid, r);
t[v] = min(t[v * 2 + 1], t[v * 2 + 2]);
}
void push(int v, int l, int r){
if(add[v] != 0){
if(r - l > 1)
for(int i = v+v+1; i < v+v+3; ++i){
add[i] += add[v];
t[i] += add[v];
}
add[v] = 0;
}
}
void upd(int v, int l, int r, int L, int R, int x){
if(L >= R) return;
if(l == L && r == R){
add[v] += x;
t[v] += x;
push(v, l, r);
return;
}
push(v, l, r);
int mid = (l + r) / 2;
upd(v * 2 + 1, l, mid, L, min(mid, R), x);
upd(v * 2 + 2, mid, r, max(mid, L), R, x);
t[v] = min(t[v * 2 + 1], t[v * 2 + 2]);
}
void upd(int l, int r, int x){
upd(0, 0, n, l, r, x);
}
long long get(int v, int l, int r, int L, int R){
if(L >= R) return 1e18;
push(v, l, r);
if(l == L && r == R)
return t[v];
int mid = (l + r) / 2;
return min(get(v * 2 + 1, l, mid, L, min(R, mid)),
get(v * 2 + 2, mid, r, max(L, mid), R));
}
long long get(int l, int r){
return get(0, 0, n, l, r);
}
int main() {
scanf("%d", &n);
for(int i = 0; i < n; ++i){
scanf("%d", p + i);
--p[i];
rp[p[i]] = i;
}
for(int i = 0; i < n; ++i)
scanf("%d", a + i);
b[0] = a[0];
for(int i = 1; i < n; ++i)
b[i] = a[i] + b[i - 1];
build(0, 0, n);
long long res = get(0, n - 1);
//for(int i = 0; i < n; ++i) cout << get(i, i+1) << ' ';cout << endl;
for(int i = 0; i < n; ++i){
int pos = rp[i];
upd(pos, n, -a[pos]);
upd(0, pos, a[pos]);
res = min(res, get(0, n - 1));
//for(int i = 0; i < n; ++i) cout << get(i, i+1) << ' ';cout << endl;
}
cout << res << endl;
return 0;
}
Idea: Roms
Tutorial
Tutorial is loading...
Solution (BledDest)
#include <bits/stdc++.h>
using namespace std;
const int MOD = 998244353;
int add(int x, int y)
{
x += y;
while(x >= MOD) x -= MOD;
while(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 inv(int x)
{
return binpow(x, MOD - 2);
}
int divide(int x, int y)
{
return mul(x, inv(y));
}
typedef vector<int> poly;
void norm(poly& p)
{
while(p.size() > 0 && p.back() == 0)
p.pop_back();
}
poly operator +(const poly& a, const poly& b)
{
poly c = a;
while(c.size() < b.size()) c.push_back(0);
for(int i = 0; i < b.size(); i++) c[i] = add(c[i], b[i]);
norm(c);
return c;
}
poly operator +(const poly& a, int b)
{
return a + poly(1, b);
}
poly operator +(int a, const poly& b)
{
return b + a;
}
poly operator *(const poly& a, int b)
{
poly c = a;
for(int i = 0; i < c.size(); i++) c[i] = mul(c[i], b);
norm(c);
return c;
}
poly operator /(const poly& a, int b)
{
return a * inv(b);
}
poly operator *(const poly& a, const poly& b)
{
poly c(a.size() + b.size() - 1);
for(int i = 0; i < a.size(); i++)
for(int j = 0; j < b.size(); j++)
c[i + j] = add(c[i + j], mul(a[i], b[j]));
norm(c);
return c;
}
poly interpolate(const vector<int>& x, const vector<int>& y)
{
int n = int(x.size()) - 1;
vector<vector<int> > f(n + 1);
f[0] = y;
for(int i = 1; i <= n; i++)
for(int j = 0; j <= n - i; j++)
f[i].push_back(divide(add(f[i - 1][j + 1], -f[i - 1][j]), add(x[i + j], -x[j])));
poly cur = poly(1, 1);
poly res;
for(int i = 0; i <= n; i++)
{
res = res + cur * f[i][0];
cur = cur * poly({add(0, -x[i]), 1});
}
return res;
}
int eval(const poly& a, int x)
{
int res = 0;
for(int i = int(a.size()) - 1; i >= 0; i--)
res = add(mul(res, x), a[i]);
return res;
}
poly sumFromL(const poly& a, int L, int n)
{
vector<int> x;
for(int i = 0; i <= n; i++)
x.push_back(L + i);
vector<int> y;
int cur = 0;
for(int i = 0; i <= n; i++)
{
cur = add(cur, eval(a, x[i]));
y.push_back(cur);
}
return interpolate(x, y);
}
int sumOverSegment(const poly& a, int L, int R)
{
return eval(sumFromL(a, L, a.size()), R - 1);
}
int main() {
int n;
cin >> n;
vector<int> L(n), R(n);
for(int i = 0; i < n; i++)
{
cin >> L[i] >> R[i];
R[i]++;
}
reverse(L.begin(), L.end());
reverse(R.begin(), R.end());
vector<int> coord = {0, MOD - 2};
for(int i = 0; i < n; i++)
{
coord.push_back(L[i]);
coord.push_back(R[i]);
}
sort(coord.begin(), coord.end());
coord.erase(unique(coord.begin(), coord.end()), coord.end());
vector<int> cL = coord, cR = coord;
cL.pop_back();
cR.erase(cR.begin());
int cnt = coord.size() - 1;
vector<poly> cur(cnt);
for(int i = 0; i < cnt; i++)
if(cL[i] >= L[0] && cR[i] <= R[0])
cur[i] = poly(1, inv(R[0] - L[0]));
for(int i = 1; i < n; i++)
{
vector<poly> nxt(cnt);
int curSum = 0;
for(int j = 0; j < cnt; j++)
{
nxt[j] = sumFromL(cur[j], cL[j], i) + curSum;
curSum = add(curSum, sumOverSegment(cur[j], cL[j], cR[j]));
}
for(int j = 0; j < cnt; j++)
nxt[j] = nxt[j] * (cL[j] >= L[i] && cR[j] <= R[i] ? inv(R[i] - L[i]) : 0);
cur = nxt;
}
int ans = 0;
for(int i = 0; i < cnt; i++)
ans = add(ans, sumOverSegment(cur[i], cL[i], cR[i]));
cout << ans << endl;
}
Finally, the editorial is out :D
O(n^3) solution for F 69782525
69879879 lagrang interpolation can be done in O(n). This is also a O(n^3) solution.
in F how are we dividing into segments, little confused after reading tourist solution, also could someone help me what is this another way(link) of solving increasing sequences question.Thanks in advance.
We use the values of $$$l_i$$$ and $$$r_i + 1$$$ as left borders of the segments: we sort all of them, get rid of the unique values, and each value now defines the beginning of a new segment. So, for each segment we get, all values from it can be used at the same positions of the non-descending sequence we are trying to construct.
In fact,problem F is very similar to another problem in APIO 2016,and in that problem, $$$n\le 500$$$,it means you have to solve it with $$$O(n^3)$$$
And the link of that problem on Luogu is https://www.luogu.com.cn/problem/P3643,I think you are supposed to see it.
Please,can anyone explain problem C,how you are calculating nxt table since I am not able to visualize it. thank you.
If you are at index i in the string and want to calculate the smallest index of jth char between i and |s|, two possibilities occur. 1. s[i] is the jth character 2. s[i] is some other character than the jth character
For the second case, s[i][j]=s[i+1][j] and for the first case s[i][j] is simply i
Think about it this way
For each position we need the next position for all characters from 'a' — 'z' if they exist.
So starting from back to front for the current position we get results from index in front and for the current index we can set next position for the character at index i to be index i, since that's closer.
Hope this helps.
case -1: if any char in t is not present in s. other wise: store the positions of a,b,c...z occurring in S, in a vector of size 26.
now build z from t by taking one char at a time (starting from 0). now suppose you have build some part of z and you had just chosen a character which was at position 'cur' in s.
now we want to add next char of t at the end of z — lets say the next char is x. hence we need the position of 'x' in s. if we think greedily we will chose that position which is just after our cur. if there is no x after cur. we can start from beginning and therefore setting cur=0, and number of steps ++.
https://codeforces.net/contest/1295/submission/69747737
Just maintain an array recording the nearest char C whose index is strictly bigger than the current index enumerated from n to 1
What can be the possible divide and conquer solution for problem E? Thank you
I think my point-update segment tree solution counts as divide-and-conquer, since it solves the problem for sublengths of the permutation and combines them: https://codeforces.net/blog/entry/73413#comment-576470
Small correction: The combinatoric identity in F, the denominator of the last fraction is k and not k+1.
Will be fixed in a few minutes. Thank you!
Is there any inclusion exclusion based solution to D?
Maybe this could help
Of course there is :) https://codeforces.net/contest/1295/submission/69760075
Sorry, can you please explain your idea? I understand the general idea but not the specifics. Thanks
can you please explain
yes.. https://codeforces.net/contest/1295/submission/69784096
can you plz plz explain atleast explain
}` this
There exists a combinatorial proof of Euler Phi Function formula that uses the inclusion-exclusion principle.
my stupid ass was not able to understand even the tutorial..... for all the stupid ones out there my friend Thallium54 have a blog https://thallium.space/题解/tutorial/2020/01/29/CF1295D
I also did it with Inclusion-Exclusion https://codeforces.net/contest/1295/submission/84912597
IN PROBLEM D HOW IS 0 GETTING MANAGED BY USING THE EULER TOILENT FUNCTION.
EDITORIAL SAYS THAT GCD(0,m')=m'
but 0 can be added to a and gcd(a,m) will be same as gcd(a+0,m);
please help..
Thanks in advance ..
$$$x' = x' ' g$$$ and author shows why 0 is not valid for $$$x' '$$$. The case $$$gcd(a, m) = gcd(a + 0, m)$$$ is covered when $$$x' ' = a'$$$
i am not able to understand the tutorial for 1295D can you explain
Let $$$gcd(a,m) = g$$$. Also Let $$$x' = x+a$$$.
We want $$$gcd(x',m) = g$$$ which is equivalent to $$$gcd(\frac{x'}{g},\frac{m}{g}) = 1$$$
So we need find number of $$$x'' = x'/g$$$ co-prime with $$$m' = m/g \in [1,m'-1]$$$ Since $$$x+a > 0$$$
This is the Euler Phi Function.
thanks finally got it
Please explain problem E in simple words..
Which words do you not understand?
We are iteration on val or on POS and what we store on segment tree. And Finally this thing -- So what will happen if we increase val by 1? Let's define the position of pk=val as k. For each pos≥k we don't need to move pk to the second set anymore, so we should make t[pos]−=ak. On the other hand, for each pos<k we need to move pk from the second set to the first one now, so we should make t[pos]+=ak. Thanks
Every node of segment tree stores the minimum cost if we select a segment [0,r] and we want all the elements between [0,r] lie between [0,val]. Initially assume val is -1. Therefore for every segment [0,r] we will have to shift its elements to the other segment (since no value lies between [0,val], i.e., [0,-1]). Now we will iterate over val. Now when value is 0, let the position of 0 in the original array be 'ind'. How to update the answer now??
For every segment [0,r] which contain the index 'ind', we do not need to shift val to the other segment (we shifted it when val was -1,). Thus for every segment [0,r] which contain 'ind' we will add (-a[ind]). And for every segment [0,r] which does not contain 'ind' we need to get it, therefor we will add (a[ind]).
Hope you got it. Feel free to ask if you didn't understand anything.
We build segment tree on the given array and then check ind for every node Right? And why we iterate on Val not on pos.
As we are sweeping according to val, which means we already know the answer for val, and we want to know the answer for val+1.
Why are we sweeping from 1 to n+1 ? shouldn't it be from 1 to n ? n+1 doesn't even exist in the permutation.
"All elements in the left set smaller than all elements in the right set" means that there is such value val that all elements from the first set less than val and all elements from the second set are more or equal to val. "
Since all elements on the left set are less than (not less than equal to )val therefore sweeping till n+1, means left set will contain numbers from [1,n].
what is n in the editorial of Problem-B ? i mean what does n mean?
$$$n$$$ is the length of the string.
C can also be solved using binary search, pretty easy to understand.69761627
Hey, I didn't get your code.How you are applying a binary search. Can you explain in detail please.
Firstly we store the positions of all the appearance of each characters in string s. Then for each character in string t, we want the first appearance of that character after the position of the last character, which could be done with binary search.
last character of what? string s or t ?
I mean the last character that has been obtained in string t. Sorry for my bad English.
F says: we will count all the non-descending sequences
but must be non-increasing, since pair (1, 2) is inversion according to task
UPD: sorry, must be descending with possible equal, since descending with possible equal != non-increasing (1, 3, 2) is not-increasing, but not a descending with possible equal
descending means a[i+1]<a[i] and non-descending means a[i+1]>=a[i]
yes, but we need the array to look something like this: 3 3 2 1 a[i] >= a[i+1] it's not a strict decrease, but not a non-decrease or non-increase
a[i+1] is less than a[i]----------a[i+1]<a[i]. -----------descending
a[i+1] is not less than a[i]------a[i+1]>=a[i]. -------non-descending
In this problem, we need to count the sequence a[] which satisfied:
a[1]<=a[2]<=...<=a[n].
And it means the sequcence a[] is a non-descending sequence.
Yeah I got confused by that; once I realized the issue, I just reversed the array after reading the input to get the right answer
What programming language is the D solution written is? Is that just pseudocode?
Looks like Kotlin.
Sure, it is Kotlin!
Any other solution for problem D? or more detailed explanation for the one mentioned in the tutorial?
1) gcd(a, m) is fixed -> let it be G
2) Want to find gcd(a + x, m) = G, 0 <= x < m
3) Same as gcd(X, m) = G, a <= X < a + m
3.5) gcd(X, m) = gcd(X % m, m) (Think about euclidean algorithm)
3.75) When you loop X from a to a + m — 1, X % m includes all possible outcome from 0 to m — 1.
4) Same as gcd(Y, m) = G, 0 <= Y < m
5) Same as gcd(Y / G, m / G) = 1, 0 <= Y / G < m / G (Floor division)
6) So count number of Z such that gcd(Z, m / G) = 1 under m / G. That's the definition of euler's totient function.
How could you be sure that
(in point 5)
after divide all numbers(0 to m-1)
by G there can be foundφ(n)
numbers of value those are coprime to m/G ??For gcd(a, b) = G, a = k1*G and b = k2*G where gcd(k1, k2) = 1
So gcd(a / G, b / G) = 1 is a necessary and sufficient condition
Does that help?
What do you mean by $$$\phi(n)$$$?
If you want number of numbers coprime to $$$m/G$$$, then you use $$$\phi(m/G)$$$.
$$$\phi(n)$$$ gives number of numbers coprime to $$$n$$$, and smaller than $$$n$$$.
THANKS
I hope my blog would help a bit.
can you please re-upload the blog that explains problem D?
It says the blog is no more existing . Thallium54
Fixed
thanks
Can someone explain editorial for B?
To me it was easier to see it like this.
Suppose the input was
011101000100
, then you build the "balance" array of it, which is what the editorial mentioned:The last number in the balance array, $$$bal(n)$$$, is going to be a "delta", and you can see that in the next iteration of the input the balance array will be the same, only delta units higher, like this:
From here you can see that from each position $$$i$$$ the numbers you can reach are $$$bal(i) + \delta \times k$$$, where $$$\delta = bal(n)$$$, for some $$$k \geq 0$$$.
You also need to be careful of two things. What if the delta is 0? then the balance array is going to repeat itself to infinity, so if the $$$x$$$ is in the initial balance, then the answer is
-1
, and if it's not it's0
. Lastly, how many times will $$$x$$$ appear? well for each position $$$i$$$, $$$x$$$ can be reached 0 or 1 times. If it can be reached more than once, that immediately means delta is 0, so the answer is-1
. You can check if $$$x$$$ can be reached from an initial balance $$$bal(i)$$$ easily using modulo or the formula in the editorial.Thanks for helping!
"So let's make a sweep line on val from 1 to n+1 while trying to maintain all answers for each prefix pos". What does making a sweep line mean?
It means we reconceptualize the $$$val$$$ dimension as physical time, and find a way to transition our program's state from $$$val = x$$$ to $$$val = x + 1$$$ (so like a line "sweeping" over that dimension over time). Working it out as in the editorial reveals that we want an array that we can do range additions and range minimums quickly, which can be implemented by a lazy-propagating segment tree.
Thanks guys for the editorial. By the way.
F says: ** Calculating the number of ways to take K elements from an interval [L,R) in sorted order can be reduced to calculating the number of ways to compose K as the sum of R-L non-negative summands (order matters). **, but must be the number of ways to compose R-L as the sum of k non-negative summands (order matters)
can anyone please optimize this code ? thanks in advance. this is my submission
REGARDING QUESTION D can this question be solved by taking prime factors of m and then by inclusion and exclusion of factors of these prime numbers?
Can anyone help me to find out the bug of my solution?
Problem: 1295E - Permutation Separation
For every prefix of from 1 to i, I calculate the dollar needed to make this segment fully covered with only elements less or equal to i (stored this result to L[i]).
The same process for every suffix of the array and stored the result to R[i]. Then for all possible partitions from 1 to n, I calculate the minimum one.
Can anybody help me with the following?
"Otherwise for each such j there is at most one k satisfying bal(j) + k.bal(n) = x ?
We look at the string in iterations. One iteration is one time copy the string s to the end of the whole infinite string.
If the balance for the string s bal(n)!=0, then it is so that the balance at a position j is at most once equal to x in all iterations.
This is so because the balance at a position j changes from iteration to iteration by bal(n), and because it changes, it can be equal to x only once.
That one time is the k-th iteration. To find the answer, we count the positions j where such a k exists.
Can anybody help me figure out why my DP solution for E is wrong ? dp[i] means the minimum cost if I choose there is i elements in the first set. Considering array a is a permutation, so there is exactly [1, i] in the first set if it is "good", am I right? I wrote my solution based on that. Here is my submission. 70131818 Thanks!
Sorry, never mind this, I figured it out already.
Can you explain where was the problem? My approach is similar with yours and I have the same wrong answer on 10th test case (69928822).
We miss many situations, if we split the array into [1, k] and [k+1, n] at first, the "good" array after we move the elements may not as the same length as the original(k in the first and n-k in the second, this is nay not the minimum). That is the point. So naive but right solution must be two loops, one is the pos to split, another is the length(or val).
Problem A is almost same with 774C - Maximum Number.
Can anybody help me please: I cannot understand why in problem E we cannot compute L and R arrays as well as in prodipdatta7' solution in such way: Let's consider given permutation P and sequence 1,..., n — denote as N; current sum for L — denote as sum; set S. For i from 1 to n — 1:
Such approach fails on 10th test.
I think that the following example can help:
Input:
4
2 4 1 3
8 6 9 7
Correct output: 6
Your output: 7
The correct solution (total cost = 6):
Step 1: Split the initial permutation into [2, 4, 1] and [3];
Step 2: Move 4 from the first set to the second set (total cost = 6). The final sets are [2, 1] and [4, 3].
Your solution (total cost = 7):
Step 1: Split the initial permutation into [2, 4, 1] and [3];
Step 2: Move 3 from the second set to the first set (total cost = 7). The final sets are [2, 4, 1, 3] and [].
Thank you very much !
You're making the same mistake as https://codeforces.net/blog/entry/73467?#comment-578439
And you too !
Yeah :')
in Problem D why m`>1 ? I need explanation :( it's too hard
E can also be solved using Ternary search.
Solution link — here
C using Binary Search https://codeforces.net/contest/1295/submission/87246971
Problem D is just Insane.
Since I found the editorial and all other comments quite tough to understand, here's my Detailed Beginner friendly approach. As Someone above said, this is what I tend to explain, a bit more clearly Perhaps.
Step By Step Approach For D:
Let gcd(a,m) = g. So, we can say a=g*k and m=g*l. Now Putting back a and m , gcd(g*k,g*l) = g. Obviously , we can say gcd(k,l)=1.
Now looking closely, we need to find all x such that gcd(a+x,m)=g. means that in our answers, x should also be a multiple of g. So let x=y*g. Now, gcd(g*k + g*y , g*l) = g, or gcd(k+y , l) = 1.
Now What could be the range of y ? See, we have x=y*g and x<m and m=g*l, So we can say that 0<=y<l. So now just need to find for how many y in range [0,l), gcd(k+y,l) = g ?
Now our Problem has been reduced to find how many numbers ranging from k to k+l are coprime with l.
Now there's a Euler Totient Function i.e. Phi(n) which tells how many numbers between [1,n) are coprime with n, in O(sqrt(n)).
We tend to use this, But how? Our current problem is to find how many numbers between [k,k+l) are coprime with l.
Observe, that if 2 numbers are coprime, i.e. gcd(temp,l)=1 and temp>l, we can say temp = l*some_number + tmp%l . See, we can also say that gcd(temp%l,l)=1 . (See this step again).
Now, we have established that we have a Range (of length l) [k....l.....k+l] to look for which of these have gcd()=1 with l, and for all those numbers z in this range greater l, we can simply replace them by z%l.
So in the end, we have our Range as [1...l] only. Now to find all the numbers in [1,l] which are coprime with l, just use the Totient Function in O(sqrt(n)
Code.
Thank You for your explanation
I really liked Problem E
Explanation for D : https://blatherstrike.blogspot.com/2024/01/1295d-same-gcds.html