hdu4803 Poor Warehouse Keeper

Source: Internet
Author: User

Given two digital display boards A, B and two buttons, press the button to increase their corresponding digital display by 1.

Also, the B-Display board displays only the integer portion of its actual value. The actual value of the two monitors has a ratio p.

Press the button on board a, the number shown on board a increases by 1 to a + 1, p is unchanged, the number on the B board is updated to (int) (b * (A + 1)/a).

Press the button on the B board, the value shown on board A does not change, b board plus 1 to B + 1,p update to (b + 1)/A.

Given the target State (A1, B1), ask at least how many times the press button can reach that state from the initial state (1, 1).

If the output is not reached-1.

Obviously press the A-plate button to increase the value of the B-plate ratio of p, press the B-plate value to increase the proportion.

The scale increase process is irreversible, the state is unreachable and only when A1 < B1.

The first step from a plate array from 1 to A1 requires at least a1-1 operation, which is independent of the state of the B board.

The numerical variation of a plate causes the B plate to multiply, taking into account that f (i) = (i + 1)/I is a strictly diminishing function.

Intuitively we should be as far as possible to press a board operation ahead of time, so the number on the B board will be close to the target value as soon as possible.

Note that the ratio p< (b + 1)/A, we try to approach the value as close as possible, starting with 1.

Then, as the number of a plate increases, the value is gradually approximated, and the number of operations of the B board is recorded, the problem can be solved.

The floating-point number performs the approximate operation, which causes the error, and the fraction represents the rational (ratio).

acm.hdu.edu.cn/showproblem.php?pid=4803
1#include <cstdio>2#include <cstring>3#include <algorithm>4 5 using namespacestd;6 typedef __int64 LL;7 8 LL A, b;9 Ten ll GCD (ll A, ll b) { One     if(!B)returnA; A     returnGCD (b, a%b); - } -  the voidsolve () { -     if(b <a) { -printf"-1\n"); -         return; +     } -LL LHS =1, RHS =1; +     //RHS:: Denominator A     //LHS:: Numerator atLL cnt = A-1; -     //operations required for A -      for(inti =1; I <= A; i++){ -LL F = (b +1) * I * lhs-a * rhs, g = A *LHS; -LL k = (f% g = =0? F/g-1: F/g); -CNT + =K; in         //extra operations required for B -RHS + = k *LHS; toLL GCD = GCD ((i +1) * RHS, I *LHS); +RHS = RHS * (i +1) /GCD; -LHS = LHS * I/GCD; the     } *printf"%i64d\n", CNT); $ }Panax Notoginseng  - intMain () { the      while(~SCANF ("%i64d%i64d", &a, &b)) { + solve (); A     } the     return 0; +}
View Code

hdu4803 Poor Warehouse Keeper

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.