본문 바로가기

코드포스

[코드포스 Practice9] C. Treasure Hunt

Captain Bill the Hummingbird and his crew recieved an interesting challenge offer. Some stranger gave them a map, potion of teleportation and said that only this potion might help them to reach the treasure.

Bottle with potion has two values x and y written on it. These values define four moves which can be performed using the potion:

Map shows that the position of Captain Bill the Hummingbird is (x1, y1) and the position of the treasure is (x2, y2).

You task is to tell Captain Bill the Hummingbird whether he should accept this challenge or decline. If it is possible for Captain to reach the treasure using the potion then output "YES", otherwise "NO" (without quotes).

The potion can be used infinite amount of times.

Input
The first line contains four integer numbers x1, y1, x2, y2 ( - 105 ≤ x1, y1, x2, y2 ≤ 105) — positions of Captain Bill the Hummingbird and treasure respectively.

The second line contains two integer numbers x, y (1 ≤ x, y ≤ 105) — values on the potion bottle.

Output
Print "YES" if it is possible for Captain to reach the treasure using the potion, otherwise print "NO" (without quotes).


처음 딱 보니 수학문제이고 저번 코포 때 본 문제라 쉽게 풀겠다 생각했다. 하지만 결국 못 품

 

처음에는 x1 -> x2로 가려면 k*x를 더해야 하니 x2 - x1이 x의 배수가 되면 Yes를 출력하게 했다. 하지만 두번째 예제 넣어보면 바로 아니라는 걸 알 수 있다.

 

다음으로 여차여차 더하고 빼 보니 일정한 비율이 있는 것 같았다. 그래서 k의 비율이 이와 일치하면 참이라 생각했다. 하지만 이것도 틀렸음.. 다시 생각해보니 비율이 10:0일 수도 있다. 

 

 

좀 더 생각해보니 이거 비율의 합이 짝수이면 값이 어떻든 다 만들 수 있다. 그래서 (x2-x1) + (y2-y1)이 짝수이면 Yes를 출력했다. 근데 이번에는 7번 테스트에서 틀렸다.. 계속 고민해도 모르겠어서 포기했음.

 

끝나고 어디서 틀렸나 확인해보니 x, y가 엄청 큰 경우에서 틀렸다ㅋㅋㅋ 아 맞네 이런 경우도 있구나. 참고해서 다시 풀어봐야겠다. 

 

이렇게 다시 짰는데 5번 테스트에서 틀렸다. 이건 저녁 먹고 다시 해봐야지. 약속 늦게 생겼음 =3

 

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <functional>
#include <string>
 
using namespace std;
using i64 = long long;
 
int main() {
    int x1, y1, x2, y2;
    scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
    int x, y;
    scanf("%d %d", &x, &y);
    
    if((x2-x1 + y2-y1) % 2 != 0)
        printf("NO\n");
    else if((x2-x1)%x != 0)
        printf("NO\n");
    else if((y2-y1)%y != 0)
        printf("NO\n");
    else
        printf("YES\n");
    
    return 0;
}

아 이거

 

코드 2의 배수인거 왜 저렇게 해놨냐

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>
#include <functional>
#include <string>
 
using namespace std;
using i64 = long long;
 
int main() {
    int x1, y1, x2, y2;
    scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
    int x, y;
    scanf("%d %d", &x, &y);
    
    if(((x2-x1)/x + (y2-y1)/y) % 2 != 0)
        printf("NO\n");
    else if((x2-x1)%x != 0)
        printf("NO\n");
    else if((y2-y1)%y != 0)
        printf("NO\n");
    else
        printf("YES\n");
    
    return 0;
}

 

고쳤고 맞았다.

헝.. 수학 어려움