You can use several words in query to find by all of them at the same time. In addition, if you are lucky search understands word forms and some synonyms. It supports search by title and author. Examples:

  • 305 — search for 305, most probably it will find blogs about the Round 305
  • andrew stankevich contests — search for words "andrew", "stankevich" and "contests" at the same time
  • user:mikemirzayanov title:testlib — search containing "testlib" in title by MikeMirzayanov
  • "vk cup" — use quotes to find phrase as is
  • title:educational — search in title

Results

1.

Euclid

Last visit:  8 years ago
Registered:  13 years ago
2.
By TLE, history, 8 years ago, In English
Avito Cool Challenge 2018 Editorial [problem:1081A] Idea: [user:yjq_naiive,2018-12-17] Developer: [user:yjq_naiive,2018-12-17] <spoiler summary="Hint"> 1 sounds like the minimum value we can get. Why? Is it always true? </spoiler> <spoiler summary="Solution"> When $n \leq 2$, we can do nothing. When $n \geq 3$, since $1<\frac{n}{n-1}<2$, $n-1$ isn't a divisor of $n$, so we can choose it and get $1$ as the result. </spoiler> <spoiler summary="Code (yjq_naiive)"> ~~~~~ #include <bits/stdc++.h> using namespace std; int main() { int n; scanf("%d", &n); if (n == 2) { puts("2"); } else { puts("1"); } return 0; } ~~~~~ </spoiler> [problem:1081B] Idea: [user:yanQval,2018-12-17] Developer: [user:yanQval,2018-12-17] <spoiler summary="Hint"> Consider the number of people wearing hats of the same color. </spoiler> <spoiler summary="Solution"> let $b_i=n-a_i$ represent the number of people wearing the same type of hat of $i$-th person. Notice that the person wear...
$, $s_i=s_{i+q}=s_{i+q-p}=s_{i+d}$. So $q-p$ is also a period. Using Euclid algorithm we can get, So $q-p$ is also a period. Using Euclid algorithm we can get $gcd(p,q)$ is a period.

Full text and comments »

  • Vote: I like it
  • +154
  • Vote: I do not like it

