Givena,b,c,D MeetbC>ad , seeking to makecn−d< Span id= "mathjax-span-172" class= "Mo" >⌊ ab n⌋ minimum n .
Data size: 1≤a,b,c,d≤ten
Makey=⌊abx⌋,x=⌈bya⌉ , the original problem is equivalent to seekingcx−dy The minimum value, ax≥by,x≥1,y≥0 .
Consider it in two different cases
1. a≥b
MakeK=⌊a B⌋ y≥ kx ,c >ad/b≥k< Span id= "mathjax-span-270" class= "Mi" >d
MakeA′=A−k b,c′=c−< Span id= "mathjax-span-288" class= "Mi" >kd ,y′=y−k< Span id= "mathjax-span-298" class= "Mi" >x , at this time the original problem of the approximate number of items are satisfied, the problem can be reduced scale.
The solution of the original problem(x,y) corresponding to the present solution(x,kx+y)
Obviously, ifa-=0 , we can get (x, y′) = (1,< Span id= "mathjax-span-338" class= "mn" >0)
2. A<b
Order k=⌊b−1a⌋,x>ky
MakeB′=B− ka ,d′=d−k< Span id= "mathjax-span-380" class= "Mo" >∗c ,x′=x−k< Span id= "mathjax-span-390" class= "Mi" >y
Similarly, the problem can be scaled down if the approximate number of pieces of the original question is satisfied.
Obviously, ifdto≤0 , we can get(x,y)=(1,0)
#include <bits/stdc++.h>using namespaceStd;typedefLong LongLL;voidSolve (ll A, ll B, LL C, LL D, LL &x, LL &y) {if(A = =0|| D <=0) {x =1, y =0;return;} if(A >=b) {LL k= A/b; Solve (a-K * b, B, c-k *D, D, X, y); Y+ = k *x; } Else{LL k= (b-1) /A; if(D/c < k) x =1, y =0; ElseSolve (A, b-k * A, C, d-k *c, x, y); X+ = k *y; }}intMain () {intT scanf"%d", &T); for(int_ =0; _ < T; ++ _) {LL A, B, C, D; scanf ("%lld%lld%lld%lld", &a, &b, &c, &d); LL x, y; Solve (A, B, C, D, X, y); printf ("%lld\n", x); } return 0;}View Code
Zhejiang Provincial Roller Function