Uva 10673-play with Floor and ceil (extended Euclid)

Source: Internet
Author: User

Topic Link: Click to open the link

Test instructions: Gives a solution of X, K for equation P*floor (x/k) +q*ceil (x/k) =x. Floor () is rounded up and ceil () is rounded down.

Naked extension gcd, the title does not say the case of no solution, should be the default X%GCD (Floor (x/k), Ceil (x/k)) ==0

For extended Gcd,ax+by=d①, when D is a multiple of G=GCD (A, b), the equation ① has a solution that translates to the solution of Ax+by=g②, assuming that the solution of the equation ② is (x0,y0)

The equation ② around the same multiply d/g, get a A * (x0*d/g) +b* (y0*d/g) =g*d/g, so the solution of the equation ① is (x0*d/g,y0*d/g)

#include <algorithm> #include <iostream> #include <cstring> #include <cstdlib> #include < string> #include <cctype> #include <vector> #include <cstdio> #include <cmath> #include < queue> #include <stack> #include <map> #include <set> #define MAXN 1002#define _ll __int64#define ll Long long#define INF 0x3f3f3f3f#define Mod 10000007#define pp pair<int,int> #define ull unsigned long longusing names Pace Std;ll X,k;ll gcd (ll A,ll b) {return b==0?a:gcd (b,a%b);} ll EX_GCD (ll a,ll b,ll& d,ll& x,ll& y) {if (!b) {d=a;x=1;y=0;} ELSE{EX_GCD (b,a%b,d,y,x); y-=x* (A/b);}} void Solve () {ll a=floor (double) x/(double) k); ll B=ceil (double) x/(double) k); ll G=GCD (A, b); ll D=g,tx,ty;a/=g;b/=g;ex _GCD (a,b,d,tx,ty);p rintf ("%lld%lld\n", tx*x/g,ty*x/g);} int main () {int t;scanf ("%d", &t), while (t--) {scanf ("%lld%lld", &x,&k); solve ();} return 0;}



Uva 10673-play with Floor and ceil (extended Euclidean)

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.