Untitled

 avatar
unknown
plain_text
3 years ago
891 B
10
Indexable
#include <stdio.h>

int main()
{
    int T;
    long n;
    long x, y, z, three;        // "three" is the movement for those who get three balloons
    long total;                 // total movement

    scanf("%d", &T);
    while (T--) {
        scanf("%ld %ld %ld %ld", &n, &x, &y, &z);
        three = (((x * y) % n) * z) % n;
        if (three == 0) {                   // this case, GDC(x, y, z) is divisible by n
            printf("1\n");
        } else {
            if (three > n / 2)   three = n - three;
            total = n;                      // initialize "total" before starting the loop
            while (total % three) {         // if "total" is not divisible by "three"
                total += n;                 // here we try to find GCD(total, gcd) 
            }
            printf("%ld\n", total / three);
        }
    }
    return 0;
}
Editor is loading...