luogu#P8322. 『JROI-4』少女幻葬
『JROI-4』少女幻葬
Background
Original background of this problem
"Girl's Necro-Fantasy" is the theme song of Yakumo Ran. It is also the battle music for the boss of the extra stage in Touhou Youyoumu.
It is one of the most classic pieces among the music using the "ZUN trumpet" (ZUN 号). The piano seems to depict a powerful and beautiful beast, and the bright ZUN trumpet, together with Ran's gorgeous bullet patterns, brings the theme of death to its peak.
Problem Description
Given a sequence of length , and the value range for the -th number . Now Ran wants to know how many sequences satisfy, for a given constant , that:
-
The greatest common divisor of any two adjacent numbers is not .
-
The greatest common divisor of any three adjacent numbers is exactly .
Since the answer may be very large, output its value .
Input Format
The first line contains two integers .
In the next lines, each line contains two integers, representing .
Output Format
One line: the answer modulo .
3 1
1 6
2 6
3 6
3
4 1
11 45
19 81
31 53
7 28
6295
Hint
[Sample Explanation]
For sample , the valid sequences are: .
[Constraints and Conventions]
- Subtask 1 (7 pts): , .
- Subtask 2 (23 pts): , .
- Subtask 3 (25 pts): , .
- Subtask 4 (45 pts): no special restrictions.
For of the data, , , .
Here, .
Translated by ChatGPT 5