CodeForces 321A Ciel and Robot(數學模擬)
阿新 • • 發佈:2017-05-14
ont amp force 等於 return printf print -- 題意
題目鏈接:http://codeforces.com/problemset/problem/321/A
題意:在一個二維平面中,開始時在(0,0)點,目標點是(a。b),問能不能通過反復操作題目中的指令,從原點移動到目標點。
分析:如果一次完畢全部的命令後。移動到了(xx,yy),而且從(Xi。Yi)反復操作k次指令到達目標點。則能夠列出方程 Xi + k * xx = a && Yi + k * yy = b。然後解出k。推斷k是否大於等於0就可以。
#include <cstdio> #include <cstring> #include <algorithm> using namespace std; typedef __int64 LL; LL a, b, xx, yy; LL X[150], Y[150]; int len; bool check(LL x, LL y) { LL tmp_x = a - x, tmp_y = b - y; if(xx == 0) { if(yy == 0) { if(tmp_x == 0 && tmp_y == 0) return true; else return false; } else { if(tmp_y % yy == 0) { if(tmp_y / yy >= 0 && tmp_x == 0) return true; else return false; } else return false; } } else { if(yy == 0) { if(tmp_x % xx == 0) { if(tmp_x / xx >= 0 && tmp_y == 0) return true; else return false; } else return false; } else { if(tmp_x % xx == 0 && tmp_y % yy == 0) { if(tmp_x / xx >= 0 && tmp_y / yy >= 0 && tmp_x / xx == tmp_y / yy) return true; else return false; } else return false; } } } int main() { char op[150]; while(~scanf("%I64d%I64d", &a, &b)) { scanf("%s", op); if(a == 0 && b == 0) { printf("Yes\n"); continue; } len = strlen(op); LL x = 0, y = 0; int FLAG = 0; for(int i = 0; i < len; i++) { if(op[i] == ‘U‘) y++; else if(op[i] == ‘D‘) y--; else if(op[i] == ‘L‘) x--; else if(op[i] == ‘R‘) x++; X[i] = x, Y[i] = y; } xx = X[len-1], yy = Y[len-1]; for(int i = 0; i < len; i++) { if(check(X[i], Y[i])) { FLAG = 1; printf("Yes\n"); break; } } if(!FLAG) printf("No\n"); } return 0; }
CodeForces 321A Ciel and Robot(數學模擬)