首页 > 代码库 > uva 11024 - Circular Lock(数学)

uva 11024 - Circular Lock(数学)

题目链接:uva 11024 - Circular Lock

题目大意;有个2*2的矩阵,给定p,s,P为p数组中所有元素的最大公约数。s为2*2矩阵的初始状态,每次可以选择一行或是一列同时加1,最终使得sij%P=0

解题思路:gijaij还需要多少可以是P的倍数,判断g11?g12?g21+g22是P的倍数即可。

/********************
 * A + C = a + k1 * p
 * B + C = b + k2 * p
 * A + D = c + k3 * p
 * B + D = d + k4 * p
 *
 * a - b - c + d + (k1 - k2 - k3 + k4) * p
 *  = 0;
********************/



#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
const int maxn = 5;

int s[maxn][maxn], p[maxn][maxn];

inline int gcd (int a, int b) {
    return b == 0 ? a : gcd(b, a % b);
}

int main () {
    int cas;
    scanf("%d", &cas);
    while (cas--) {
        for (int i  = 1; i <= 2; i++)
            scanf("%d %d %d %d", &s[i][1], &s[i][2], &p[i][1], &p[i][2]);

        int P = p[1][1];
        for (int i = 1; i <= 2; i++)
            for (int j = 1; j <= 2; j++)
                P = gcd(P, p[i][j]);

        int sum = 0;
        for (int i = 1; i <= 2; i++) {
            for (int j = 1; j <= 2; j++) {
                s[i][j] = P - s[i][j] % P;
                if (i == j)
                    sum += s[i][j];
                else
                    sum -= s[i][j];
            }
        }
        printf("%s\n", sum % P == 0 ? "Yes" : "No");
    }
    return 0;
}