CF185D.Visit of the Great

普及/提高-

通过率:0%

AC君温馨提醒

该题目为【codeforces】题库的题目,您提交的代码将被提交至codeforces进行远程评测,并由ACGO抓取测评结果后进行展示。由于远程测评的测评机由其他平台提供,我们无法保证该服务的稳定性,若提交后无反应,请等待一段时间后再进行重试。

题目描述

The Great Mushroom King descended to the dwarves, but not everyone managed to see him. Only the few chosen ones could see the King.

We know that only LCM(k2l+1,k2l+1+1,...,k2r+1)LCM(k^{2^l}+1,k^{2^{l+1}}+1,...,k^{2^r}+1) dwarves can see the Great Mushroom King. Numbers kk , ll , rr are chosen by the Great Mushroom King himself in some complicated manner which is unclear to common dwarves.

The dwarven historians decided to document all visits of the Great Mushroom King. For each visit the dwarven historians know three integers kik_{i} , lil_{i} , rir_{i} , chosen by the Great Mushroom King for this visit. They also know a prime number pip_{i} . Help them to count the remainder of dividing the number of dwarves who can see the King, by number pip_{i} , for each visit.

输入格式

The first line contains the single integer tt (1<=t<=105)(1<=t<=10^{5}) — the number of the King's visits.

Each of the following tt input lines contains four space-separated integers kik_{i} , lil_{i} , rir_{i} and pip_{i} (1<=ki<=106(1<=k_{i}<=10^{6} ; 0<=li<=ri<=1018; 2<=pi<=109)0<=l_{i}<=r_{i}<=10^{18}; 2<=p_{i}<=10^{9}) — the numbers, chosen by the Great Mushroom King and the prime module, correspondingly.

It is guaranteed that for all visits number pip_{i} is prime.

Please do not use the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.

输出格式

For each visit print the answer on a single line — the remainder of dividing the number of the dwarves who can see the King this time, by number pip_{i} . Print the answers for the visits in the order, in which the visits are described in the input.

输入输出样例

  • 输入#1

    2
    3 1 10 2
    5 0 4 3
    

    输出#1

    0
    0
    

说明/提示

We consider that LCM(a1,a2,...,an)LCM(a_{1},a_{2},...,a_{n}) represents the least common multiple of numbers a1a_{1} , a2a_{2} , ...... , ana_{n} .

We consider that x0=1x^{0}=1 , for any xx .

首页