Some algorithms (routines)

Source: Internet
Author: User
Tags gcd

Probability/Expectation DP

There are some probabilities/expectations that DP can quickly introduce such a formula:
\[\begin{align}f_i&=a+bf_i\ (1-b) f_i&=a\f_i&=\frac{a}{1-b}\end{align}\]
BZOJ4872

XSY2472

Divide and conquer

There are a few questions that are only included/not included in the case, just consider the current \ ([l,r]\) to \ ([ l,mid]\) and \ ([mid+1,r]\) effects.

Here's an example

  \ (A (x) \) is \ (n-1\) a polynomial,\ (b_i (x) \) is a polynomial,\ (\forall i\) ( A (x) \mod b_i (x) \)

The direct do is \ (O (n^2) \) .

Because \ ((A (x) \mod C (x)) \mod b_i (x) =a (x) \mod b_i (x) \)(\ (C (x) \mod b_i (x) =0\))

The current set of
\[d_{l,r}=a (x) \mod (\prod_{i=l}^rb_i (x)) \]
So
\[\begin{align}d_{l,mid}&=d_{l,r}\mod (\prod_{i=l}^{mid}b_i (x)) \d_{mid+1,r}&=d_{l,r}\mod (\prod_{i=mid+1 }^{r}b_i (x)) \end{align}\]
So we can do it recursively until we find out all the \ (d_{i,i}\)

Complexity of Time:
\[t (n) =2t (\frac{n}{2}) +o (Nlogn) =o (n{log}^2n) \]
Multi-point evaluation

XSY2469

Euler phi function

is the \ (\phi\) function

Everyone knows that this thing is a integrable function.
\[\phi (AB) =\phi (a) \phi (b) ~ ~ ~ ((a) =1) \]
What if \ ((b) \neq 1\) ?

