A. 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 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).

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).

官方题解:

322C - Ciel and Robot 321A - Ciel and Robot

Note that after Ciel execute string s, it will moves (dx, dy).

And for each repeat, it will alway moves (dx, dy).

So the total movement will be k * (dx, dy) + (dx[p], dy[p]) which (dx[p], dy[p]) denotes the movement after execute first p characters.

We can enumerate p since (0 <= p < |s| <= 100), and check if there are such k exists.

Note that there are some tricks:

We can divide dx or dy directly because they both can become zero.

Another trick is that k must be non-negative.

Many people failed on this test case (which no included in the pretest):

-1 -1

UR

#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std;
int dx[111],dy[111];
int ta,tb;
char str[111];
int main()
{
    cin>>ta>>tb;
    cin>>str;
    memset(dx,0,sizeof(dx));
    memset(dy,0,sizeof(dy));
    int len=strlen(str);
    for(int i=0;i<len;i++)
    {
        if(str=='U')
        {
            dy[i+1]=dy+1;
            dx[i+1]=dx;
        }
        else if(str=='D')
        {
            dy[i+1]=dy-1;
            dx[i+1]=dx;
        }
        else if(str=='L')
        {
            dx[i+1]=dx-1;
            dy[i+1]=dy;
        }
        else if(str=='R')
        {
            dx[i+1]=dx+1;
            dy[i+1]=dy;
        }
    }
/*
    for(int i=0;i<=len;i++)
    {
        cout<<dx<<" and "<<dy<<endl;
    }
*/
    int ddx=dx[len],ddy=dy[len];
    int OK=0;
    if(ta==0&&tb==0) OK=1;
    if(!OK)
    for(int i=1;i<=len;i++)
    {
        if(ta==dx&&tb==dy)
        {
            OK=1;
            break;
        }
    }
    if(!OK)
    for(int i=0;i<len;i++)
    {
        int kx=ta-dx;
        int ky=tb-dy;
        if(ddx==0&&ddy==0)
        {
            if(kx==0&&ky==0)
            {
                OK=1;break;
            }
        }
        else if(ddx==0&&ddy!=0)
        {
            if(kx==0)
            {
                if(ky%ddy==0)
                {
                    if(ky/ddy>=0)
                    {
                        OK=1;
                        break;
                    }
                }
            }
        }
        else if(ddy==0&&ddx!=0)
        {
            if(ky==0)
            {
                if(kx%ddx==0)
                {
                    if(kx/ddx>=0)
                    {
                        OK=1;
                        break;
                    }
                }
            }
        }
        else if(ddx!=0&&ddy!=0)
        {
            if(kx%ddx==0&&ky%ddy==0)
            {
                int t1=kx/ddx;
                int t2=ky/ddy;
                if(t1==t2)
                {
                    if(t1>=0)
                    {
                        OK=1;
                        break;
                    }
                }
            }
        }
    }
    if(OK==1)
    {
        puts("Yes");
    }
    else puts("No");
    return 0;
}

CodeForces 321A的更多相关文章

  1. CodeForces 321A Ciel and Robot(数学模拟)

    题目链接:http://codeforces.com/problemset/problem/321/A 题意:在一个二维平面中,開始时在(0,0)点,目标点是(a.b),问能不能通过反复操作题目中的指 ...

  2. Codeforces Round 190 div.2 322C 321A Ciel and Robot

    唔...这题是数学题. 比赛时做出来,但题意理解错了,以为只要判断那点是不是在线上就行了,发现过不了样例就没提交. 思路:记录每一步的偏移,假设那点是在路径上的某步,然后回推出那一个周期的第一步,判断 ...

  3. python爬虫学习(5) —— 扒一下codeforces题面

    上一次我们拿学校的URP做了个小小的demo.... 其实我们还可以把每个学生的证件照爬下来做成一个证件照校花校草评比 另外也可以写一个物理实验自动选课... 但是出于多种原因,,还是绕开这些敏感话题 ...

  4. 【Codeforces 738D】Sea Battle(贪心)

    http://codeforces.com/contest/738/problem/D Galya is playing one-dimensional Sea Battle on a 1 × n g ...

  5. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  6. 【Codeforces 738A】Interview with Oleg

    http://codeforces.com/contest/738/problem/A Polycarp has interviewed Oleg and has written the interv ...

  7. CodeForces - 662A Gambling Nim

    http://codeforces.com/problemset/problem/662/A 题目大意: 给定n(n <= 500000)张卡片,每张卡片的两个面都写有数字,每个面都有0.5的概 ...

  8. CodeForces - 274B Zero Tree

    http://codeforces.com/problemset/problem/274/B 题目大意: 给定你一颗树,每个点上有权值. 现在你每次取出这颗树的一颗子树(即点集和边集均是原图的子集的连 ...

  9. CodeForces - 261B Maxim and Restaurant

    http://codeforces.com/problemset/problem/261/B 题目大意:给定n个数a1-an(n<=50,ai<=50),随机打乱后,记Si=a1+a2+a ...

随机推荐

  1. 4.python中的用户交互

    学习完如何写'hello world'之后,我们还是不太满意,因为这样代码就写死了,以后运行的时候都只打印一局固定的话而已. 但是,我想在程序运行后,自己手动输入内容怎么办,此时就要学习如何使用用户交 ...

  2. AlertDialog.Builder对话框类的用法

    1.在测试时,如何实现一个提示 可以使用 Toast.makeText(this, "这是一个提示", Toast.LENGTH_SHORT).show(); //从资源文件str ...

  3. Reverse Vowels of a String

    Write a function that takes a string as input and reverse only the vowels of a string. Example 1:Giv ...

  4. 菜鸟学习Spring——初识Spring

    一.概念. Spring是一个开源框架,Spring是于2003 年兴起的一个轻量级的Java 开发框架,由Rod Johnson 在其著作Expert One-On-One J2EE Develop ...

  5. SQLServer异常捕获

    在SQLserver数据库中,如果有很多存储过程的时候,我们会使用动态SQL进行存储过程调用存储过程,这时候,很可能在某个环节就出错了,但是出错了我们很难去跟踪到出错的存储过程,此时我们就可以使用异常 ...

  6. 说明一下JNI 与AIDL

    代码在评论中. JNI: 为什么需要JNI: 因为android是由[JAVA & C/C++]组成.Java运行在Dalvik虚拟机中. 没有办法直接访问底层硬件.底层HW相关目前技术一般都 ...

  7. 安装RubyMine

    在mac上安装RubyMine的方法: 1.运行 brew cask install rubymine  自动安装. 2.按提示安装java更新. 3.RubyMine注册码: name: rubym ...

  8. Java Day 15

    String 字符串对象一旦被初始化就不会被改变  字符串常量池  String s = "abc"; //字符串常量池 String s = new String("a ...

  9. 【转载】matlab中freqz函数的使用

    freqz函数计算线性系统的频率响应,包括幅频响应和相频响应,基本输入为线性系统的AMMA模型系数向量,一个典型的AMMA模型为 %               jw               -j ...

  10. JavaScript美术馆进化史

    内容选自<<JavaScript DOM 编程艺术>>第4-6章,跟着作者一起见证美术馆的进化吧. 先放效果图,然后一步步做出每个效果.每个效果都有它实用的地方,且知道过程可以 ...