Under construction

Still being built. Things may change

Bad-hash students

Time limit5sMemory limit512 MB

Summary
Given n, k1, and alpha, count how many distinct cells the broken probe sequence k_i = k1 + alpha*k_{i-1}^2 mod n visits before it repeats.
Level

Hard8 of 10

Topics
Math, Number theory, Simulation, Implementation
Solved
No attempts yet

Problem

A hash table is a standard data structure in computer science that lets you store distinct items in a relatively small array. Hash tables have several implementations, and several ways to handle overflows and collisions. One of them is called hashing with open addressing and quadratic probe.

It works like this. For some fixed integer constant α\alpha, suppose our hash table TT has length nn and consists of the cells T[0]T[0], T[1]T[1], T[2]T[2], ..., T[n−1]T[n-1]. To store an object, we first apply an initial hash function to compute an initial key k1k_1, which is simply an index between 0 and n−1n-1. We try to store the object at position k1k_1 in TT. If T[k1]T[k_1] is already used, we try another position at index k2=k1+α⋅12mod  nk_2 = k_1+\alpha\cdot 1^2 \mod n. If T[k2]T[k_2] is also already used, we continue to probe the hash table at positions ki=k1+α⋅(i−1)2mod  n for i=3,4,5,…,k_i = k_1+\alpha\cdot (i-1)^2 \mod n \text{ for } i= 3,4,5, \dots, and so on until we find the first empty position in the array TT. If we cannot find an empty cell of TT after nn probes, we give up and raise an exception. This way of storing objects is supposed to work well when the hash function that computes the initial key k1k_1 uniformly distributes its values over the integer interval [ ⁣[0,n−1] ⁣][\![ 0,n-1]\!] and when the number mm of objects to be stored is a little smaller than nn.

The sophomore students of Professor Amaretto's class have been asked to implement hash tables. Not one student got it right, a horrendous situation that would never have happened in Professor Mayor's freshman class. It took a long time for Professor Amaretto to understand what his students had coded. Their first mistake was that their complicated choice of initial hash function was buggy and in fact always returns the same value k1k_1. Their second mistake was that they all coded the formula ki=k1+α⋅ki−12mod  nk_i = k_1+\alpha\cdot {k_{i-1}}^2 \mod n instead of the correct formula.

Still, Professor Amaretto is curious to know how usable these broken hash tables are, implemented incorrectly by his bad-hash students.

Input

The input file consists of multiple test cases. The first line of the input file consists of a single integer cc indicating the number of test cases. Each test case follows, and consists of a single line with 3 integers nn, k1k_1, α\alpha separated by single spaces, where nn (2≤n≤2000000002\le n \le 200000000) is the size of the hash table, k1k_1 (0≤k1≤n−10\le k_1 \le n-1) is the constant return value of the wrongly coded initial hash function, and α\alpha (1≤α≤n−11\le \alpha \le n-1 and α≤100\alpha \le 100) is the coefficient used for the buggy quadratic probing.

Output

For each test case in the input, your program should produce one line consisting of one integer that indicates the number of cells of the hash table that can be assigned.

Examples1

  1. Example 1

    Input
    2
    3 0 1
    7 3 3
    
    Expected output
    1
    4