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)\)
- if \(x=0\) then
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.
- 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.
- 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
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
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
Will\(b=ka+r\)Enter the equation and organize it
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')\)
- if \(a=0\) then
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
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
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
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\)
- if \(L=0\) then
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
solve: May wish to set the answer to\(f(a,n,b,c)\), Then\(a=0\)There, there is
when\(a\geq c\)or\(b\geq c\)There, there is
when\(a<c\)and\(b<c\)At that time, yes (may wish to set\(\displaystyle m=\left\lfloor\frac{an+b}{c}\right\rfloor\))
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)\)
- if \(n=0\) then
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\))
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\)
- if \(\xi(x)\in\mathbb Z\) then
- 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,
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:
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
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.
It is
Wait for the style\(t\)ofMaximumfor
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\))
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}\)
- if \(n=0\) then
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
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
End with flowers.
-
In fact, this conditional judgment can be removed, because\(n<0\)This algorithm will not be wrong.↩︎