Inverse Knapsack

시간 제한3초메모리 제한256 MB

요약
큰 소수 p와 목표 x가 주어질 때, 1부터 5000까지의 서로 다른 정수를 최대 S개 골라 역수의 합이 x와 p에 대해 합동이 되도록 만든다.
난이도

어려움10점 중 9점

유형
정수론, 그리디, 수학, 완전 탐색
정답자
아직 제출이 없습니다

문제

For his number theory homework, Busy Beaver is given TT pairs of a large prime pp and an integer xx. For each pair, Busy Beaver needs to find a subset of 1,12,13,⋯ ,15000\\{1,\frac 12,\frac 13,\cdots,\frac 1{5000}\\} of size at most SS whose sum is equal to xx modulo pp. Can you help him find such subsets?

A rational number ab\frac ab is equal to xx modulo pp if a≡bx(modp)a \equiv bx \pmod p.

입력

The first line contains two integers TT and SS (1≤T≤10001 \le T \le 1000, 150≤S≤5000150 \le S \le 5000), indicating the number of testcases and the maximum size of the subset.

Each of the next TT lines contains two integers pp and xx (108≤p≤101810^8 \le p \le 10^{18}, 0≤x≤p−10 \le x \le p-1), where pp is prime.

출력

For each testcase, output one line indicating the answer. Start with some integer kk (0≤k≤S0 \le k \le S), indicating the size of the subset, and then follow with kk distinct integers a_1,…,a_ka\_1,\dots,a\_k in increasing order (1≤a_1<a_2<⋯<a_k≤50001 \le a\_1 < a\_2 < \dots < a\_k \le 5000).

Your output should satisfy 1a_1+1a_2+⋯+1a_k≡x(modp)\frac{1}{a\_1} + \frac{1}{a\_2} + \dots + \frac{1}{a\_k} \equiv x \pmod p.

It can be proven that for all pp, xx satisfying the input constraints, such a subset always exists.

힌트

In the first test case, the empty subset sums to x=0x = 0 modulo p=998244353p = 998244353.

In the second test case, 11≡1(mod1000000007)\frac{1}{1} \equiv 1 \pmod {1000000007}.

In the third test case, 12≡500000004(mod1000000007)\frac{1}{2} \equiv 500000004 \pmod {1000000007}.

In the fourth test case, 11+119+12025≡642833014(mod1000000007)\frac{1}{1} + \frac{1}{19} + \frac{1}{2025} \equiv 642833014 \pmod {1000000007}.

예제1

  1. 예제 1

    입력
    4 150
    998244353 0
    1000000007 1
    1000000007 500000004
    1000000007 642833014
    
    예상 출력
    0
    1 1
    1 2
    3 1 19 2025