Drazil and Date【周賽題目-H】
Drazil and Date
題目 [CodeForces - 515A]
Someday, Drazil wanted to go on date with Varda. Drazil and Varda live on Cartesian plane. Drazil’s home is located in point (0, 0) and Varda’s home is located in point (a, b). In each step, he can move in a unit distance in horizontal or vertical direction. In other words, from position (x, y) he can go to positions (x + 1, y), (x - 1, y), (x, y + 1) or (x, y - 1).
Unfortunately, Drazil doesn’t have sense of direction. So he randomly chooses the direction he will go to in each step. He may accidentally return back to his house during his travel. Drazil may even not notice that he has arrived to (a, b) and continue travelling.
Luckily, Drazil arrived to the position (a, b) successfully. Drazil said to Varda: “It took me exactly s steps to travel from my house to yours”. But Varda is confused about his words, she is not sure that it is possible to get from (0, 0) to (a, b) in exactly s steps. Can you find out if it is possible for Varda?
Time limit | Memory limit | Tags | Source |
---|---|---|---|
1000 ms | 32768 kB | math *1100 | Codeforces Round #292 (Div. 2) |
Input
You are given three integers a, b, and s ( - 109 ≤ a, b ≤ 109, 1 ≤ s ≤ 2·109) in a single line.
Output
If you think Drazil made a mistake and it is impossible to take exactly s steps and get from his home to Varda’s home, print “No” (without quotes).
Otherwise, print “Yes”.
Example
Input | Output |
---|---|
5 5 11 | No |
10 15 25 | Yes |
0 5 1 | No |
0 0 2 | Yes |
Note
In fourth sample case one possible route is:
問題連結: CodeForces - 515A
問題描述
先放個中文翻譯吧(記得提高英文水平啊,作者本人)
有一天,Drazil想和瓦爾達約會。德拉吉和瓦爾達生活在笛卡爾平面上。Drazil的家位於點(0,0),瓦爾達的家位於點(A,B)。在每個步驟中,他可以在水平或垂直方向上以單位距離移動。換言之,從位置(x,y)他可以到達位置(x+1,y)、(x-1,y)、(x,y+1)或(x,y_1)。
不幸的是,Drazil沒有方向感。所以他隨機選擇每個方向的方向。在旅途中,他可能不小心回到家裡。德拉齊爾甚至沒有注意到他已經到達(A,B)並繼續旅行。
幸運的是,Drazil成功地到達了(A,B)的位置。Drazil對瓦爾達說:“我從我的房子到你家走了整整一步。”但是瓦達對他的話感到困惑,她不能確定是否可以按正確的步驟從(0,0)到(a,b)。你能查明瓦爾達是否可能嗎?
問題分析
從位置(x,y)他可以到達位置(x+1,y)、(x-1,y)、(x,y+1)或(x,y_1)。
Drazil可能已經到了Varda家但是它不知道,換言之就是可以經過點(a,b)但還繼續走,這時如果步數不能讓他回到(a,b)點也要輸出“No”
我們要做的就是去判斷Drazil所說的步數到底能不能走到Varda的家,如果不能我們就輸出“No”,如果能我們就輸出“Yes”
(其實我蠻想把一次一次的WA過程以及原因都說出來的題目不難但是我WA了好多次)
其實我想的比較複雜,賽後聽完別人的闡述之後覺得別人的簡單很多。
程式碼分析
作為座標,a,b的值可能是負數(這個在Example中沒有給出,一開始就沒有考慮到)【所以就要先把a、b變成正數,這樣在相加的時候才不會出現錯誤】
首先把a、b變成正數
這是我的實現方法:
if (a < 0)
{
a = -a;
}
if (b < 0)
{
b = -b;
其實還是複雜了(當時還不知道絕對值的函式(ಥ﹏ಥ))
其實可以這樣乾的
#include<cmath>//C語言是math.h(標頭檔案)
a = abs(a);//求a的絕對值
b = abs(b);//求b的絕對值
(其實還是還是覺得用一個變數去儲存(a+b)的值好一點,不然在書寫上有點麻煩)
如果a和b都等於0那麼就是在原點,這是步數一定是偶數才能回到原點
【這一步其實是看到Example裡面的0 0 2想到的】
if ((a == 0 && b == 0) && s % 2 == 0)
{
cout << "Yes" << endl;
}
如果s剛好等於(a+b),那麼是可以的也是最短的程
當然也是可以輸出“Yes”的
我處理這個的程式碼如下
else if ((a + b) == s)
{
cout << "Yes" << endl;
}
接下來判斷當s>(a+b)時的情況
我當時在桌上話了很多次
就是當(a+b)與s的奇偶型別相同時就可以到達
所以實現的程式碼如下(略顯複雜了)
else if (((a + b) < s) && ((a + b) % 2 == 0 && s % 2 == 0))
{
cout << "Yes" << endl;
}
else if (((a + b) < s) && ((a + b) % 2 != 0 && s % 2 != 0))
{
cout << "Yes" << endl;
}
如果不是的話就輸出“No”
else cout << "No" << endl;
AC通過的CPP全部程式碼如下
#include<iostream>
using namespace std;
int main()
{
int a, b, s;
while (cin >> a >> b >> s)
{
if (a < 0)
{
a = -a;
}
if (b < 0)
{
b = -b;
}
if ((a == 0 && b == 0) && s % 2 == 0)
{
cout << "Yes" << endl;
}
else if ((a + b) == s)
{
cout << "Yes" << endl;
}
else if (((a + b) < s) && ((a + b) % 2 == 0 && s % 2 == 0))
{
cout << "Yes" << endl;
}
else if (((a + b) < s) && ((a + b) % 2 != 0 && s % 2 != 0))
{
cout << "Yes" << endl;
}
else cout << "No" << endl;
}
return 0;
}
More
【賽後聽了一下別人的想法】
其實只要當步數s大於x+y時若s-(x+y)是偶數則能到達,若為奇數則不能到達。