Location>code7788 >text

Eujili Delite related algorithms [including universal European Jear]]

Popularity:912 ℃/2025-01-29 13:18:32

Foreword

This article will introduce a few andOujiliAlgorithm -related algorithms, they all useTacklingCalculating strategy, obtaining similar to\(O(\log V)\)\(V\)Excellent complexity of the value domain).

This article will provide clear (mathematical formula for use\(\LaTeX\)Show)pseudocode

Agreed

  • For integer\(x,y\),definition\(z=x\bmod y\), And just be\(z\)The smalleNon -negative integer,satisfy\(y\mid (x-z)\)Essence The middle is uprightDividesymbol.

Find the largest convention

question: Give positive integer\(x,y\leq V\),beg\(\gcd(x,y)\)。(\(\gcd(x,y)\)express\(x\)and\(y\)The biggest convention)


Explain:like\(x=y\)Then ordinary,\(\gcd(x,y)=x\)Essence Might as well\(x<y\), Obvious\(\gcd(x,y)=\gcd(y-x,x)\), According to the definition of molding, you can get it\(\gcd(x,y)=\gcd(y\bmod x,x)\)So there are practices.


pseudocode: (The return value of the function is the answer)

  • function \(\text{solve}(x,y)\)
    • if \(x=0\) then
      • return \(y\)
    • return \(\text{solve}(y\bmod x,x)\)

Complexity analysis: Set time complexity\(\mathcal{T}(x,y)\)Now we prove the following proposition:

  • \(\displaystyle\exists x',y'\in \mathbb N^+,x'\leq \frac{x}{2},y'\leq y\),satisfy\(\mathcal{T}(x,y)\leq \mathcal{T}(x',y')+O(1)\)

If the proposition is established, it will be easy to launch\(\mathcal{T}(x,y)=O(\log V)\)It's right.

Might as well\(r=y\bmod x\)Discuss in two cases.

  1. like\(\displaystyle r\leq\frac{x}{2}\)According to the algorithm process,\(\mathcal{T}(x,y)=\mathcal{T}(x,r)+O(1)=\mathcal{T}(r,x)+O(1)\)The proposition was established.
  2. like\(\displaystyle r>\frac{x}{2}\)There are\(\mathcal{T}(x,y)=\mathcal{T}(r,x)+O(1)=\mathcal{T}(x\bmod r,x)+O(1)\),at this time\(\displaystyle x\bmod r=x-r\leq\frac{x}{2}\), So the proposition was established.

Therefore, the complexity of the algorithm is

\[\boxed{O(\log V)} \]

The complexity of the algorithm is similar to it and will no longer prove it.

Expand Ouji Mile

question: Give positive integer\(a,b\leq V\), Request about about\((x,y)\)Equation

\[ax+by=1 \]

Any integer solution (that is, satisfaction\(x,y\in\mathbb{Z}\)), Ensure that there is an integer solution.


Explain:when\(a=b\)Yes, the equation has no integer solution, so you might as well set it\(a<b\)Essence Directly\(b\)WriteRemovingform

\[\begin{gathered} k=\lfloor\frac{b}{a}\rfloor\\ r=(b\bmod a)\\ b=ka+r \end{gathered} \]

Will\(b=ka+r\)Enter the equation and organize it

\[ry+a(x+ky)=1 \]

You can see it about\((y,x+ky)\)The equation, so, the original coefficient is\(\displaystyle\left[\begin{matrix}a\\ b\end{matrix}\right]\)The problem can be converted into coefficients\(\displaystyle\left[\begin{matrix}r\\ a\end{matrix}\right]\)Question. In this way, the algorithm is like.


