2024 ICPC Asia Shenyang Regional
2024 ICPC Asia Shenyang Regional
[B] Magical Palette
You are given a magical palette with a grid of \(m\) rows and \(n\) columns. There are \(m\cdot n\) kinds of selectable pigments, represented by integers \(\mathbb{N}\cap[0,m\cdot n)\). You need to assign a pigment to each row, named \(\vec{a}=(a_i)_{i=0}^{m-1}\), and one to each column, named \(\vec{b}=(b_i)_{i=0}^{n-1}\). Afterwards, for cell of the \(i\)-th row and the \(j\)-th column, you'll mix a color \(c_{i,j}=a_i\cdot b_j\bmod{m\cdot n}\). Find out whether you can make each of the \(m\cdot n\) cells contain a different color. If so, give a possible selection of \(\vec{a}\) and \(\vec{b}\).
There are \(T\in\mathbb{N}\cap[1,10^4]\) testcases. It is guaranteed that \(m,n\in\mathbb{N}\cap[1,10^6]\) and the sum of \(m\cdot n\) over all testcases does not exceed \(10^6\).
不妨设\(m\leq n\)。
必要条件
不妨设\(a_0\cdot b_0\equiv0\pmod{m\cdot n}\),则存在\(u,v\in\mathbb{N}^*\)使得\(u\cdot v=m\cdot n,\ u\mid a_0,\ v\mid b_0\)。由于第\(0\)行的数均为\(a_0\)的倍数,故\(\forall j\in\mathbb{N}\cap[0,n)\ (u\mid c_{0,j})\),由\(a_0\)得到的\(c_{0,j}=u_0\cdot\text{some integer}\bmod{(m\cdot n)}\)最多有\(\frac{m\cdot n}{u}\)个互异值(证明见下)。另一方面,列数\(n\)又要求\(c_{0,j}\)要有\(n\)个互异值,所以\(\frac{m\cdot n}{u}\geq n\iff u\leq m\),同理\(v\leq n\)。然而实际上\(u\cdot v=m\cdot n\),故\(u=m,\ v=n\),也就是\(m\mid a_0,\ n\mid b_0\)。
证明:通过任取\(s\in\mathbb{N}^*\),一共能得到最多\(\frac{m\cdot n}{u}\)个互异的\(a_0\cdot s\bmod{(m\cdot n)}\)。
设\(a_0=u\cdot t\ (t\in\mathbb{N^*})\),则任取\(s\in\mathbb{N}^*\),
\[a_0\cdot s\bmod{(m\cdot n)}=u\cdot s\cdot t\bmod{(m\cdot n)}\triangleq r \]必为\(u\)的倍数,因为对于任何\(x\in\mathbb{Z}\)满足\(x\bmod{(m\cdot n)}=r\),存在\(q\in\mathbb{Z}\)使得
\[x=q\cdot m\cdot n+u\cdot s\cdot t=q\cdot u\cdot v+u\cdot s\cdot t=(q\cdot v+s\cdot t)u, \]因此\(x\)必为\(u\)的倍数,包括\(r\)在内。
\(r\in\mathbb{N}\cap[0,m\cdot n)\),这个范围里有多少个数是\(u\)的倍数呢?那就是\(\frac{m\cdot n}{u}\)个。Q.E.D.
知道\(m\mid a_0,\ n\mid b_0\)后,由上面证明的结论知道,第一行最多能得到\(n\)个值,分别是\(\{m\cdot s:s\in\mathbb{N},\ s<n\}\);同理第一列最多能得到\(m\)个值,分别是\(\{n\cdot t:t\in\mathbb{N},\ t<m\}\)。要使这些值最多有一个重合的(即\(a_0\cdot b_0\bmod{(m\cdot n)}\)),必须使
即\(m,n\)互质。
充分条件
下面证明\(m,n\)互质时必有解。只需构造出一组解即可。
考虑线性构造,即
要求对于任意\(i_0,i_1,j_0,j_1\in\mathbb{N},\ i_0<i_1<m,\ j_0<j_1<n\),都有
展开合并化简得
可以取
于是

浙公网安备 33010602011771号