Thursday, July 24, 2014

Hackerrank Weekly Challenges - Week 7 - Die Hard 3 solution

Hackerrank Weekly Challenges - Week 7 - Die Hard 3: https://www.hackerrank.com/contests/w7/challenges/die-hard-3

Hackerrank Weekly Challenges - Week 7 - Die Hard 3 editorial: http://chasethered.com/2014/07/hackerrank-weekly-challenge-7-problem-1-die-hard-3/ &
https://www.hackerrank.com/contests/w7/challenges/die-hard-3/editorial

Hackerrank Weekly Challenges - Week 7 - Die Hard 3 solution: http://ideone.com/FET4JY

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;

int gcd(int u, int v) {
    if(u==v) return u;
    if(v==0) return u;
    if(u==0) return v;
    if(~u&1) {
        if(v&1) return gcd(u>>1, v);
        return gcd(u>>1, v>>1)<<1;
    }
    if(~v&1) return gcd(u, v>>1);
    if(u>v) return gcd((u-v)>>1, v);
    return gcd((v-u)>>1, u);
}

int main() {
    int t, a, b, c;
    scanf("%d", &t);
    while(t--) {
        scanf("%d%d%d", &a, &b, &c);
        if(c%gcd(a, b)==0 && c<=max(a, b)) printf("YES\n");
        else printf("NO\n");
    }
    return 0;
}

No comments:

Post a Comment