3.
By RP-1, 20 months ago, In English
Sqrt Heuristic for Floor Sums _Thanks to [user:Kapt,2025-04-10] for proofreading this blog._ Recently, I came across a couple of AtCoder problems on modular arithmetic. In short, they both involved sequences $\\{ A, A+C, A+2C, \dots \\}$ modulo $M$. The usual way to deal with these sequences is floor sums: **Problem (Floor Sum).** Calculate $\displaystyle \sum_{k=0}^n \left\lfloor \frac{Ck+A}{M} \right\rfloor$. This is solvable in $O(\log M)$ with a recursive Euclid-like algorithm. If you want to learn more about floor sums, I recommend [this blog](https://asfjwd.github.io/2020-04-24-floor-sum-ap/) by [user:_timelord,2025-04-10]. However, what I want to show you is how you can solve problems like that with a completely different idea. The basis of our reasoning is the following statement: (For the rest of this blog, let $0 \le C, A < M$, $\gcd(C, M) = 1$, $n < M$, just to avoid hassle.) **Statement.** The sequence $\\{A, C + A, 2C + A, \dots, Cn + A\\} \bmod M$ can be split into $O(\sqrt{M})$ d...
is solvable in $O(\log M)$ with a recursive Euclid-like algorithm. If you want to learn more, This is solvable in $O(\log M)$ with a recursive Euclid-like algorithm.

Full text and comments »

  • Vote: I like it
  • +161
  • Vote: I do not like it

4.
By balbit, history, 5 years ago, In English
Let's Solve Some More Hard Problems! Inspired by ivan100sic's [problem list blog](https://codeforces.me/blog/entry/98630), I'll pick 10 nontrivial-seeming recent POI (Polish Olympiad in Informatics) problems that I haven't solved and track my progress here. It seems to me that POI is better suited for this format as their editorials are quite difficult to find (if possible [at all](https://szkopul.edu.pl/problemset/problem/v2Y2_UW56ENMcbwP22tkTb7a/site/?key=statement)). While Ivan's original challenge was supposed to last an entire year, I'm not nearly as patient, so I won't look for editorials only until February 1st (in just over 2 weeks). Hopefully I'll have completed at least 80% by that time. Until then, GLHF!!! The List: 1. [Multidrink 2013](https://szkopul.edu.pl/problemset/problem/GyDCbdIgFY5FsMa9iIsBl3hf/site/?key=statement) [Solved Jan 15] <spoiler summary="Fun-ness:"> 5/10 <spoiler summary="Comments"> My thought process on this problem went like "ew this is disgusting implementation" => ...
point{ }; vector > from[maxn]; // x=1 or 3, a-value ll euclid(ll a, ll b, ll euclid(ll a, ll b, ll &x, ll &y) { if (b) { ll d = euclid(b, a % b, y, x); return y -= a/b, pii k; euclid(-m,n,k.f,k.s); if (k.f * -m + k.s * n < 0) { k.f *= -1; k.s *= -1; }

Full text and comments »

Tags pow, odz, eni, abh, ai
  • Vote: I like it
  • +149
  • Vote: I do not like it

5.
By Zlobober, 12 years ago, translation, In English
Codeforces Round #296 Editorial [problem:527A] ============== It’s easy to see that described process is equivalent to the following loop: ~~~~~ while a > 0 and b > 0: if a ⩾ b: a = a - b else: b = b - a ans = ans + 1 ~~~~~ But such naive approach will obviously lead to verdict TLE, since it makes ~$10,2015-03-19^{12}$ operations even on the third sample test. The key idea is to replace repeating subtraction operations with integer division operations. This leads to the logarithmic-time solution that looks similar to the Euclid algorithm: ~~~~~ while a > 0 and b > 0: if a ⩾ b: ans = ans + a div b a = a mod b else: ans = ans + b div a b = b mod a ~~~~~ [problem:527B] ============== The first observation is that the new Hamming distance may not be less than the old one minus two, since we change only two characters. So the task is to actually determine, if we can attain decrease by two, one or can’t attain decrease ...
the Euclid algorithm:

Full text and comments »

  • Vote: I like it
  • +55
  • Vote: I do not like it

6.
By applepi216, 2 years ago, In English
Codeforces Round 986 (Div. 2) Editorial Thank you everyone for participating in the round! We hope you enjoyed the problems :) ### [problem:2028A] <spoiler summary="Hint"> How many times do you have to repeat the string until you know for certain whether Alice will ever reach the Red Queen? </spoiler> <spoiler summary="Solution"> [tutorial:2028A] </spoiler> <spoiler summary="Code"> ~~~~~ def solve(): [n, a, b] = list(map(int, input().split())) s = str(input()) x, y = 0, 0 for __ in range(100): for c in s: if c == 'N': y += 1 elif c == 'E': x += 1 elif c == 'S': y -= 1 else: x -= 1 if x == a and y == b: print("YES") return print("NO") t = int(input()) for _ in range(t): solve() ~~~~~ </spoiler> ### [problem:2028B] <spoiler summary="Hint"> Do casework on whether $b = 0$ or not. </spoil...
* invert(b); } mint invert(mint a) { ll x, y, g = euclid(a.x, mod, x, y); assert(g == 1, euclid(ll a, ll b, ll &x, ll &y) { if (!b) return x = 1, y = 0, a; ll d = euclid (b, a % b, y, x, ll euclid(ll a, ll b, ll &x, ll &y) { if (!b) return x = 1, y = 0, a; ll d = euclid(b, a % b, y

Full text and comments »

  • Vote: I like it
  • +4
  • Vote: I do not like it

7.
By PokemonMaster, history, 14 months ago, translation, In English
2147G Number Theory Approach # G. Modular Tetration — a Number Theory Solution ## Preliminary Comments I deleted the first post and am publishing a new one with preliminary comments. This problem turned out to be problematic and spoiled the contest for strong participants, since some cheaters copied it, but it didn’t seem difficult to me, except for deriving the final formula and a bit of implementation. During the contest, I managed to work out the implementation on paper, but I couldn’t handle the coding. I am a junior in mathematics, focusing on number theory and algebra (at the last JBMO I managed to solve both of these topics), but I still have many gaps in CP and implementation. In number theory I solve or make substantial progress on many problems from IMO shortlists. If you assign a CF-like rating to those problems, I solve problems steadily up to 2000, sometimes up to 2500, and there have been harder ones. Now I want to use number theory and mathematical implementation of programming problems ...
### 2) $\gcd$ trick (Euclid), $ trick (Euclid) Classical fact: if $a^r\equiv1\pmod m$ and $a^s\equiv1\pmod m$, then $$ a

Full text and comments »

  • Vote: I like it
  • +74
  • Vote: I do not like it

8.
By wuhudsm, history, 2 years ago, In English
TheForces Round #31 (Div2.9-Forces) Editorial [A](https://mirror.codeforces.com/gym/105245/problem/A) Idea:[user:yugandhar_master,2024-03-14] <spoiler summary="solution"> Let's handle each two lines separately. We greedily place kings in all the white squares in the first row. Then, we cannot place kings in the second row. Overall, the answer is $\lfloor \frac{n+1}{2} \rfloor \cdot \lfloor \frac{m+1}{2} \rfloor$. </spoiler> <spoiler summary="code(C++)"> ~~~~~ #include <bits/stdc++.h> using namespace std; #define ll long long #define fast \ ios_base::sync_with_stdio(0); \ cin.tie(0); \ cout.tie(0); int main(){ fast; ll t; cin>>t; while(t--){ int n,m; cin>>n>>m; cout<<((n+1)/2)*((m+1)/2)<<"\n"; } } ~~~~~ </spoiler> [B](https://mirror.codeforces.com/gym/105245/problem/B) Idea:[user:beyondpluto,2024-06-14] <spoiler summary="solution"> First let's assign a number...
ans; } Node euclid(ll p, ll q, ll r, ll n, Node U, Node R) { if (!n) return Node(); if (r, ans_r = euclid(a, c, b, r, U, R); ans_r.sumy = (ans_r.sumy + (b / c)) % P; auto, ; } return ans; } Node euclid(ll p, ll q, ll r, ll n, Node U, Node R) { if (!n

Full text and comments »

  • Vote: I like it
  • +13
  • Vote: I do not like it

9.
By UnexpectedValue, 21 month(s) ago, In English
[Tutorial] A New Perspective on Numbers and Operators: Introducing Group Theory We often start our mathematical journey with counting, primes, and basic arithmetic. Then we encounter modular arithmetic, with its intriguing properties and theorems like Euclid's algorithm and the Chinese Remainder Theorem. But what if there's a deeper, more abstract framework that ties all these concepts together? You might have encountered hints that group theory is behind some clever algorithms and data structures ([like this comment suggests](https://codeforces.me/blog/entry/103174?#comment-915676)). But if you've tried to learn about it, you might have found resources that felt either too abstract and disconnected from your existing knowledge ([for instance](https://zhtluo.com/cp/from-burnside-to-polya-a-short-introduction-to-group-theory.html)), or too intimidating and complex ([like this one](https://codeforces.me/blog/entry/91731)). This blog aims to bridge that gap. We'll start with familiar ground in discrete math and modular arithmetic, and then progressively in...
= x' $ , we can use Euclid the same way with the base-case $r=0$ it will return $g = a$ , $ x = 0, $g = d$ , $ x = y' - qx' $ , and $ y = x' $ , we can use Euclid the same way with the base-case $r

Full text and comments »

  • Vote: I like it
  • +93
  • Vote: I do not like it

10.
By twoplusthree, 4 years ago, In English
Replay of JU BitFest Season One 2023 — Editorial Thank you for participating! We hope you enjoyed the problems! Links: - [**Contest Invitation link**](https://codeforces.me/contestInvitation/9c4ecdad63ab18b93d701d7012d9b52ed98a8224) - [**Group link**](https://codeforces.me/group/QV84qHXgrs) ## [A. Crush-ed](https://codeforces.me/gym/445869/problem/A) Author: [user:VS-Codes,2023-06-02] <spoiler summary="Tutorial"> The problem statement is simple enough, you can do all tasks up to difficulty $k$ every hour, and you need to maximize the time spent. However, you do need to perform a task every hour. So it is always optimal to start with the easiest task(lowest difficulty), do all tasks of that difficulty, and gradually move up. The answer (maximum time spent) is the total number of unique difficulties present in the array. </spoiler> <spoiler summary="Implementation"> ```c++ #include<bits/stdc++.h> #define int long long #define endl "\n" #define FastIO ios_base::sync_with_stdio(false); cin.tie(NULL); using n...
) { ll x, y, g = euclid(a.x, mod, x, y); assert(g == 1); return Mod((x + mod) % mod); } Mod, ; typedef pair pii; const ll mod = 998244353; // change to something else ll euclid, ll euclid(ll a, ll b, ll &x, ll &y) { if (!b) return x = 1, y = 0, a; ll d = euclid(b, a % b, y

Full text and comments »

  • Vote: I like it
  • +38
  • Vote: I do not like it

11.
By PokemonMaster, history, 14 months ago, translation, In English
Introducing GCD in math and CP # Introducing **gcd**: sacrifice one variable to gain tight constraints In problems with two (or more) integer variables it’s often profitable to **factor out their gcd**: let $d=\gcd(x,y)$, then write $x=a d,\ y=b d$ with **$\gcd(a,b)=1$**. Sure, you’ve added one more variable ($d$), but in exchange you get the **coprimality** of $a,b$, which sharply restricts the search space, makes divisibility statements “split” into clean factors, and simplifies the final casework/estimates. --- ## How to apply (6-step template) 1. **Normalize.** Handle zeros/signs first (e.g., consider the cases $x=0\leftrightarrow y=0$, $x,y>0$, etc.). 2. **Introduce gcd.** Put $d=\gcd(x,y)$, set $x=a d,\ y=b d$ with $\gcd(a,b)=1$. 3. **Cancel powers of $d$.** Divide the original equation/divisibility by the **maximal** power of $d$. 4. **Split divisibility.** Any statement like $U(a,b)\mid V(a,b)$ with coprime pieces often forces a “big” factor to divide one **rigid** component. 5. **Finish...
line facts (by Euclid) you’ll use repeatedly:, ## Mini-lemma library (all 1–3 lines via Euclid), . **Finish with tiny gcd-lemmas.** Standard 1–3 line facts (by Euclid) you’ll use repeatedly: * $\gcd

Full text and comments »

  • Vote: I like it
  • +35
  • Vote: I do not like it

12.
By hydra_cody, 17 months ago, In English
Number theory blog post collection ### Permutations - [Tutorial] A comprehensive guide to permutations for beginners [by](https://codeforces.me/blog/entry/111187) [user:nor,2024-01-02] ### Modular Arithmetic - Modular Arithmetic for Beginners [by](https://codeforces.me/blog/entry/72527) [user:Spheniscine,2024-01-02] - If mod(%) are so expensive why not make own modulus? [by](https://codeforces.me/blog/entry/80609) [user:Qualified,2024-01-02] ### Binary Exponentiation - Binary Exponentiation [by](https://cp-algorithms.com/algebra/binary-exp.html) cp-algo - Calculating Series Sums with Binary Exponentiation [by](https://codeforces.me/blog/entry/122195) [user:UnexpectedValue,2024-01-02] - Binary Exponentiation [by](https://www.youtube.com/watch?v=L-Wzglnm4dM) [user:Errichto,2024-01-02] **Problems** -- [Set 1](https://vjudge.net/contest/484703) ### Factors - [tutorial]Counting Divis...
greatest common divisor [by](https://cp-algorithms.com/algebra/euclid -algorithm.html) cp-algo, - Extended Euclidean Algorithm [by](https://cp-algorithms.com/algebra/extended- euclid, /algebra/euclid-algorithm.html) cp-algo

Full text and comments »

  • Vote: I like it
  • +65
  • Vote: I do not like it

13.
By gridnevvvit, 14 years ago, translation, In English
Editorial Codeforces Round #184 (Div.2) ## [problem:305A] All you have to do is implement following algorithm: 1. If we have numbers $0$ or $100$, we take them to needed subset. 2. If we got number greater than $0$ and less than 10, we take it. 3. If we got number $x \in [10;100)$ divisible by 10 we take it. 4. In case we have no numbers of second and third type, we take a number $x \in [10;100)$ that is not divisible by 10 [Solution](http://pastebin.com/nQXi0Khh) ## [problem:305B] There are at most two ways to represent rational fraction as continued. Using Euclid algorithm you can do that for $\frac{p}{q}$ and then check equality of corresponding $a_i$. [Solution](http://pastebin.com/x4rXtxen) ## [problem:305C] First of all, let's carry over all powers of two in the following way: if we have $a_i = a_j$, $i \ne j$, carry 1 to $a_i + 1$. Now as all of $a_i$ are distinct, the answer is $max(a_i)$ &mdash; $cnt(a_i)$ + 1, where $max(a_i)$ &mdash; maximal value of $a_i$,$cnt(a_i)$ &mdash; size of $a$ ...
represent rational fraction as continued. Using Euclid algorithm you can do that for $\frac{p}{q}$ and, There are at most two ways to represent rational fraction as continued. Using Euclid algorithm you

Full text and comments »

  • Vote: I like it
  • +42
  • Vote: I do not like it

14.
By 7oSkaaa, history, 5 years ago, In English
Topics Problems Good Day to you! While a lot of us, when he begins to start competitive programming he found difficult to found problems on some topics in the beginning to practice of it, and same thing for a lot of ICPC Communites that they have started. These are some topic not advanced and videos, problems and articles on them. <br> <spoiler summary="Prefix Sum & Frequency Array"> [Wonderful Coloring](https://codeforces.me/contest/1551/problem/B1) [Do Not Be Distracted](https://codeforces.me/contest/1520/problem/A) [Letter](https://codeforces.me/problemset/problem/43/B) [Pangram](https://codeforces.me/problemset/problem/520/A) [Andryusha and Socks](https://codeforces.me/contest/782/problem/A) [Count Numbers](https://codeforces.me/group/c3FDl9EUi9/contest/262795/problem/A) [Count Characters](https://codeforces.me/group/c3FDl9EUi9/contest/262795/problem/B) [Range Sum Query](https://codeforces.me/group/c3FDl9EUi9/contest/262795/problem/E) [Count a's](https...
[CP Algorithms](https://cp-algorithms.com/algebra/euclid

Full text and comments »

  • Vote: I like it
  • +23
  • Vote: I do not like it

15.
By Yagnik_Dhameliya, 3 years ago, In English
Epiphany 13.0 — Editorial ###[Epiphany 13.0](https://codeforces.me/gym/494905) **A: Gifts** ------------------------------ [user:jaineshmachhi,2023-12-27] <spoiler summary="Tutorial"> ###[Epiphany 13.0 A &mdash; Gifts](https://codeforces.me/gym/494905/problem/A) The minimum number of operations required to sort a list of $n$ gifts is $n−x$, where $x$ is the length of the longest sorted suffix. **Proof:** Let $k$ be the minimum number of steps required to sort the gifts. Observe that if $k$ steps are optimal, the first $k$ gifts must be moved, implying that the remaining $n-k$ gifts were already sorted in ascending order. Consider the $n-k$ sorted gifts at the end. We can iteratively insert the unsorted gifts into this suffix to achieve complete sorting: Pick the first unsorted gift and place it in its correct position within the sorted suffix, maintaining the sorted order. Repeat this process for the remaining $k−1$ unsorted gifts. This takes $k$ operations. </spoiler> <spoiler summ...
)),\;\; t \in \mathbb{Z}$$ Result 2: Using the [Euclid Algorithm](https://cp-algorithms.com/algebra, Result 2: Using the [Euclid Algorithm](https://cp-algorithms.com/algebra/extended-euclid

Full text and comments »

  • Vote: I like it
  • +17
  • Vote: I do not like it

16.
By Jose_17, 6 months ago, In English
Farthest Voronoi Diagram Recently I've been looking at the problem [Euclid](https://qoj.ac/contest/2156/problem/12226). Basically, it requires computing the regions of the farthest-point Voronoi diagram in $O(n \log n)$ or better. We know that the nearest can be computed with D&C, Fortune's Algorithm, Delaunay Triangulation, 3D Convex Hull (as mentioned by [Monogon](https://codeforces.me/profile/Monogon) in his [Efficient 3D Convex Hull Tutorial](https://codeforces.me/blog/entry/81768)). After doing some research, I found [The farthest point Delaunay triangulation minimizes angles] (https://www.sciencedirect.com/science/article/pii/092577219290013I), in which David Eppstein notes that 'It is a curious fact that both the nearest and farthest point Voronoi diagrams can be obtained as the planar projections of the lower and upper portions of the convex hull of the transformed point set'. So, that's it. However, I want to detail the process in case it helps anyone, and also highlight a few details that ...
### Problems - [Jumping to Victory](https://codeforces.me/gym/104555/problem/J) - [Euclid](https, Recently I've been looking at the problem [Euclid ](https://qoj.ac/contest/2156/problem/12226

Full text and comments »

  • Vote: I like it
  • +20
  • Vote: I do not like it

17.
By Siyah, history, 3 years ago, In English
[Cpp Tricks] [part One] ### OK &mdash; I GOT -20 , IGNORE THIS FUCK THIS LIFE **Hi ^~^** #### Part One [Useful Functiones]: **1.1 &mdash; power-Function:** **[Binary Exponentiation]** is a trick which allows to calculate $a^n$ using $O(\log n) $ The idea is that we can traverse through all the bits of a number from LSB to MSB in $O(\log n) $ time. Write $n$ in base $2$. The number has exactly $ \lfloor \log_2 n \rfloor + 1 $ digits in base 2, we only need to perform $O(\log n) $ multiplications, if we know the powers $a^1, a^2, a^4, a^8, \dots, a^{2^{\lfloor \log n \rfloor}}$ . <spoiler summary="Implementation"> ```c++ int dnt_pow(int a, int b, int md = mod) { int pw_ans = 1; while(b) { if(b&1) { pw_ans = (a*pw_ans)%md; } a = (a*a)%md; b >>= 1; } return pw_ans ; } ``` </spoiler> **1.2 &mdash; GCD-Function:** **[Euclidean algorithm]** is a trick which allows to calculate $gcd(a,b) $ using $O(\log \min(a, b))$ The idea is that subtrac...
and Binary GCD you can read [This](https://cp-algorithms.com/algebra/euclid -algorithm.html, For Time Complexity and Binary GCD you can read [This](https://cp-algorithms.com/algebra/euclid

Full text and comments »

  • Vote: I like it
  • -30
  • Vote: I do not like it

18.
By shishyando, 5 years ago, In English
Codeforces Round #792 (Div. 1 + Div. 2) Editorial We know about FST and we are really, really sorry about it. Even though we still hope that you liked the problems. <spoiler summary="A: Digit Minimization"> [problem:1684A] <spoiler summary="Editorial"> [tutorial:1684A] </spoiler> <spoiler summary="Implementation"> ~~~~~ #include <bits/stdc++.h> using namespace std; int main() { int T; cin >> T; while (T --> 0) { string n; cin >> n; if (n.size() == 2) { cout << n[1] << '\n'; } else { cout << *min_element(n.begin(), n.end()) << '\n'; } } return 0; } ~~~~~ </spoiler> </spoiler> <spoiler summary="B: Z mod X = C"> [problem:1684B] <spoiler summary="Editorial"> [tutorial:1684B] </spoiler> <spoiler summary="Implementation"> ~~~~~ #include <bits/stdc++.h> using namespace std; void solve() { int a, b, c; cin >> a >> b >> c; cout << a + b + c << " " << b + c << " " << c << "\n"; } int main()...
[problem: 1684G]

Full text and comments »

  • Vote: I like it
  • -206
  • Vote: I do not like it

19.
By Sazzon, history, 10 years ago, In English
Contest da semana #1 Olá todos! Esse é o primeiro editorial feito pelos alunos da UECE no Codeforces. Esse contest foi criado como um treino do grupo de estudos da maratona. A seleção das questões do UVa, organização do contest foi feita com muito empenho pelo [user:alissonrgs,2016-11-26]. Obrigado especial aos que ajudaram a fazer este editorial: [user:wjoao,2016-11-26], [user:Lamartinec,2016-11-26] e novamente ao [user:alissonrgs,2016-11-26]. Por favor, leiam as questões, tentem fazer, se não conseguirem leiam o editorial. **Em último caso** vejam o código. **Sorry for the lack of translation, we are from Brazil and the group is closed. Don't downvote this post just because you don't understand the language. We are using the platform to communicate internally. Please be nice !** :-) [Burguer Time?](https://uva.onlinejudge.org/external/116/11661.pdf) ------------------ **Pré-requisitos:** Nenhum O problema é facilmente resolvido se pensarmos que se houver um restaurante e uma farmácia no...
Cabral [Euclid Problem](https://uva.onlinejudge.org/external/101/10104.pdf, [Euclid Problem](https://uva.onlinejudge.org/external/101/10104.pdf) ------------------, int main(){ ll a, b; while(~scanf("%lld %lld", &a, &b)){ euclid(a, b); printf("%lld, void euclid(ll a, ll b){ if(b == 0){ x = 1; y = 0; d = a; return

Full text and comments »

  • Vote: I like it
  • +2
  • Vote: I do not like it

20.
By ChallaAjay, history, 6 weeks ago, In English
GCD and LCM: the small things I wish I knew earlier Hi everyone! I'm not a high-rated guy (just ~1300), so this isn't some deep post. I just noticed that gcd and lcm show up in a LOT of problems, and every time I would forget some tiny detail and lose time on it. So I wrote everything in one place. Maybe it helps someone who is at the same stage as me, and if I got something wrong please tell me in the comments, I'll fix it :) ### 1. Euclid's algorithm The whole idea is one line: `gcd(a, b) = gcd(b, a % b)`, and when `b = 0` the answer is `a`. ~~~~~ long long gcd(long long a, long long b) { while (b) { a %= b; swap(a, b); } return a; } ~~~~~ If you use C++17 you can just write `std::gcd(a, b)` (it's in `<numeric>`). On older compilers `__gcd(a, b)` works too. It runs in `O(log(min(a, b)))`. Reason: every two steps the bigger number becomes at least half of what it was. So even for numbers around 10^18 it's like 90 steps, nothing to worry about. One small thing: `gcd(0, x) = x`. Th...
### 5. Extended Euclid

Full text and comments »

  • Vote: I like it
  • -17
  • Vote: I do not like it

21.
By GlebsHP, history, 9 years ago, translation, In English
Разбор задач разминочного раунда Яндекс.Алгоритма 2018 A. Time Through the Glass --------------------- Consider the movement of "real" and "reflected" hands. If "real" hands rotates for some angle, "reflected" hand passes exactly the same angle in other direction. Thus, the sum of angles of two hands is always equal 360 degrees. For each hand we will find its resulting position independently. For hour hand it is 12 minus the current position, while for minute hand it is 60 minus current position. In both cases we should not forget to replace 12 or 60 with 0. B. Palindromic Feature ----------------------- Consider some substring of the given string that is a palindrome. We can remove its first character and its last character and the string will remain a palindrome. Continue this process till the string length becomes two or three (depending on parity). The number of substrings of length two or three is linear, same as their total length. Thus, we can apply naive algorithm to select optimum palindrome. If there is no palinromi...
similar to Euclid algorithm and continues while we don't have $1$ and $x$. Finally, pairs $(1, x, $) and so one. The process is similar to Euclid algorithm and continues while we don't have $1$ and $x

Full text and comments »

  • Vote: I like it
  • +64
  • Vote: I do not like it

22.
By Kefrov, history, 2 years ago, In English
Proof for Modular Division trick This short video serves as proof for the following mathematical statement: <br> $$ \displaystyle \textbf{(H)} : \frac{a}{b} \mod m = a \cdot b^{-1} \mod m $$ $$ \displaystyle \text{Such that } b \mid a \text{ and} \gcd(b, m) = 1 $$ <iframe allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" allowfullscreen="" frameborder="0" height="500" referrerpolicy="strict-origin-when-cross-origin" src="https://www.youtube.com/embed/BMDUYcgt1A8" title="YouTube video player" width="850"></iframe> <br> [problem:1999F] urged me to study modular arithmetic, specifically modular division. However, no document or video I found contained this proof, so I tried to prove it myself. It's not hard, so maybe that's why I couldn't find it, or maybe I didn't search well enough. Anyway, often we are tasked with calculating a large number modulo $10^9 + 7$. For this, we need to perform some modular arithmetic tricks, but things get complica...
href="https://cp-algorithms.com/algebra/extended-euclid -algorithm.html">Extended Euclidean Algorithm, We can easily find $b^{-1}$ using the

Full text and comments »

  • Vote: I like it
  • +8
  • Vote: I do not like it

23.
By saelcc03, history, 2 years ago, In English
Extended euclidean algorithm — iterative version of cp algorithms HI WORLD ------------------ I love cp algorithms articles but sometimes there are some proofnesses that I can't figure out by myself. I've just checked the proofness of transition of coefficients in [extended euclidian algorithm](https://cp-algorithms.com/algebra/extended-euclid-algorithm.html) and I'd like to share my explanation here. Given $a$ and $b$, we need to calculate $x$ and $y$ such that satisfies Bézout's identity: $ax + by = gcd(a,b)$. Starting with values $x=1$, $y=0$, $x_1=0$, $y_1=1$, $a_1=a$, $b_1=b$, after some iterations $b_1$ will become $0$ and $a_1$ will become $gcd(a,b)$: ~~~~~ // by cp algorithms int gcd(int a, int b, int& x, int& y) { x = 1, y = 0; int x1 = 0, y1 = 1, a1 = a, b1 = b; while (b1) { int q = a1 / b1; tie(x, x1) = make_tuple(x1, x - q * x1); tie(y, y1) = make_tuple(y1, y - q * y1); tie(a1, b1) = make_tuple(b1, a1 - q * b1); } return a1; } ~~~~~ We have these transitions:...
algorithm](https://cp-algorithms.com/algebra/extended-euclid-algorithm.html) and I'd like to share my, in [extended euclidian algorithm](https://cp-algorithms.com/algebra/extended- euclid-algorithm.html

Full text and comments »

  • Vote: I like it
  • +24
  • Vote: I do not like it

24.
By Edvard, history, 10 years ago, translation, In English
Editorial of Educational Codeforces Round 16 ### [problem:710A] Easy to see that there are only three cases in this problem. If the king is in the corner of the board the answer is $3$. If the king is on the border of the board but not in a corner then the answer is $5$. Otherwise the answer is $8$. <spoiler summary="С++ solution"> ~~~~~ char c, d; bool read() { return !!(cin >> c >> d); } void solve() { int cnt = 0; if (c == 'a' || c == 'h') cnt++; if (d == '1' || d == '8') cnt++; if (cnt == 0) puts("8"); else if (cnt == 1) puts("5"); else if (cnt == 2) puts("3"); else throw; } ~~~~~ </spoiler> Complexity: $O(1)$. ### [problem:710B] The function of the total distance is monotonic between any pair of adjacent points from the input, so the answer is always in some of the given points. We can use that observation to solve the problem by calculating the total distance for each point from the input and finding the optimal point. The other solution uses the observation that the answer ...
extended Euclid algorithm and $p$ is any integral number. The variable $p$ should satisfy two

Full text and comments »

  • Vote: I like it
  • +11
  • Vote: I do not like it

25.
By sslotin, 5 years ago, In English
Modular inverse, Montgomery multiplication, and integer factorization I've been working on computational number theory for [my book](https://en.algorithmica.org/hpc/) and recently wrote a few new articles on: - calculating modular inverse using [binary exponentiation](https://en.algorithmica.org/hpc/number-theory/exponentiation/) and [extended Euclidean algorithm](https://en.algorithmica.org/hpc/number-theory/euclid-extended/) (spoiler: Euclid wins _on average_); - [Montgomery multiplication](https://en.algorithmica.org/hpc/number-theory/montgomery/) and fast [modulo / division / divisibility checks](https://en.algorithmica.org/hpc/arithmetic/division/) for when the divisor is constant (which can often be used to speed up modular arithmetic by 5-15x); - how to use them in various [factorization algorithms](https://en.algorithmica.org/hpc/algorithms/factorization/) (upd: fixed link) (writing a 3x-faster-than-state-of-the-art implementation of Pollard's algorithm in the process). This is largely inspired by [user:Jakube,2022-05-26]'s [tutorials](ht...
-theory/euclid-extended/) (spoiler: Euclid wins _on average_); - [Montgomery multiplication](https, ](https://en.algorithmica.org/hpc/number-theory/euclid-extended/) (spoiler: Euclid wins _on average_

Full text and comments »

  • Vote: I like it
  • +68
  • Vote: I do not like it

26.
By AM_I_Learning, history, 6 years ago, In English
Unofficial Editorial for A to D problems Codeforces Round 675 Div2. <spoiler summary="A"> This problem says we need to find 4th side length of the quadrilateral given 3 lengths. There are various approaches to solve this problem. WHat I thought was that align one line parallel to OY axis and aling one line parallel to x axis and in the middle align the other line. So we get a structure something like this. | D | | | C | | A |_______| B Now I got 4 coordinates with three lines so 4th line is obviously. CD if AB=a , BC =b , AD =c , and assume A to be origin. Coordinate of C={a,b} , D={ 0,c}. Thus length of CD = $$ceil(sqrt((a-0)*(a-0)+(b-c)*(b-c)))$$. By euclids formula. Why ceil? because to make float to integer as our quadrilateral can have sides which are aligned at angle to origin thus ceil works fine here. </spoiler> [submission:94665721] <spoiler summary="B"> In this problem we were required to make all rows and columns of the matrix palindromic. RO...

Full text and comments »

  • Vote: I like it
  • +127
  • Vote: I do not like it

27.
By daihan, 9 years ago, In English
What is the time complexity of Euclidian algorithm . I searched google , about this , I understand only that worst case happens when a,b are consecutive fibonacci numbers . Someone says Euclidian algo's time complexity is O(log(a+b) ) , some says O(log(n) ) . But their explanation is not clear to me . I read the first answer from here : https://stackoverflow.com/questions/3980416/time-complexity-of-euclids-algorithm . I pasted it bellow . It describes the analysis of euclid algo . ~~~~~ "One trick for analyzing the time complexity of Euclid's algorithm is to follow what happens over two iterations: a', b' := a % b, b % (a % b) Now a and b will both decrease, instead of only one, which makes the analysis easier. You can divide it into cases: Tiny A: 2a <= b Tiny B: 2b <= a Small A: 2a > b but a < b Small B: 2b > a but b < a Equal: a == b Now we'll show that every single case decreases the total a+b by at least a quarter: Tiny A: b % (a % b) < a and 2a <= b, so b is decreased by at least half, so a+b decreased b...
. It describes the analysis of euclid algo . ~~~~~ "One trick for analyzing the time complexity of, I pasted it bellow . It describes the analysis of euclid algo .

Full text and comments »

  • Vote: I like it
  • +3
  • Vote: I do not like it

28.
By MarioYC, 15 years ago, In English
SGU 106 I'm trying to solve this problem, however I'm getting WA on test 15. I first handle cases where a = 0 or b = 0, then solve a != 0 and b != 0 by using extended euclid algorithm to calculate k = -a^{-1}c (mod b), which replaced in the equation gives me to inequalities.<div><br></div><div><meta http-equiv="content-type" content="text/html; charset=utf-8"><span class="Apple-style-span" style="-webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; "><pre style="color: rgb(0, 0, 0); background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: rgb(255, 255, 255); background-position: initial initial; background-repeat: initial initial; "><span style="color: rgb(0, 74, 67); ">#</span><span style="color: rgb(0, 74, 67); ">include </span><span style="color: rgb(128, 0, 0); ">&lt;</span><span style="color: rgb(64, 1, 90); ">cstdio</span><span style="color: rgb(128, 0, 0); ">&gt;</span> <span style="color...
= 0 or b = 0, then solve a != 0 and b != 0 by using extended euclid algorithm to calculate k = -a^{-1

Full text and comments »

  • Vote: I like it
  • 0
  • Vote: I do not like it

29.
By Nourhan_Abo-Heba, history, 12 months ago, In English
GCD, LCM, Modular Arithmetic & Generating Divisors — a Codeforces-style Guide **Tags:** `#math` `#number-theory` `#implementation` `#templates` **Difficulty:** ★☆☆ — ★★☆ **TL;DR:** Treat every number as a multiset of prime powers (a “box of primes”). * `gcd` = take **minimum** exponent for each prime. * `lcm` = take **maximum** exponent for each prime. * Use Euclid (`%`) for fast `gcd`. * For modular division with prime modulus, use Fermat → inverse = `pow(a, MOD-2)`. * Generate all divisors by recursion over prime powers. --- ## 1) Numbers = Boxes of Prime Factors Example: * `12 = 2^2 * 3^1` → box: {2,2,3} * `6 = 2^1 * 3^1` → multiplying by 6 is like *adding* `2` and `3` into the box. If * `24 = 2^3 * 3^1` * `30 = 2^1 * 3^1 * 5^1` then `gcd(30,24)` uses **min exponents**: `2^min(3,1)*3^min(1,1)*5^min(0,1) = 2^1 * 3^1 = 6`. `lcm` uses **max exponents**: `2^3 * 3^1 * 5^1 = 120`. --- ## 2) GCD — Properties & Implementation Properties: * `gcd(a,b) = gcd(b,a%b)` (Euclid) * `gcd(a,0) = a` * `gcd(a,b) = gcd(a-b, b)` (su...
**minimum** exponent for each prime. * `lcm` = take **maximum** exponent for each prime. * UseEuclid, prime. * Use Euclid (`%`) for fast `gcd`. * For modular division with prime modulus, use Fermat, * `gcd(a,b) = gcd(b,a%b)` (Euclid) * `gcd(a,0) = a` * `gcd(a,b) = gcd(a-b, b)` (subtraction

Full text and comments »

  • Vote: I like it
  • -22
  • Vote: I do not like it

30.
By Arpa, history, 6 months ago, In English
Codeforces Round 1102 (Div. 2) – Video Tutorial <center> <iframe width="560" height="315" src="https://www.youtube.com/embed/RUi476wcJYc" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe> </center> <table border="1" cellpadding="6" cellspacing="0" width="100%"> <thead> <tr> <th>Problem</th> <th>Name</th> <th>Submission</th> </tr> </thead> <tbody> <tr> <td>A</td> <td>Euclid, Sequence and Two Numbers</td> <td>[submission:377690829]</td> </tr> <tr> <td>B</td> <td>Palindrome, Twelve and Two Terms</td> <td>[submission:377690818]</td> </tr> <tr> <td>C</td> <td>Vessels, Heights and Two Versions (Easy Version)</td> <td>[submission:377690805]</td> </tr> <tr> <td>D</td> <td>XOR, Expression and Two Binary Numbe...
> A Euclid, Sequence and Two Numbers

Full text and comments »

  • Vote: I like it
  • +3
  • Vote: I do not like it

31.
By Team5_HuyenTrang, history, 3 years ago, In English
[ProPTIT_PracticeD23_C++] Function and Recursion — Lời giải [A. [ProPTIT] Số đối xứng](https://codeforces.me/group/NIQkV4Z4K5/contest/505338/problem/A) <spoiler summary="Lời giải"> - Tạo 1 số mới là số đảo ngược của số ban đầu - So sánh số ban đầu và số đảo ngược </spoiler> <spoiler summary="Code"> https://ideone.com/FHJWg0 </spoiler> [B. [ProPTIT] Số nén tối giản](https://codeforces.me/group/NIQkV4Z4K5/contest/505338/problem/B) <spoiler summary="Lời giải"> - Đệ quy: điều kiện dừng khi n < 10 - Gán n bằng tổng các chữ số của n </spoiler> <spoiler summary="Code"> https://ideone.com/EinmF4 </spoiler> [C. [ProPTIT] Lũy thừa](https://codeforces.me/group/NIQkV4Z4K5/contest/505338/problem/C) <spoiler summary="Lời giải"> - Đệ quy: điều kiện dừng: số mũ = 1 hoặc 0 - Sử dụng công thức truy hồi: $a^b = a^{b-1} * a$ </spoiler> <spoiler summary="Code"> https://ideone.com/jMxMt2 </spoiler> [D. [ProPTIT] Tổ hợp](https://codeforces.me/group/NIQkV4Z4K5/contest/505338/problem/D) <spoiler summary="Lời giải"> - Đ...
- Tìm GCD: sử dụng thuật toán Euclid,... - Tìm $LCM = a * b / GCD(a, /contest/505338/problem/H) - Tìm GCD: sử dụng thuật toán Euclid

Full text and comments »

  • Vote: I like it
  • -6
  • Vote: I do not like it

32.
By caokynb, 7 weeks ago, In English
PRACTICE 2 — CHỮA BÀI ### [A. Tổng các chữ số](https://codeforces.me/group/ScLsFaangZ/contest/720586/problem/A) <spoiler summary="Ý Tưởng"> Bài toán yêu cầu tính tổng các chữ số của một số nguyên dương $N$ với nhiều bộ test $T$. Vì $N \le 10^{18}$ vượt quá phạm vi của kiểu `int` 32-bit, ta cần dùng kiểu `long long` trong C++. **Thuật toán xử lý** Với mỗi bộ test: 1. Khởi tạo biến `sum = 0` để lưu tổng các chữ số. 2. Sử dụng vòng lặp `while (N > 0)`: - Lấy chữ số cuối bằng `N % 10` và cộng vào `sum`. - Loại bỏ chữ số cuối bằng phép chia nguyên `N /= 10`. 3. In ra `sum` sau khi vòng lặp kết thúc. **Cách khác — xử lý bằng chuỗi:** Đọc $N$ dưới dạng `string`, duyệt từng ký tự và cộng giá trị `c - '0'`. Cả hai cách đều hiệu quả. - **Độ phức tạp thời gian:** $O(T \times \log_{10}(N))$. Với $T \le 100$ và $N \le 10^{18}$, có khoảng tối đa $100 \times 18 \approx 1800$ bước xử lý chữ số. - **Độ phức tạp không gian:** $O(1)$. </spoiler> <spoiler summary="Code C++"> [Code](...
** 1. Tính $G=\gcd(A,B)$ bằng thuật toán Euclid (`std::gcd`). 2. Nếu $G=1$, in `-1`. 3. Duyệt, 1. Tính $G=\gcd(A,B)$ bằng thuật toán Euclid (`std::gcd`). 2. Nếu $G=1$, in `-1`. 3. Duyệt $i$ từ

Full text and comments »

  • Vote: I like it
  • 0
  • Vote: I do not like it

33.
By alhod, history, 3 years ago, In English
Codeforces/DMOJ but for Competitive Math To all those that compete in math contests like the AMC in the US or Euclid in Canada, would you be interested in a platform similar to Codeforces or DMOJ but for competitive math? A few months ago I was preparing for the Euclid, and wanted to look for a platform similar to Codeforces or DMOJ but hosted virtual math competitions instead, because only doing past problems and mock contests was getting kinda dull. However, I couldn't find any such platform like this. Thinking about it now, competitive math seems to be much more popular (at least at a high school level) compared to competitive programming, yet it lacks any platforms that host online contests, use a rating system, and have an active community. I'm thinking of creating such a platform, but I first wanted to get a sense of what others in the competitive math field thought of the idea. Would you be interested in such a platform? Why or why not? Do you have any advice/suggestions for developing such a platform? Also, ...
A few months ago I was preparing for the Euclid, and wanted to look for a platform similar to, To all those that compete in math contests like the AMC in the US or Euclid in Canada, would you be

Full text and comments »

  • Vote: I like it
  • +7
  • Vote: I do not like it

34.
By i_am_eating_wa_and_tle, history, 9 years ago, In English
Why not Extended Euclid?? Problem Link: [http://lightoj.com/volume_showproblem.php?problem=1077](http://lightoj.com/volume_showproblem.php?problem=1077) For pdf [Click here](http://lightoj.com/volume_showproblem.php?problem=1077&language=english&type=pdf) As the problem says to find the lattice points from (Ax, Ay) to (Bx, By).My idea was to find the number of solutions of the equation x * (Ay-By) &mdash; y * (Ax-Bx) = Ax * ( Ay-By) &mdash; Ay * (Ax-Bx) which is basically Extended Euclid. I got WA so many times. And after that I found [this](https://math.stackexchange.com/questions/628117/how-to-count-lattice-points-on-a-line). But I can't understand why this problem can't be solved using Extended Euclid. Can anybody Please explain ? After solving this problem I thought either I don't understand Extended Euclid or there is a bug in my code.
Why not Extended Euclid??, ) = Ax * ( Ay-By) — Ay * (Ax-Bx) which is basically Extended Euclid. I got WA so many times, -Bx) which is basically Extended Euclid. I got WA so many times. And after that I found [this](https

Full text and comments »

35.
By be_right_back_in_2018, history, 10 years ago, In English
Trade-offs between Mathematics and CP Hello guys, it's been a long while since I've last used Codeforces.. I neither have a question nor aiming to discuss a problem, it's more like a survey and I really like to listen to all of your different opinions Do you think there are any trade-offs between Mathematics and Competitive Programming? I mean, I've almost always thought that one cannot be great at both of the fields, though it's important to have some essential skills of the other field no matter which side you belong My current reflection is: Mathematics is so important not only for CP but for CS and any other scientific field, but it concerns itself about some so deep low-level worries that programmers tend to take for granted, since it's almost always the case that they use some pre-built tools and I personally think that though it's always better and more "efficient" to understand what's going on under the hood, it makes you less productive to worry so much about details and re-think about the same prob...
search techniques, but some problem emerged and required me to use Extended Euclid Algorithm to, Euclid Algorithm to calculate the Modular Multiplicative Inverse of some number, and since I

Full text and comments »

36.
By daihan, 9 years ago, In English
Given A, B , N . Find x , y such that (A*x + B*y) = N You are given _**N** , **A** , **B**_ find such x and y that **(A*x + B*y) = N** and **x>=0 , y>=0** . It is not guaranteed that N will be always gcd(a,b) . If such x , y does not exist print -1 , either print x and y in same line separated by space , if there are multiple x , y that satisfy the equation print any of them . **For example:** If N=9 , A=2 , B=5 then ans is 2 1 . Because 2*2+5*1=9 . Here A=2 , x=2 and B=5 , y=1 . **Constraint:** 1<= N , A , B <= 10^18 **INPUT** 9 2 5 22 3 7 439365 78717 87873 505383 77277 99291 1000000 3 999997 474441 99291 77277 911106 51038 78188 321361 79845 81826 1000000000000000000 8765433 54343345 **OUTPUT** 2 1 5 1 0 5 -1 1 1 4 1 1 11 3 1 25359400 18397426840 I have solved using linear loop . for(long long i=0; i<=n/a; i++) if((n-i*a)%b==0) { printf("%lld %lld\n",i,(n-i*a)/b); return 0; ...
**UPD solved it using extended euclid here is the code** : https://ideone.com/6fzPLF

Full text and comments »

  • Vote: I like it
  • -15
  • Vote: I do not like it

37.
By justnemat, history, 14 months ago, In English
Should I Focus on Projects or Aim for a CCO Medal? Lately, I’ve been asking myself a big question: what’s the best path to stand out for admission into top universities in Canada and the U.S.? Should I invest my time into creating a few meaningful projects, or should I give my absolute best to earn a medal at the Canadian Computing Olympiad (CCO)? Option 1: Build 3 Projects Here are the three projects I’m considering: 1. Competitive Programming Tracker – A web app that tracks coding contest progress, offering analytics and recommendations for improvement. 2. QuantSim – A simulation tool for basic trading strategies, designed for students curious about quantitative finance. 3. Euclid Puzzle Generator – A platform that generates math competition–style problems with step-by-step solutions. Option 2: Aim for a CCO Medal On the other hand, winning a medal at the CCO is one of the strongest signals of programming ability in Canada. It’s highly respected by top universities like Waterloo, MIT, and U of T. A medal could demon...
finance. 3. Euclid Puzzle Generator – A platform that generates math competition–style problems, students curious about quantitative finance. 3. Euclid Puzzle Generator – A platform that generates

Full text and comments »

  • Vote: I like it
  • -18
  • Vote: I do not like it

38.
By sahal, history, 7 years ago, In English
Extended Euclid! can anyone help me? How to solve this problem in Extended Euclid algorithm??? https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1574
Extended Euclid!, can anyone help me? How to solve this problem in Extended Euclid algorithm??? https

Full text and comments »

  • Vote: I like it
  • 0
  • Vote: I do not like it

39.
By I_love_tigersugar, 13 years ago, In English
Closest Point Pair Given N distinct points on the plane. Find the pair of point with the smallest (and largest) Euclid distance between them. I only know the Brute-Force O(n^2) algorithm to solve this. Can someone give me any faster algorithm? Thanks in advance.
Given N distinct points on the plane. Find the pair of point with the smallest (and largest)Euclid

Full text and comments »

  • Vote: I like it
  • +14
  • Vote: I do not like it

40.
By Qualified, history, 6 years ago, In English
If mod(%) are so expensive why not make own modulus? I see [this](https://cp-algorithms.com/algebra/euclid-algorithm.html) article and at the bottom, it says that modulus operators are expensive so they implemented a slightly faster version of Euclidean Algorithm. Why not make a more efficient mod? ~~~~~ int mod(int a, int b) { // computes a % b; return (a - b * (a / b)); } ~~~~~
I see [this](https://cp-algorithms.com/algebra/euclid-algorithm.html) article and at the bottom, it

Full text and comments »

  • Vote: I like it
  • +87
  • Vote: I do not like it

41.
By caokynb, history, 2 months ago, In English
PRACTICE 1 — CHỮA BÀI ### [A. &#91;GCC TRAINING 2026 &mdash; PRACTICE 1&#93; Chữ Hoa chữ thường](https://codeforces.me/group/ScLsFaangZ/contest/719645/problem/A) <spoiler summary="Ý Tưởng"> - Trong bảng mã ASCII, các chữ cái in thường ('a' đến 'z') và các chữ cái in hoa ('A' đến 'Z') được sắp xếp liên tiếp nhau. - Khoảng cách giữa chữ thường và chữ hoa tương ứng luôn là một hằng số: 'a' &mdash; 'A' = 32. - Do đó, để chuyển một ký tự thường c thành ký tự hoa, ta chỉ cần: - Lấy c &mdash; 32 (hoặc trừ đi 'a' &mdash; 'A'). </spoiler> <spoiler summary="Code"> [Code](https://ideone.com/psG4Nf) </spoiler> ### [B. &#91;GCC TRAINING 2026 &mdash; PRACTICE 1&#93; Tính tiền mua hàng](https://codeforces.me/group/ScLsFaangZ/contest/719645/problem/B) <spoiler summary="Ý Tưởng"> - Tổng số tiền cần thanh toán là tích giữa đơn giá và số lượng: $\text{Tổng tiền} = p \times n$. - Vì $p$ là số thực nên ta dùng kiểu dữ liệu `double`, còn $n$ là số nguyên dùng kiểu `int`. - Để in ra chính xác...
= \sqrt{x_0^2 + y_0^2}$ (Khoảng cách Euclid). - `type == 2`: $P = \vert{}x_0\vert{} + \vert{}y_0, `:** - `type == 1`: $P = \sqrt{x_0^2 + y_0^2}$ (Khoảng cách Euclid). - `type == 2`: $P = \vert{}x_0

Full text and comments »

  • Vote: I like it
  • -3
  • Vote: I do not like it

42.
By ArepitaDePernil, 12 years ago, In English
Latin America Regional 2003 Kryptonite Mine (Live Archive 2885) Hello i am having troubles with this problem, i am getting w.a and i really don't know what else to modify, so if you guys can help me that would be great, ill explain what i do in my solution 1- I construct a graph with edges between teleporters, ends of segments, the start position and the end position, (the weight of this edge is the euclid distance) and an edge connects 2 points if there is no line segment (excluding end and start points) 2- I run a dijkstra [N_points][N_teleporters] , if i find a teleporter i can move to other connected teleporters with 0 cost. 3- i use the function round(A) in the answer i don't know if my solution is ok, or it needs some other thing, here is my code for more detail about my actual solution: http://pastie.org/private/7fkh9k8ngg6ue940phydq Thanks a lot!
end position, (the weight of this edge is the euclid distance) and an edge connects 2 points if

Full text and comments »

  • Vote: I like it
  • 0
  • Vote: I do not like it

43.
By acktron, history, 6 years ago, In English
[Video Tutorial] Dynamic Programming Tutorial with Fibonacci Sequence ![ ](https://i9.ytimg.com/vi/qRL_5G39Cfc/mqdefault.jpg?time=1616427600000&sqp=CND04oIG&rs=AOn4CLDbaCmwAGI5ix6YigOvca7BfuLt1A) I made a short tutorial on **Dynamic Programming** on my YouTube Channel explaining [Fibonacci Sequence with D.P.](https://www.youtube.com/watch?v=qRL_5G39Cfc) For more information you guys can refer this link :- [Fibonacci Numbers &mdash; Competitive Programming Algorithms](https://cp-algorithms.com/algebra/fibonacci-numbers.html) Here are some Famous practice **Problems on Fibonacci Sq**: [SPOJ &mdash; Euclid Algorithm Revisited](https://www.spoj.com/problems/MAIN74/) [SPOJ &mdash; Fibonacci Sum](https://www.spoj.com/problems/FIBOSUM/) [Project Euler &mdash; Even Fibonacci numbers](https://www.hackerrank.com/contests/projecteuler/challenges/euler002/problem) If you have any Doubt in Tutorial feel free to comment it:)......
**Problems on Fibonacci Sq**: [SPOJ — Euclid Algorithm Revisited](https://www.spoj.com, [SPOJ — Euclid Algorithm Revisited](https://www.spoj.com/problems/MAIN74/)

Full text and comments »

  • Vote: I like it
  • -4
  • Vote: I do not like it

44.
By RussianCodeCup, history, 10 years ago, translation, In English
Russian Code Cup 2016 Elimination Round — Editorial <div class="problem-statement"><div class="header"><div class="title">A. Important Test</div></div><div class="tutorial"><p>For each variant consider all prefixes of tasks. It is optimal to copy the task that takes most time, so we keep track of maximal time and calculate the sum <span class="tex-span"><i>S</i></span> of <span class="tex-span"><i>t</i><sub class="lower-index"><i>ij</i></sub></span> of the prefix. If the maximal time is greater than <span class="tex-span"><i>t</i><sub class="lower-index">0</sub></span> it is better to copy this task, getting the total time needed be <span class="tex-span"><i>S</i>&thinsp;-&thinsp;<i>max</i>(0,&thinsp;<i>M</i>&thinsp;-&thinsp;<i>t</i><sub class="lower-index">0</sub>)</span>. Now compare this value to <span class="tex-span"><i>t</i></span> to check whether it is possible to write down that many tasks for this variant.</div></div> <div class="problem-statement"><div class="header"><div class="title">B. Centipede</div></div><div class="tu...
class="lower-index">i, p + 1. Use extended Euclid, > + 1. Use extended Euclid algorithm to find s

Full text and comments »

  • Vote: I like it
  • +35
  • Vote: I do not like it

45.
By dragneel3131, history, 9 years ago, In English
Calcuate (a/b) mod p when p is not prime I have to calculate the value of (a/b) mod p where p may or may not be a prime number. For p being prime, I can simply find (b inverse for mod p) using euclid theorem or little fermat theorem, and perfrom (a * inverse(b)) % p. But in case p is not prime, how can I find the solution? I think there is a method by doing prime factorization of p and then using chineese remainder theorem but not sure about it. Can anyone help how can I solve this problem? a and b both are less than p.
being prime, I can simply find (b inverse for mod p) using euclid theorem or little fermat theorem

Full text and comments »

  • Vote: I like it
  • -18
  • Vote: I do not like it

46.
By abzaloid, 12 years ago, translation, In English
nearest neighbour problem Hi there, Nearest neighbour is a problem where you are given $N$ points $\bold{x}_i \in \mathbf{R}^d$ and you are asked to process online queries: for a new point $\bold{x}_{new}$ find the closest point from given $N$ points. Euclid distance is used: $|AB|= \sqrt{\sum_{i=1}^d (A_i - B_i)^2}$ Who knows where I can test this problem?
given $N$ points. Euclid distance is used: $|AB|= \sqrt{\sum_{i=1}^d (A_i - B_i)^2}$, point from given $N$ points. Euclid distance is used: $|AB|= \sqrt{\sum_{i=1}^d (A_i - B_i)^2

Full text and comments »

  • Vote: I like it
  • +13
  • Vote: I do not like it

47.
By snake, 17 years ago, In English
Contest 7 A.ad hoc<div>B.Simulation</div><div>C.Extended Euclid</div><div><br></div><div><br></div><div>First Contest Here, but not the last one. I like it</div><div><br></div><div>UPD:D.Palindrome Judgement(Use string hash function to decide if it is.O(n))</div>
C.Extended Euclid

Full text and comments »

  • Vote: I like it
  • 0
  • Vote: I do not like it