Set\ (d= (a, b) \)
\[\begin{align}\phi (a) &=a (1-\frac{1}{p1_1}) (1-\frac{1}{p1_2}) \cdots (1-\frac{1}{p1_{n1}) \\phi (b) &= B (1-\frac{1}{p2_1}) (1-\frac{1}{p2_2}) \cdots (1-\frac{1}{p2_{n2}) \\phi (AB) &=ab (1-\frac{1}{p3_1}) (1-\frac{1} {p3_2}) \cdots (1-\frac{1}{p3_{n3}) \\phi (d) &=d (1-\frac{1}{p4_1}) (1-\frac{1}{p4_2}) \cdots (1-\frac{1}{p4_{n4}) \end{ Align}\]
Can be found, for the latter part
\[\begin{align} (1-\frac{1}{p1_1}) (1-\frac{1}{p1_2}) \cdots (1-\frac{1}{p1_{n1}) \times (1-\frac{1}{p2_1}) (1-\ Frac{1}{p2_2}) \cdots (1-\frac{1}{p2_{n2}) \= (1-\frac{1}{p3_1}) (1-\frac{1}{p3_2}) \cdots (1-\frac{1}{p3_{n3}}) \ Times (1-\frac{1}{p4_1}) (1-\frac{1}{p4_2}) \cdots (1-\frac{1}{p4_{n4}}) \end{align}\]
If\ (p\)Only in\ (a\)Or\ (b\)appear in the\ (ab\)appear in the. If at the same time\ (a\)And\ (b\)In the same time, it will\ (ab\)And\ (d\)appear in the.

So there are
\[\phi (AB) =\frac{\phi (a) \phi (b) D}{\phi (d)}~~~ (d= (b)) \]

Reverse thinking situation One

Sometimes we do something very difficult to do, we can start all the operation after the reply in one.

For example: Given a diagram, there are two actions: 1. Delete Edge; 2. Ask for connectivity.

We can first delete the need to delete the side, and then add it back, using and check set maintenance connectivity.

Situation Two

Sometimes ask you \ (\forall a\), meet the requirements of \ (b\) and.

We can enumerate all the \ (b\)and calculate each \ ( b\) contribution to each \ (a\) .

agc005f
 

A kind of whole order problem

Have \ (n\) items, you have to select these items, each item has three properties \ (a_i,b_i,c_i\), when you select an item, set the current selected item's \ (c\) attribute and the \ (s\ ), then choose the benefit of this item is \ (a_i+b_is\), ask you the maximum profit is how much.

Let's say we've been in a sequence. Consider two adjacent items (which may be set to the top two), and when the current order is better than the one after the exchange:
\[\begin{align}a_1+a_2+b_2c_1&>a_2+a_1+b_1c_2\b_2c_1&>b_1c_2\\frac{c_1}{b_1}&>\frac{c_2}{ B_2}\end{align}\]
So we get a full order relationship.

So can you expand to any of the two things?
\[\begin{align}a_i+b_ix+a_j+b_j (x+y+c_i) &>a_j+b_jx+a_i+b_i (X+y+c_j) \b_jy+b_jc_i&>b_iy+b_ic_j\\ Frac{c_i+y}{b_i}&>\frac{c_j+y}{b_j}\\end{align}\]
It doesn't seem like a good line. We need another way of thinking.

Suppose we find an optimal solution, but do not satisfy the above properties, then we can exchange two adjacent items to make the answer optimal. So the direct ordering greedy can get the optimal solution.

If there are other restrictions on the topic, you can also get DP or do other things after getting this order.

MO Team

It is well known that the time complexity of MO team is related to the size of the block.

If the block size is \ (t\), the time complexity is \ (O (\FRAC{N^2}{T}+MT) \)

If the block size is \ (\sqrt n\), the time complexity is \ (O ((n+m) \sqrt n) \)

If the block size is \ (\frac{n}{\sqrt{m}}\), the time complexity is \ (O (n\sqrt m+m\log m) \)

So sometimes you can speed up by resizing the block. Commonly known as the assistant.

A class of single-point modification interval summation problem

Sometimes we have to modify a point to find the interval and.
  
| algorithm | modify | sum |
|:----:|:----:|:----:|
| tree array/Line Tree | \ (O (log n) \)| \ (O (LOGN) \)|
| chunking 1| \ (O (1) \)| \ (O (\sqrt n) \)|
| chunking 2| \ (O (\sqrt n) \)| \ (O (1) \)|

The tree-like array/line-segment tree is a classic practice and is not spoken here.

Chunked 1: Each modification puts the corresponding position and the corresponding block of the and \ (+1\)

Block 2: Each time you modify the corresponding position and the corresponding block's and \ (+1\), then find the intra-block prefix and, within the block suffix and, the block prefix and.

Some people are going to ask, what is the use of block practice so slow?

It's very useful! When there is an imbalance between the number of changes and the number of queries, we can do better than a tree-like array.

Bo Master once with MO Team + sub-block 1 water over a \ (n=m={10}^6\) problem. Run faster than ZJT God Ben's line tree.

Issues related to alignment

A lot of questions make you beg for each permutation \ (a\), if \ (\cdots\), then \ (\cdots\).

We can consider the contribution of this number when inserting the number of n\ ( i\) from small to large.

Implement all numbers with Trie\ (+1\), querying the entire number of XOR and

We built a trie tree from low to high.

Starting from the root, swap the left and right subtrees, and then perform the same action (carry) on the subtrees Tree of \ (0\) .

Multiple sets of queries for Möbius inversion

\[\BEGIN{ALIGN}ANS&=\SUM_{I=1}^LC^{\GCD (i,b)}\&=\sum_{d|s}c^d\sum_{i=1}^l[\gcd (i,s) =d]\&=\sum_{d|s }C^D\SUM_{I=1}^{\FRAC{L}{D}}[\GCD (I,\frac{s}{d}) =1]\&=\sum_{d|s}c^d\sum_{i|\frac{s}{d}}\mu (i) \lfloor\frac{ L}{id}\rfloor\\end{align}\]

There seems to be no way to simplify.

This time to enumerate \ (j=id\)

\[\BEGIN{ALIGN}ANS&=\SUM_{J|S}\LFLOOR\FRAC{L}{J}\RFLOOR\SUM_{I|J}\MU (i) c^\frac{j}{i}\\end{align}\]

Set \ (f (x) =\sum_{i|x}\mu (i) c^\frac{x}{i}\)

So \ (f (x) \) can be preprocessed.
  

General situation

\[\BEGIN{ALIGN}ANS&=\SUM_{I=1}^N\SUM_{J=1}^MF (\GCD (i,j)) \&=\sum_{i=1}^{\min (n,m)}f (i) \sum_{i|j}\mu (\ Frac{j}{i}) \lfloor\frac{n}{j}\rfloor\lfloor\frac{m}{j}\rfloor\&=\sum_{i=1}^{\min (n,m)}\lfloor\frac{n}{i}\ Rfloor\lfloor\frac{m}{i}\rfloor\sum_{j|i}f (j) \mu (\frac{i}{j}) \end{align}\]

This allows preprocessing of the following \ (g (n) =\sum_{i|n}\mu (\frac{n}{i}) f (i) \)
Ask before each enumeration.
Time complexity:\ (O (n+t\sqrt{n}) \)  

Divide and conquer FFT

The divided FFT generally has two uses.

The product of many polynomials (general division)

With a \ (n\) polynomial, the sum of the Times is \ (m\), then the time complexity is \ (O (m\log m\log n) \). There are altogether \ (\log n\) layers, each layer is \ (O (m\log m) \) .

To find a class of series (CDQ)

Series \ (f_n=\sum_{i=0}^{n-1}f_ig_{n-i}\). For a divide-and-conquer interval \ ([l,r]\), the answer to \ ([l,mid]\] is first calculated, and the contribution of this part to the right \ ([mid+1,r]\) is computed.

Time complexity:\ (O (n\log^2 n) \)
  

Matrix Fast Power +fft

The DP transfer is as follows:

\[f_{i+1,j ', k+v}+=f_{i,j,k}\]

  \ (I\leq n,j\leq l,k\leq m\)
where \ (v\) is only related to \ (j\) , and finally the value of \ (k=s\) or \ (k\mod m=s\) is obtained.
The time complexity of the violence is \ (O (l^3m^3\log n) \) .
We can think of this thing as a polynomial.

\[g_{i,j}=\sum_{k\geq 0}f_{i,j,k}x^k\]
  
Transfer can be seen as multiplying by a polynomial (single-entry).
If you ask for the value of \ (K\mod m=s\) , you can consider a cyclic convolution.
Can be evaluated first, take each point value to run over the matrix fast power, and then interpolation back.
Time complexity:\ (O (ml^3\log n) +\) time complexity for point value interpolation \ (o (m^2)/O (m\log m) \)

Matrix Fast Power optimization for multiple groups of queries DP

Set the matrix size to \ (m\), the number of times is \ (n\), ask the number of groups is \ (t\), the naïve implementation is \ (O (tm^3\log n) \) .
You can first find the \ (i\) power of the transfer matrix.
Each query only needs to take a \ (1\times m\) matrix to multiply the transfer matrix on the line. Each multiplication is \ (O (m^2) \) .
Time complexity:\ (O (m^3+tm^2\log n) \)

Some algorithms (routines)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.