7028.Counting LCM Arrays

Time Limit: 1s Memory Limit: 512MB

Given two integers $n$ and $k$ , your task is to count the number of arrays $a_1, a_2,\dots, a_n$ of positive integers where $\operatorname{lcm}(a_i, a_{i+1}) = k$ for all $1 \le i < n$ .

Input Format(From the terminal/stdin)

The first line has an integer $t$ : the number of test cases.

The next $t$ lines have two integers $n$ and $k$ : the length of the array and the value of lcm.

  • $1 \le t \le 1000$
  • $2 \le n \le 10^9$
  • $1 \le k \le 10^9$

Output Format(To the terminal/stdout)

Print $t$ integers: the answer to each test case modulo $10^9 + 7$ .

Sample Input

Copy
3
3 4
4 6
1337 42
 \n
 · \n
 · \n
    ·  \n

Sample Output

Copy
11
64
602746233
  \n
  \n
         \n

The arrays for the first test case are $[1, 4, 1]$ , $[1, 4, 2]$ , $[1, 4, 4]$ , $[2, 4, 1]$ , $[2, 4, 2]$ , $[2, 4, 4]$ , $[4, 1, 4]$ , $[4, 2, 4]$ , $[4, 4, 1]$ , $[4, 4, 2]$ and $[4, 4, 4]$ .

Source: CSES, Additional Problems I, 3169

Submit

请先 登录

© 2025 FAQs