Ultraviolet A 11024-circular lock
Question Link
Given a matrix, you can add 1 to a row or column each time. Can you create a matrix where % P equals 0, the method obtained by P is the GCD of all numbers in matrix p.
Idea: Push the formula. There are four value-added Methods: A, B, C, and D.
A + C is added to position a + K1 P, and A is a multiple of P in the original position difference.
Likewise
Add a + D to position a, and then add B + K2 p.
Add B + C to position a C + K3 P
Add B + D to position a. d + K4 P
In this case, a-B-c + D + (K1-K2-K3 + K4) * p = 0 is obtained;
Therefore, if a-B-c + D is a multiple of P
Code:
#include <cstdio>#include <cstring>int t, s[2][2], p[2][2];int gcd(int a, int b) {return b == 0 ? a : gcd(b, a % b);}int main() {scanf("%d", &t);while (t--) {for (int k = 0; k < 2; k++) {for (int i = 0; i < 2; i++)scanf("%d", &s[k][i]);for (int i = 0; i < 2; i++)scanf("%d", &p[k][i]);}int P = p[0][0];for (int i = 0; i < 2; i++)for (int j = 0; j < 2; j++)P = gcd(P, p[i][j]);int sum = 0;for (int i = 0; i < 2; i++) {for (int j = 0; j < 2; j++) {if (i == j) sum -= P - s[i][j];else sum += P - s[i][j];}}printf("%s\n", sum % P == 0 ? "Yes" : "No");}return 0;}
Ultraviolet A 11024-circular lock (number theory + reasoning)