아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

The Hash Table

시간 제한1초메모리 제한512 MB

요약
i를 0부터 n-1까지 슬롯 i^2 mod m에 넣을 때 각 슬롯에 이미 있는 원소 수만큼 비용을 내고, 총비용을 구한다.
난이도

보통10점 중 6점

유형
수학, 정수론, 조합론, 구현
정답자
아직 제출이 없습니다

문제

There is a hash table with mm slots, numbered from 00 to m−1m-1. Initially the slots are empty.

There are nn elements, numbered from 00 to n−1n-1, which should be inserted into the hash table in this order.

The hash function is h(x)=x2 mod mh(x)=x^2 \bmod m, so the element number ii will be inserted into the slot numbered (i2 mod m)(i^2 \bmod m).

Because of the strange implementation, inserting an element into a slot costs TT, where TT is the number of elements this slot already contains. Please compute the total cost of inserting all these nn elements into the table.

입력

The first line contains an integer tt, denoting the number of test cases (1≤t≤51 \le t \le 5).

Each test case is given on a single line with two integers, nn and mm (1≤n≤1091 \le n \le 10^9, 2≤m≤1092 \le m \le 10^9).

출력

For each test case, print a single line containing the answer.

힌트

In the first test case, the elements 0,1,2,3,40,1,2,3,4 are inserted into slots 0,1,0,1,00,1,0,1,0 respectively, incurring costs of 0+0+1+1+2=40+0+1+1+2=4.

예제1

  1. 예제 1

    입력
    3
    5 4
    1234 5678
    5 4
    
    예상 출력
    4
    229
    4