您的位置:首页 > 其它

CF 322C - Ciel and Robot 计算周期

2013-07-12 08:24 621 查看
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| ≤ 100, s only
contains characters 'U', 'D', 'L',
'R') — the command.

Output

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

Sample test(s)

input
2 2
RU


output
Yes


input
1 2
RU


output
No


input
-1 1000000000
LRRLU


output
Yes


input
0 0
D


output
Yes


Note

In the first and second test case, command string is "RU", so the robot will go right, then go up, then right, and then up and so on.

The locations of its moves are (0, 0)  →  (1, 0)  →  (1, 1)  →  (2,
1)  →  (2, 2)  →  ...

So it can reach (2, 2) but not (1, 2).

字符串s能够无限次重复,问能否按照字符串S的指令,从(0,0)移动到(a,b)

只用输出Yes或者No
因为指令S<=100。。就是判断额。。算周期

   x[len]*zhouqi + x[i] == a

&& y[len]*zhouqi + y[i] == b

/*
* @author ipqhjjybj
* @date  20130711
*
*/
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
#define max(a,b) ((a)>(b)?(a):(b))
#define min(a,b) ((a)<(b)?(a):(b))

int main(){
int a,b;
char s[105];
int x[105]={0},y[105]={0};

scanf("%d %d %s",&a,&b,s+1);
int len = strlen(s+1);
for(int i=1;i<=len;i++){
switch(s[i]){
case 'U':x[i]=x[i-1],y[i]=y[i-1]+1;break;
case 'D':x[i]=x[i-1],y[i]=y[i-1]-1;break;
case 'L':x[i]=x[i-1]-1,y[i]=y[i-1];break;
case 'R':x[i]=x[i-1]+1,y[i]=y[i-1];break;
}
}
int zhouqi;
for(int i = 0;i<=len;i++){
int da=a-x[i];
int db=b-y[i];
zhouqi=(x[len]!=0?da/x[len]:(y[len]!=0?db/y[len]:1));
if(zhouqi>=0
&&x[len]*zhouqi==da
&&y[len]*zhouqi==db){
printf("Yes\n");
return 0;
}
}
printf("No\n");
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  CF ACM 水题