Under construction

Still being built. Things may change

Algebra on Segment

Time limit10sMemory limit512 MB

Summary
Given an array modulo a prime p, support range multiplication and report the order of the subgroup generated by the elements of a segment.
Level

Hard8 of 10

Topics
Segment tree, Number theory, Math
Solved
No attempts yet

Problem

Consider a prime number pp.

All operations below are performed in the multiplicative group modulo pp.

You are given an array of size nn. Process qq queries of two types:

  • "1 ll rr xx": multiply all elements of the segment [l;r][l; r] by xx.
  • "2 ll rr": find the order of the subgroup generated by the elements of the segment [l;r][l; r].

See the notes below for the definitions.

Input

The first line contains three integers pp, nn, qq: the prime number, the size of the array, and the number of queries (2≤p<1092 \le p < 10^{9}, pp is prime, 1≤n≤1051 \le n \le 10^{5}, 1≤q≤1051 \le q \le 10^{5}).

The second line contains the initial array aa of size nn (1≤ai<p1 \le a_i < p).

Each of the next qq lines is a query. A query of the first type has the form "1 ll rr xx" (1≤l≤r≤n1 \le l \le r \le n, 1≤x<p1 \le x < p) and multiplies all elements of [l;r][l; r] by xx. A query of the second type has the form "2 ll rr" (1≤l≤r≤n1 \le l \le r \le n) and asks for the order of the subgroup generated by the elements of [l;r][l; r].

Output

For each query of the second type, print the answer on a separate line.

Notes

This section defines the group theory terms used in the statement. All definitions are standard, so readers who know the basics can skip it.

A group is a set with an associative binary operation. The group is closed under the operation, has an identity element (1 for multiplication), and every element has an inverse.

The multiplicative group modulo a prime pp has elements 1,2,…,p−11, 2, \ldots, p-1, and the operation is multiplication modulo pp. It is easy to check that this is a group.

A subgroup is a subset HH of a group GG that is itself a group under the operation of GG. In particular, it is closed under the operation.

The subgroup generated by a set S⊆GS \subseteq G is ⟨S⟩\langle S \rangle, the smallest subgroup of GG that contains SS. This subgroup is unique.

The order of a group is the number of elements in it.

Examples1

  1. Example 1

    Input
    17 3 7
    10 16 2
    2 2 3
    2 1 2
    2 2 2
    1 1 2 3
    2 1 1
    2 3 3
    2 1 3
    
    Expected output
    8
    16
    2
    4
    8
    16