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. 网页绘制图表 Google Charts with JavaScript #2 ....与ASP.NET网页结合 (ClientScriptManager.RegisterStartupScript 方法)

    此为文章备份,原文出处(我的网站) 网页绘制图表 Google Charts with JavaScript #2 ....与ASP.NET网页结合 (ClientScriptManager.Regi ...

  2. 生产WCF客户端类文件的命令格式

    生产WCF客户端类文件的命令格式: svcutil.exe net.tcp://127.0.0.1:8732/ChromaMI.Remote.ConfigService/RemoteConfigSer ...

  3. 第二章 管理程序流(In .net4.5) 之 管理多线程

    1. 概述 本章包括同步资源以及取消长时间任务相关的内容. 2. 主要内容 2.1 同步资源 ① lock关键字实现.会阻塞程序,有可能会导致死锁. ② volatile关键字可以禁用编译优化,用于避 ...

  4. jQuery两句话实现HTML转义与反转义

    $('<div>').text('<a>').html() 结果:<a> $('<div>').html('<a>').text() 结果: ...

  5. [转载]--类unix系统如何初始化shell

    Shell的配置文件 当我们在linux中打开一个shell的时候,系统会读取相关的配置文件来初始化shell(其他unix-like OS也一样).配置文件包括以下这些: 1. 全局配置(对所有用户 ...

  6. wpf 获取DataGrid某一个单元格,设置此单元格ToolTip内容和背景颜色

    public void GetCell()        {            for (int i = 0; i < this.datagrid1.Items.Count; i++)    ...

  7. Java从入门到精通——调错篇之SVN 出现 Loced错误

    我在更新SVN的时候同时并打开了在这SVN上的一个文档结果导致了我更新的时候提示Loced错误 解决方法:出现这个问题后使用"清理"功能,如果还不行,就直接到上一级目录,再执行&q ...

  8. Oracle数据迁移至MySQL

    ORACLE DB: 11.2.0.3.0 MYSQL DB: 5.5.14 因项目需求,需要将ORACLE生产中数据迁移至MYSQL数据库中作为初始数据,方法有如下几种: 1.ORACLE OGG ...

  9. [INS-41112] Specified network interface doesnt maintain connectivi

    OS: Oracle Linux Server release 6.3 DB: Oracle 11.2.0.3 安装11.2.0.3.0的RAC,在安装GRID时报错: [INS-41112] Spe ...

  10. Postgres Basic Commands for Beginners

    Just sharing what I have learned about postgres recently. Here is a part of basic commands you may n ...