#P12435. [NERC2023] Divisibility Test

[NERC2023] Divisibility Test

题目描述

Daisy has recently learned divisibility rules for integers and she is fascinated by them. One of the tests she learned is a divisibility test by 3. You can find a sum of all digits of a decimal number and check if the resulting sum is divisible by 3. Moreover, the resulting sum of digits is congruent modulo 3 to the original number --- the remainder modulo 3 is preserved. For example, 757+5(mod3)75 \equiv 7 + 5 \pmod 3. Daisy is specifically interested in such remainder preservin divisibility tests.

There are more examples like that that she learned for decimal integers (integers base 10):

  • To test divisibility modulo 11, find an alternating sum of digits. Counting digits from the last (least significant) digit, add digits on odd positions (the last, 3rd to the last, etc) and subtract digits on even positions (2nd to the last, 4th to the last, etc) to get the sum that is congruent modulo 11 to the original number. For example, 12312+3(mod11)123 \equiv 1 - 2 + 3 \pmod {11}.
  • To test divisibility modulo 4, keep the last two digits. Their value is congruent modulo 4 to the original number. For example, 87654343(mod4)876543 \equiv 43 \pmod 4.
  • To test divisibility modulo 7, find an alternating sum of groups of three digits. For example, 43893284389+328(mod7)4389328 \equiv 4 - 389 + 328 \pmod 7.

Similar tests can be found in other bases. For example, to test divisibility modulo 5 for octal numbers (base 8), find an alternating sum of groups of two digits. For example, 12348128+348(mod5)1234_8 \equiv -12_8 + 34_8 \pmod 5.

Daisy wants to find such rules for a given base bb. She is interested in three kinds of divisibility rules:

  • Kind 1 --- take the last kk digits of an integer in base bb.
  • Kind 2 --- take a sum of groups of kk digits of an integer in base bb.
  • Kind 3 --- take an alternating sum of groups of kk digits of an integer in base bb.

It is not always possible to find such a divisibility rule. For example, in base 10 there is no such test for divisibility modulo 6, even though different approaches to testing divisibility by 6 exist.

Given base bb and modulo nn, Daisy wants to know the smallest group size kk for which such a divisibility test exits.

输入格式

There are several tests in the input. The first line of the input contains an integer tt --- the number of tests. The next tt lines describe the tests.

Each test consists of a line with two integers bb and nn --- the base and the modulo (b,n2b, n \ge 2). The sum of all bb values in the input does not exceed 10610^6, and the sum of all nn values in the input does not exceed 10610^6.

输出格式

Write tt lines --- a line for each test in the input. On a line for a test write a single integer 00 if the divisibility test for a given bb and nn does not exist. Otherwise, write two integers aa and kk, where aa is the kind of the divisibility test (1, 2, or 3) and kk is the number of digits in a group for the test, such that kk is the lowest among all possible divisiblity tests for the given bb and nn.

6
10 3
10 11
10 4
10 7
8 5
10 6
2 1
3 1
1 2
3 3
3 2
0