Codeforces 321A Ciel and Robot 枚举答案

题目链接

枚举机器人走的最后一步,用终点坐差后计算周期次数

trick:周期次数要>=0

#include <iostream>
#include <string>
#include <vector>
#include <cstring>
#include <cstdio>
#include <map>
#include <queue>
#include <algorithm>
#include <stack>
#include <cstring>
#include <cmath>
#include <set>
#include <vector>
using namespace std;
template <class T>
inline bool rd(T &ret) {
    char c; int sgn;
    if (c = getchar(), c == EOF) return 0;
    while (c != '-' && (c<'0' || c>'9')) c = getchar();
    sgn = (c == '-') ? -1 : 1;
    ret = (c == '-') ? 0 : (c - '0');
    while (c = getchar(), c >= '0'&&c <= '9') ret = ret * 10 + (c - '0');
    ret *= sgn;
    return 1;
}
template <class T>
inline void pt(T x) {
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    if (x > 9) pt(x / 10);
    putchar(x % 10 + '0');
}
typedef long long ll;
typedef pair<int, int> pii;
const int inf = 1e9;
const int N = 105;

ll a, b;
char s[N];
ll x[N], y[N];
int n;
bool work() {
    for (int i = 0; i < n; i++)
    {
        ll l = a - x[i];
        ll r = b - y[i];
        if (x[n] == 0)
        {
            if (l)continue;
            if (y[n] == 0)
            {
                if (r)continue;
                else return true;
            }
            if (r%y[n])continue;
            if(r/y[n] >= 0)
            return true;
            else continue;
        }
        if (l%x[n])continue;
        if (l / x[n] < 0)continue;
        if (y[n] == 0)
        {
            if (r)continue;

            return true;
        }
        if (r%y[n])continue;
        if (l / x[n] == r / y[n]) {
            if (l / x[n] >= 0)return true;
        }
    }
    return false;
}
int main() {
    rd(a); rd(b);
    scanf("%s", s + 1); n = strlen(s + 1);
    x[0] = y[0] = 0;
    for (int i = 1; s[i]; i++) {
        x[i] = x[i - 1]; y[i] = y[i - 1];
        if (s[i] == 'U')y[i] = y[i - 1] + 1;
        if (s[i] == 'D')y[i] = y[i - 1] - 1;
        if (s[i] == 'L')x[i] = x[i - 1] - 1;
        if (s[i] == 'R')x[i] = x[i - 1] + 1;
    }
    work() ? puts("Yes") : puts("No");
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-06 02:35:38

Codeforces 321A Ciel and Robot 枚举答案的相关文章

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

题目链接:http://codeforces.com/problemset/problem/321/A 题意:在一个二维平面中,開始时在(0,0)点,目标点是(a.b),问能不能通过反复操作题目中的指令,从原点移动到目标点. 分析:如果一次完毕全部的命令后.移动到了(xx,yy),而且从(Xi.Yi)反复操作k次指令到达目标点.则能够列出方程 Xi + k * xx = a && Yi + k * yy = b.然后解出k.推断k是否大于等于0就可以. #include <cstdi

Codeforces G. Ciel the Commander

题目描述: Ciel the Commander time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output Now Fox Ciel becomes a commander of Tree Land. Tree Land, like its name said, has n cities connected by n?-?1 undire

Educational Codeforces Round 76 F 折半枚举

Educational Codeforces Round 76 F 折半枚举 https://codeforces.com/contest/1257/problem/F 题意: 数组a,找到一个x使得a中每一个元素异或x后"二进制中1的个数"相同. 数组长度100,数字大小2^30. 思路: 折半枚举答案X,如分为X前15位和后15位.之后我们再枚举期望的"相同个数"是多少,通过hash看看能不能满足就好了. 代码: #include <bits/stdc++

用二分枚举答案题

以二分算法枚举答案的方法非常高效,枚举所需的时间复杂度只需O(logN) 设x为答案,check(x)只有真假两种取值. 能够使用二分枚举算法的条件: 存在一个X,当x小于X时和x大于X时,check(x)真假值不一样. 二分枚举即能够找出真假之间的边界,这个就是所求在满足题意下的最值了. 如果x是浮点数,那么边界的取值注意精度即可. const double eps=1e-7; while(l+eps<r) { mid=(l+r)/2; if(check(mid)) l=mid; else r

Brownie Slicing(二分枚举答案)

描述 Bessie has baked a rectangular brownie that can be thought of as an RxC grid (1 <= R <= 500; 1 <= C <= 500) of little brownie squares.The square at row i, column j contains N_ij (0 <= N_ij <= 4,000) chocolate chips.Bessie wants to par

CodeForces 321 A - Ciel and Robot

[题目链接]:click here~~ [题目大意]:一个robot 机器人 ,可以根据给定的指令行动,给你四种指令,robot初始位置是(0,0),指令一出,robot会重复行动,判断能否在无限行动的情况下经过点(n,m). [解题思路]其实仔细模拟一下,可以发现是有周期的,判断即可,见代码吧~~ 代码: #include <iostream> #include <algorithm> #include <bits/stdc++.h> using namespace

CodeForces 321C Ciel the Commander

Ciel the Commander Time Limit: 1000ms Memory Limit: 262144KB This problem will be judged on CodeForces. Original ID: 321C64-bit integer IO format: %I64d      Java class name: (Any) Now Fox Ciel becomes a commander of Tree Land. Tree Land, like its na

Codeforces Gym 100002 B Bricks 枚举角度

Problem B Bricks" Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100002 Description The prisoner of the "IF" castle has decided to run away by disassembling the brick wall in his prison cell. To hide his work from his

[codeforces 200 E Tractor College]枚举,扩展欧几里得,三分

题目出自 Codeforces Round #126 (Div. 2) 的E. 题意大致如下:给定a,b,c,s,求三个非负整数x,y,z,满足0<=x<=y<=z,ax+by+cz=s,使得f(x,y,z)=|ax-by|+|by-cz|最小 思路:枚举z,得到一个方程ax+by=s-cz,用扩展欧几里得求出这个方程的一个解,然后三分通解的整系数,求出最小f值.至于为什么可以三分画画图就清楚了,两个绝对值函数叠加在一起最多只有三种状态(第一维表示临界点较小的那个绝对值函数):(降,降)