Number Busters(数学)

B. Number Busters
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Arthur and Alexander are number busters. Today they've got a competition.

Arthur took a group of four integers a, b, w, x (0 ≤ b < w, 0 < x < w) and Alexander took integer с. Arthur and Alexander use distinct approaches to number bustings. Alexander is just a regular guy. Each second, he subtracts one from his number. In other words, he performs the assignment: c = c - 1. Arthur is a sophisticated guy. Each second Arthur performs a complex operation, described as follows: if b ≥ x, perform the assignment b = b - x, if b < x, then perform two consecutive assignments a = a - 1; b = w - (x - b).

You've got numbers a, b, w, x, c. Determine when Alexander gets ahead of Arthur if both guys start performing the operations at the same time. Assume that Alexander got ahead of Arthur if c ≤ a.

Input

The first line contains integers a, b, w, x, c (1 ≤ a ≤ 2·109, 1 ≤ w ≤ 1000, 0 ≤ b < w, 0 < x < w, 1 ≤ c ≤ 2·109).

Output

Print a single integer — the minimum time in seconds Alexander needs to get ahead of Arthur. You can prove that the described situation always occurs within the problem's limits.

Sample test(s)
input
4 2 3 1 6
output
2
input
4 2 3 1 7
output
4
input
1 2 3 2 6
output
13
input
1 1 2 1 1
output
0

    题意:

    给出a,b,w,x,c(1 ≤ a ≤ 2·10^9, 1 ≤ w ≤ 1000, 0 ≤ b < w, 0 < x < w, 1 ≤ c ≤ 2·10^9)。要求经过最少步数使 c <= a ,数值变化有两种情况:

    1.当 b >= x 时,b = b - x ,c = c - 1;

     2.当 b  <  x 时,a = a - 1,b = w - (x - b),c = c - 1;

 

    思路:

    数学题。

    1.注意数据范围,2乘10的9次方,说明要用long long型;

    2.只有当 b >= x 的时候,c 和 a 才没有同步减少,说明 c 和 a 之间的距离唯有当 b >= x 时候才能缩小,当 c == a 的时候,此时的步数为最少;

    3.设 b >= x 的时候经过了 k1 步,b < x 的时候经过了 k2 步,则可以列出两种情况的式子:

       当 b >= x 时,b = b - k1 * x,c = c - k1;

       当 b <   x 时,b = b + k2 * (w - x),c = c - k2,a = a - k2;

       而题目要求的是 k1 + k2 最小,那么当 c == a 的时候,即 c - k1 - k2 = a - k2 的时候最少,所以 k1 = c - a;

    4.因为题目要求 b >= 0 ,故 b 的总变化大于等于0,所以 b = b - k1 * x + k2 * (w - x)>= 0,同时把k1代进去,可求出 k2 >= ((c - a)* x - b) / (w - x),注意对 k2 向上取整;

    5.最后求出总式子就是 (((c - a)* x - b) / (w - x))向上取整 + c - a (就是把 k1 + k2 代进去);

    6.注意 ceil 向上取整函数最后要转回 long long 型,ceil 默认为 double 型的,但是除法在函数里面要转为 double 来算,不然会把小数部分截断了;

    7.当 c <= a 时,输出0即可,不需要代公式求出。

 

    AC:

#include<stdio.h>
#include<math.h>
int main()
{
    __int64 a,b,w,x,c,d;
    scanf("%I64d%I64d%I64d%I64d%I64d",&a,&b,&w,&x,&c);
    if(c <= a)  printf("0\n");
    else        printf("%I64d\n",(__int64)ceil((double)((c - a) * x - b) / (double)(w - x)) + c - a);
    return 0;
}

 

 

 

 

 

你可能感兴趣的:(number)