Zhejiang Provincial Roller Function

Source: Internet
Author: User

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

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.