Lang:G++
Edit12345678910111213141516171819202122232425262728293031#include <cstdio>#include <algorithm>using namespace std;const int MAXK = 1000005;int N, M, K, solution[MAXK];long long getSumLU(long long x, long long y) { // Get the sum of submatrix[1 .. x, 1 .. y]if (x > y) swap(x, y);return x * (x + 1) * ((x << 1) + 1) / 6 + ((y - x) * x * (x + 1) >> 1);}long long getSumRD(long long x, long long y) { // Get the sum of submatrix [x .. x + N - 1, y .. y + M - 1]. At least one of x and y should be 1if (1 == x) return getSumLU(N, y + M - 1) - getSumLU(N, y - 1);return getSumLU(x + N - 1, M) - getSumLU(x - 1, M);}struct Ans {int x, y;Ans(int x = MAXK, int y = MAXK): x(x), y(y) {}}ans;bool operator <(const Ans &A, const Ans &B) {return A.x + A.y < B.x + B.y || (A.x + A.y == B.x + B.y && A.x < B.x);}Ans solve(int x, int y) {int d = solution[(K - getSumRD(x, y) % K) % K];return d >= 0 ? Ans(x + d, y + d) : Ans();}