MO team algorithm Mo ' s algorithm

Source: Internet
Author: User
Tags greatest common divisor

Recent brush algorithm problem encountered a problem:

Give a sequence of length n {a[1], a[2], a[3], ..., A[n]}, and M-group inquiry (L[i], r[i], k[i]).
The number of occurrences of the sequence subscript interval in [L[i], R[i]] coprime (Greatest common divisor 1) is in this interval. Input Description:

The first line, two positive integers n, m (1≤n, m≤50000). The second line, n positive integers a[i] (1≤a[i]≤n) describes the sequence. Next m line, three positive integers per line l[i], R[i], K[i] (1≤l[i]≤r[i]≤n, 1≤k[i]≤n), describes a query.
Output Description:
Output m line, that is, the answer to each query.


At first my idea was to just press test instructions to do it, but the final display time was too complicated to go through, because it was the complexity of O (MN).
So on Google the solution of the great God, so search out a great algorithm-mo team algorithm, listen to the name is very powerful look! Altogether is a Chinese invented = =. Then studied in detail a wave:








MO team algorithm Mo ' s algorithm

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.