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 strings. 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).
The first line contains two integers a and b, ( - 109 ≤ a, b ≤ 109). The second line contains a string s (1 ≤ |s| ≤ 100, s only contains characters 'U', 'D', 'L', 'R') — the command.
Print "Yes" if the robot will be located at (a, b), and "No" otherwise.
2 2 RU
Yes
1 2 RU
No
-1 1000000000 LRRLU
Yes
0 0 D
Yes
#include<iostream>
#include<cstdio>
#include<string>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<stack>
#include<vector>
#include<climits>
#include<map>
using namespace std;
#define rep(i,n) for(int i=0; i<n; i++)
#define repf(i,n,m) for(int i=(n); i<=(m); ++i)
#define repd(i,n,m) for(int i=(n); i>=(m); --i)
#define max(a,b) (a)>(b)?(a):(b)
#define min(a,b) (a)<(b)?(a):(b)
#define fab(a) ((a)>0?(a):(0-(a)))
#define ll long long
#define arc(a) ((a)*(a))
#define inf 100000
#define exp 0.000001
#define N 200
char s[N];
ll x,y;
struct node{
ll x,y;
}a[N];
int main()
{
int sign=0;
while(cin>>x>>y)
{
sign++;
cin>>s;
a[0].x=a[0].y=0;
int len=strlen(s);
int flag=0;
rep(i,len)
{
a[i+1].x=a[i].x;a[i+1].y=a[i].y;
if(s[i]=='U') a[i+1].y++;
if(s[i]=='D') a[i+1].y--;
if(s[i]=='L') a[i+1].x--;
if(s[i]=='R') a[i+1].x++;
if(a[i+1].x==x && a[i+1].y==y)
flag=1;
}
if(x==0 && y==0)
{printf("Yes\n"); continue;}
ll xx=a[len].x,yy=a[len].y;
if((xx==0 && yy==0)&& flag==0)
{printf("No\n");continue;}
if(flag==0)
{
rep(i,len+1)
{
if(xx==0 && yy!=0 && (y-a[i].y)%yy==0&& a[i].x==x)
flag=1;
if(yy==0 && xx!=0 &&(x-a[i].x)%xx==0 && y==a[i].y)
flag=1;
if(xx*yy!=0 && (x-a[i].x)%xx==0 &&(y-a[i].y)%yy==0)
flag=1;
if(flag==1)
{
if(xx*yy!=0 && (x-a[i].x)/xx!=(y-a[i].y)/yy)
flag=0;
if(xx!=0 && (x-a[i].x)*xx<0)
flag=0;
if(yy!=0 && (y-a[i].y)*yy<0)
flag=0;
if(flag==1)
break;
}
}
}
if(flag==1)
printf("Yes\n");
else
printf("No\n");
}
return 0;
}