Official

B - Sum is Multiple Editorial by kobae964


The problem’s condition is equivalent to there existing some positive integer \(a\) such that \(1+2+\cdots+k=aN\). This expression can be simplified into \(k(k+1)=2aN\).

If the expression above holds, there exists an integer \(x\) satisfying all of the following conditions:

  • \(x\) is a divisor of \(2N\).
  • \(x\) is a divisor of \(k\).
  • \(2N/x\) is a divisor of \(k+1\).

Let us fix this \(x\) and let \(y=2N/x\). Then the problem is reduced to finding the minimum positive integer \(k\) that satisfies \(k\equiv 0 \pmod x\) and \(k\equiv -1 \pmod y\). It can be solved by using CRT.

Finally, you can try all the divisors of \(2N\) as candidates of \(x\). It takes \(O(\sqrt N)\) time to enumerate the divisors, and for each divisor it takes \(O(\log N)\) time to find \(k\). In this problem’s constraints there are at most \(30720\) divisors of \(2N\), so this solution runs fast enough.

posted:
last update: