CF1117C. Magic Ship(二分)

C. Magic Ship

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You a captain of a ship. Initially you are standing in a point (x1,y1)(x1,y1) (obviously, all positions in the sea can be described by cartesian plane) and you want to travel to a point (x2,y2)(x2,y2).

You know the weather forecast — the string ss of length nn, consisting only of letters U, D, L and R. The letter corresponds to a direction of wind. Moreover, the forecast is periodic, e.g. the first day wind blows to the side s1s1, the second day — s2s2, the nn-th day — snsn and (n+1)(n+1)-th day — s1s1 again and so on.

Ship coordinates change the following way:

  • if wind blows the direction U, then the ship moves from (x,y)(x,y) to (x,y+1)(x,y+1);
  • if wind blows the direction D, then the ship moves from (x,y)(x,y) to (x,y−1)(x,y−1);
  • if wind blows the direction L, then the ship moves from (x,y)(x,y) to (x−1,y)(x−1,y);
  • if wind blows the direction R, then the ship moves from (x,y)(x,y) to (x+1,y)(x+1,y).

The ship can also either go one of the four directions or stay in place each day. If it goes then it's exactly 1 unit of distance. Transpositions of the ship and the wind add up. If the ship stays in place, then only the direction of wind counts. For example, if wind blows the direction Uand the ship moves the direction L, then from point (x,y)(x,y) it will move to the point (x−1,y+1)(x−1,y+1), and if it goes the direction U, then it will move to the point (x,y+2)(x,y+2).

You task is to determine the minimal number of days required for the ship to reach the point (x2,y2)(x2,y2).

Input

The first line contains two integers x1,y1x1,y1 (0≤x1,y1≤1090≤x1,y1≤109) — the initial coordinates of the ship.

The second line contains two integers x2,y2x2,y2 (0≤x2,y2≤1090≤x2,y2≤109) — the coordinates of the destination point.

It is guaranteed that the initial coordinates and destination point coordinates are different.

The third line contains a single integer nn (1≤n≤1051≤n≤105) — the length of the string ss.

The fourth line contains the string ss itself, consisting only of letters U, D, L and R.

Output

The only line should contain the minimal number of days required for the ship to reach the point (x2,y2)(x2,y2).

If it's impossible then print "-1".

Examples

input

Copy

0 0
4 6
3
UUU

output

Copy

5

input

Copy

0 3
0 0
3
UDD

output

Copy

3

input

Copy

0 0
0 1
1
L

output

Copy

-1

Note

In the first example the ship should perform the following sequence of moves: "RRRRU". Then its coordinates will change accordingly: (0,0)(0,0) →→ (1,1)(1,1) →→ (2,2)(2,2) →→ (3,3)(3,3) →→ (4,4)(4,4) →→ (4,6)(4,6).

In the second example the ship should perform the following sequence of moves: "DD" (the third day it should stay in place). Then its coordinates will change accordingly: (0,3)(0,3) →→ (0,3)(0,3) →→ (0,1)(0,1) →→ (0,0)(0,0).

In the third example the ship can never reach the point (0,1)(0,1).

题意:一个船要从起点到终点,一个字符串代表风向每天的上下左右,每天船还可以选择再走一步,问至少几天到终点。

思路:首先风向和选择走的可以分离考虑,那么可以二分答案,先走K天,风向的终点先算出来,那么剩下K步自由发挥,判断距离目的地的曼哈顿距离够不够K就行。二分上界每一个轮回至少朝向目的地走一步,那么存在答案的话2e14就一定能到达。

# include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 1e5+30;
char s[maxn];
int x[maxn], y[maxn];
int x0, y0, x1, y1, n;
bool check(LL k){
    if(k < 0) return  false;
    LL d = k/n, r = k%n;
    LL dx = 1LL*d*x[n]+x[r]+x0, dy = 1LL*d*y[n]+y[r]+y0;
    LL tot = llabs(dx-x1)+llabs(dy-y1);
    return k >= tot;
}
int main(){
    scanf("%d%d%d%d%d",&x0,&y0,&x1,&y1, &n);
    scanf("%s",s+1);
    for(int i=1; i<=n; ++i){
        x[i] = x[i-1];
        y[i] = y[i-1];
        if(s[i] == 'U') ++y[i];
        else if(s[i] == 'D') --y[i];
        else if(s[i] == 'L') --x[i];
        else ++x[i];
    }
    LL l=0, r=4e17;
    while(l<=r){
        LL mid = l+r>>1;
        if(check(mid)) r = mid-1;
        else l = mid+1;
    }
    if(check(l)) printf("%lld\n",l);
    else puts("-1");
    return 0;
}

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值