pseudocode

  • function \(\text{solve}(a,b)\)
    • if \(a=0\) then
      • return \((0,1)\)
    • let \((x',y')= \text{solve}(b\bmod a,a)\)
    • return \(\displaystyle (y'-\lfloor\frac{b}{a}\rfloor\times x',x')\)

Complexity analysis: Like Ouji Lilin algorithm,\(O(\log V)\)


Grow: If you want to learn more about the algorithm in detail, seePei Shu theorem -oi wiki

The minimum value under the constraint

question: Give positive integer\(a,m\)\(a<m\)) And non -negative integer\(L\leq R<m\), RequestMinimum non -negative integer \(x\)Make

\[L\leq ax\bmod m\leq R \]

If there is no such way\(x\)Need to report without solution.


Explain:if\([L,R]\)In the middle\(a\)The multiples, which are satisfying\(\displaystyle a\times\lceil\frac{L}{a}\rceil\leq R\)hour,\(x\)Obviously\(\displaystyle\lceil\frac{L}{a}\rceil\)

In other cases, you may wish to transform the problem and add another variable\(k\), The question becomes: findMinimum non -negative integer \(k\)MakeThere are non -negative integer \(x\),satisfy

\[L\leq ax-km\leq R \]

Here\(k\)Equivalent to\(\displaystyle\lfloor\frac{ax}{m}\rfloor\)So\(k\)The minimum,\(x\)Can get the smallest\(\displaystyle \lceil\frac{L+km}{a}\rceil\)

Deformation can be obtained,exist \(x\)Satisfy the above -mentioned unqualified style, should be

\[(-R)\bmod a\leq mk\bmod a\leq (-L)\bmod a \]

in\(mk\bmod a=(m\bmod a)k\bmod a\)

So the algorithm is like to say.


pseudocode

  • function \(\text{solve}(a,m,L,R)\)
    • if \(L=0\) then
      • return \(0\)
    • if \(a=0\) or \(R<L\) then
      • returnNo solution
    • if \(\displaystyle a\times\lceil\frac{L}{a}\rceil\leq R\) then
      • return \(\displaystyle\lceil\frac{L}{a}\rceil\)
    • let \(k=\text{solve}(m\bmod a,a,(-R)\bmod a,(-L)\bmod a)\)
    • if \(k=\)No solution then
      • returnNo solution
    • return \(\displaystyle\lceil\frac{L+km}{a}\rceil\)

Time complexity: The same is\(O(\log V)\)


help: You may need this formula\(\displaystyle\lceil\frac{x}{y}\rceil=\lfloor\frac{x+y-1}{y}\rfloor\)(when\(x,y\in \mathbb{Z},y>0\)Established).

Catey

question: Given non -negative integer\(n,a,b,c\leq V\),satisfy\(c\neq 0\),beg

\[\sum_{i=0}^n\left\lfloor\frac{ai+b}{c}\right\rfloor \]


solve: May wish to set the answer to\(f(a,n,b,c)\), Then\(a=0\)There, there is

\[f(a,n,b,c)=\lfloor\frac{b}{c}\rfloor\cdot (n+1) \]

when\(a\geq c\)or\(b\geq c\)There, there is

\[\begin{gathered} f(a,b,c,n)=\sum_{i=0}^n\left\lfloor\frac{ai+b}{c}\right\rfloor\\ =\sum_{i=0}^n\left\lfloor\frac{(a\bmod c)i+(b\bmod c)}{c}\right\rfloor+\lfloor\frac{a}{c}\rfloor \cdot\frac{n(n+1)}{2}+\lfloor\frac{b}{c}\rfloor\cdot (n+1)\\ =f(a\bmod c,b\bmod c,c,n)+\lfloor\frac{a}{c}\rfloor \cdot \frac{n(n+1)}{2}+\lfloor\frac{b}{c}\rfloor\cdot (n+1) \end{gathered} \]

when\(a<c\)and\(b<c\)At that time, yes (may wish to set\(\displaystyle m=\left\lfloor\frac{an+b}{c}\right\rfloor\)

\[\begin{gathered} f(a,b,c,n)=\sum_{i=0}^n\left\lfloor\frac{ai+b}{c}\right\rfloor\\ =\sum_{i=0}^n\sum_{j=0}^{m-1} [c(j+1)\leq ai+b]\\ =\sum_{j=0}^{m-1}\sum_{i=0}^n [ai> cj+c-b-1]\\ =\sum_{j=0}^{m-1}\left(n-\left\lfloor\frac{cj+c-b-1}{a}\right\rfloor\right)\\ =mn-\sum_{j=0}^{m-1}\left\lfloor\frac{cj+c-b-1}{a}\right\rfloor\\ =mn-f(c,c-b-1,a,m-1) \end{gathered} \]

Then there are practices.


pseudocode

  • function \(\text{solve}(a,b,c,n)\)
    • if \(n=0\) then
      • return \(\displaystyle \lfloor\frac{b}{c}\rfloor\)
    • if \(a\geq c\) or \(b\geq c\) then
      • return \(\displaystyle \text{solve}(a\bmod c,b\bmod c,c,n)+\lfloor\frac{a}{c}\rfloor \cdot \frac{n(n+1)}{2}+\lfloor\frac{b}{c}\rfloor\cdot (n+1)\)
    • let \(\displaystyle m=\left\lfloor\frac{an+b}{c}\right\rfloor\)
    • if \(m=0\) then [1]
      • return \(0\)
    • return \(mn-\text{solve}(c,c-b-1,a,m-1)\)

Complexity analysis:for\(O(\log V)\)

Universal

Agreed: In this section, part of the operation will be ingroup \(G\)Under the bottom, allMultiplication operationMay be seen\(G\)Group multiplication.


question: YesGroup \(e,r,s\in G\),in\(e\)forUnit, Given function (of which\(a,b,c\in\mathbb{N},c\neq 0\)

\[\xi(x)=\frac{ax+b}{c} \]

function\(f: \mathbb{N}\to G\)See the pseudo code below. Given non -negative integer\(n\leq V\), Request\(f(n)\)Value.

In the pseudo code below,\(\epsilon\)forEndless positive number

  • function \(f(n)\)
    • let \(x=\epsilon\), \(\text{ans}=e\)
    • while \(x\leq n\) do
      • if \(\xi(x)\in\mathbb Z\) then
        • \(\text{ans}\gets \text{ans}\times r\)
      • if \(x\in\mathbb Z\) then
        • \(\text{ans}\gets \text{ans}\times s\)
      • \(x\gets x+\epsilon\)
    • return \(\text{ans}\)

Hypothetical\(G\)The multiplication method can be achieved in a single\(O(T)\)The complexity.


Explain:when\(n=0\)At the same time, ordinary,\(f(0)=e\)

May wish to set the answer to\(g(a,b,c,n,r,s)\)

Easy to find\(\xi\)The straight lines represented by the downward movement have no effect, that is,

\[g(a,b,c,n,r,s)=g(a,b\bmod c,c,n,r,s) \]

Here it may be designed\(b<c\)

when\(a\geq c\)hour,Try to let one side model and the other sideEssence according to\(f\) Differential periodicity,have:

\[g(a,b,c,n,r,s)=g(a\bmod c,b,c,n,r,r^{\lfloor\frac{a}{c}\rfloor} \times s) \]

Be available\(f\)ofGeometryUnderstand the style. Can\(f\)See if it is a straight line in the plane of the Ou Shi, whether it is multiplied\(r\)or\(s\)It depends on whether the vertical coordinates or horizontal coordinates on a straight line are an integer. Discover each time\(\text{ans}\)Multiply\(s\)You have to take at least\(\displaystyle \lfloor\frac{a}{c}\rfloor\)indivual\(r\)

when\(a<c\)hour,Try to exchange them

Consider directly\(\text{ans}\)WriteSeek accumulationForm, there is

\[\text{ans}=\prod_{i=1}^n r^{\lfloor\xi(i)\rfloor-\lfloor\xi(i-1)\rfloor}\times s \]

To facilitate research, useStringExpress the groupThe group multiplication is expressed asString stitching,set up\(e\)forEmpty string\(r=\texttt{r}\)\(s=\texttt{s}\),but\(\text{ans}\)As a reason\(\texttt{r}\)and\(\texttt{s}\)String of constituent.

Obviously,\(\text{ans}\)Zhong, the first\(i\)indivual\(\texttt{s}\)Before\(\lfloor\xi(i)\rfloor\)indivual\(\texttt{r}\)Essence In order to achieve the exchange effect, we might as well calculate\(\text{ans}\)Zhong, the first\(j\)indivual\(\texttt{r}\)There are a few in front\(\texttt{s}\)Essence It is easy to list the formula, the answer is in the unlimited aspect, integer\(t\)The maximum value.

\[\left\lfloor\frac{at+b}{c}\right\rfloor<j \]

It is

\[\frac{at+b}{c}<j \]

Wait for the style\(t\)ofMaximumfor

\[\left\lfloor\frac{c(j-1)+c-b-1}{a}\right\rfloor \]

May wish to set a function\(\displaystyle \xi'(x)=\frac{cx+c-b-1}{a}\)

then\(\text{ans}\)Can be expressed (may be set\(m=\lfloor\xi(n)\rfloor\)

\[\begin{gathered} \text{ans}=s^{\lfloor\xi'(1-1)\rfloor}\times r\times\left(\prod_{j=2}^{m} s^{\lfloor\xi'(j-1)\rfloor-\lfloor\xi'((j-1)-1)\rfloor} \times r\right) \times s^{n-\lfloor\xi'(m-1)\rfloor}\\ =s^{\lfloor\xi'(0)\rfloor}\times r\times \left(\prod_{j=1}^{m-1}s^{\lfloor\xi'(j)\rfloor-\lfloor\xi'(j-1)\rfloor} \times r\right)\times s^{n-\lfloor\xi'(m-1)\rfloor}\\ =s^{\left\lfloor\frac{c-b-1}{a}\right\rfloor}\times r\times g(c,c-b-1,a,m-1,s,r)\times s^{n-\lfloor\xi'(m-1)\rfloor}\\ =s^{\left\lfloor\frac{c-b-1}{a}\right\rfloor}\times r\times g(c,(c-b-1)\bmod a,a,m-1,s,r)\times s^{n-\left\lfloor\frac{cm-b-1}{a}\right\rfloor}\\ \end{gathered} \]

Then there are practices.


pseudocode

  • function \(\text{solve}(a,b,c,n,r,s)\)
    • if \(n=0\) then
      • return \(e\)
    • if \(a\geq c\) or \(b\geq c\) then
      • return \(\text{solve}(a\bmod c,b\bmod c,c,n,r,r^{\lfloor\frac{a}{c}\rfloor}\times s)\)
    • let \(\displaystyle m=\left\lfloor\frac{an+b}{c}\right\rfloor\)
    • if \(m=0\) then
      • return \(s^n\)
    • return \(s^{\left\lfloor\frac{c-b-1}{a}\right\rfloor}\times r\times g(c,(c-b-1)\bmod a,a,m-1,s,r)\times s^{n-\left\lfloor\frac{cm-b-1}{a}\right\rfloor}\)

Complexity analysis: If the second power of the group can\(O(T)\)Calculate, then time complexity is\(O(T\log V)\)of. If not, then use itFast Power -OI WikiThe algorithm is single\(O(T\log V)\), So the time complexity has the upper bound\(O(T\log^2 V)\)

In fact, there is a tighter upper bound, and the time complexity is set.\(\mathcal{T}(a,c)\)There are

\[\mathcal{T}(a,c)=O(T\log \frac{a}{c})+\mathcal{T}(c\bmod a,a) \]

Consider\(\displaystyle O(T\log \frac{a}{c})=O(T\log a)-O(T\log c)\)So, it will\(\mathcal{T}(a,c)\)After unfolding directly, contain\(\log\)The items of the items will only disappear\(O(1)\)One, so the complexity is

\[\boxed{O(T\log V)} \]


End with flowers.


  1. In fact, this conditional judgment can be removed, because\(n<0\)This algorithm will not be wrong.↩︎