Codeforces Round #190 (Div. 2)-C. Ciel and Robot

来源:互联网 发布:js点击触发事件 方法名 编辑:程序博客网 时间:2024/05/16 05:16

原题链接

C. Ciel and Robot
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Fox Ciel has a robot on a 2D plane. Initially it is located in (0, 0). Fox Ciel code a command to it. The command was represented by string s. Each character of s is one move operation. There are four move operations at all:

  • 'U': go up, (x, y)  →  (x, y+1);
  • 'D': go down, (x, y)  →  (x, y-1);
  • 'L': go left, (x, y)  →  (x-1, y);
  • 'R': go right, (x, y)  →  (x+1, y).

The robot will do the operations in s from left to right, and repeat it infinite times. Help Fox Ciel to determine if after some steps the robot will located in (a, b).

Input

The first line contains two integers a and b, ( - 109 ≤ a, b ≤ 109). The second line contains a string s (1 ≤ |s| ≤ 100s only contains characters 'U', 'D', 'L', 'R') — the command.

Output

Print "Yes" if the robot will be located at (a, b), and "No" otherwise.

Examples
input
2 2RU
output
Yes
input
1 2RU
output
No
input
-1 1000000000LRRLU
output
Yes
input
0 0D
output
Yes
把robot沿着s串走一次称为一周期,一周期后走到(x, y)若x != 0 || y != 0则接下来每一周期结束后走到的点都在(0, 0)->(x, y)这条射线上.把(a, b)沿着s串逆方向走,观察是否在这条射线上

#include <bits/stdc++.h>#define maxn 105using namespace std;typedef long long ll;char s[maxn];int a, b, x, y, p1, p2;bool judge(int k1, int k2){if(k1 == 0 && k2 == 0) return true;if(x == 0 && y == 0){if(k1 == 0 && k2 == 0) return true;return false;}if(x == 0){if(k1 == 0 && (ll)y * k2 > 0 && k2 % y == 0)   return true;return false;}if(y == 0){if(k2 == 0 && (ll)x * k1 > 0 && k1 % x == 0)   return true;return false;}if((ll)x * k2 == (ll)y * k1 && (ll)x * k1 > 0 && (ll)y * k2 > 0 && k1 % x == 0 && k2 % y == 0) return true;return false;}int main(){//freopen("in.txt", "r", stdin);scanf("%d%d%s", &a, &b, s);x = 0;y = 0;for(int i = 0; s[i]; i++){    if(s[i] == 'L')     x--;    else if(s[i] == 'R')     x++;    else if(s[i] == 'D')     y--;    else     y++;}if(judge(a, b)){puts("Yes");return 0;}int len = strlen(s);for(int i = len-1; i >= 0; i--){p1 = a, p2 = b;for(int j = i; j >= 0; j--){if(s[j] == 'L')          p1++;    else if(s[j] == 'R')       p1--;    else if(s[j] == 'D')       p2++;        else          p2--;}if(judge(p1, p2)){puts("Yes");return 0;}}puts("No");return 0;}



0 0
原创粉丝点击