One-time indefinite equation of two elements

Source: Internet
Author: User

Definition: A,b,c is an integer, and ab!=0, then the shape of Ax+by=c equation is called two Yuan one indefinite equation

Theorem 1: Set A, B is an integer and d = gcd (A, B) if d|c, then there is an infinite number of integer solutions to the return, otherwise there is no integer solution.

Here we can probably find that two yuan once the indefinite equation and the congruence equation can be converted to each other, for example, under the condition of a>0 and b>0, it is equivalent to finding the integer solution of the one-element linear congruence equation Ax≡c (mod b) ax+by=c integer solution.

Give the POJ 2142 to practice practiced hand

The Balance
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 5225 Accepted: 2297

Description

Ms. Iyo Kiffa-australis have a balance and only kinds of weights to measure a dose of medicine. For example, to measure 200mg of aspirin using 300mg weights and 700mg weights, she can put one 700mg weight on the side O f the medicine and three 300mg weights on the opposite side (Figure 1). Although she could put four 300mg weights on the medicine side and both 700mg weights on the other (Figure 2), she would no T choose this solution because it's less convenient to use more weights.
You is asked to help she by calculating how many weights is required.

Input

The input is a sequence of datasets. A DataSet is a line containing three positive integers a, B, and D separated by a space. The following relations hold:a! = B, a <= 10000, B <= 10000, and D <= 50000. You could assume that it was possible to measure D mg using a combination of a MG and b mg weights. In the other words, you need not consider "no solution" cases.
The end of the input is indicated by a line containing three zeros separated by a space. It is a dataset.

Output

The output should is composed of lines, each corresponding to an input dataset (a, B, D). An output line should contain, nonnegative integers x and y separated by a space. They should satisfy the following three conditions.
    • You can measure DMG using x many AMG weights and y many BMG weights.
    • The total number of weights (x + y) is the smallest among those pairs of nonnegative integers satisfying the previous cond Ition.
    • The total mass of weights (ax + by) is the smallest among those pairs of nonnegative integers satisfying the previous Conditions.

No extra characters (e.g. extra spaces) should appear in the output.

Sample Input

700 300 200500 200 300500 200 500275 110 330275 110 385648 375 40023 1 100000 0 0

Sample Output

1 31 11 00 31 149 743333 1

Source

Japan 2004 solves the problem with the expansion of Euclid.
1#include <cstdio>2#include <cstring>3#include <iostream>4 5 using namespacestd;6 7 intgcdintAintb)8 {9     if(b = =0)returnA;Ten     Else returnGCD (b,a%b); One } A  -  - intEX_GCD (intAintBint&x,int&y) the { -     if(b = =0){ -x =1; -y =0; +         returnA; -     } +     Else { AEX_GCD (b,a%b,x,y); at         intt =x; -x =y; -y = t-a/b*y; -             returnT; -     } - } in  - intMain () to { +     inta,b,c,x,y; -      while(SCANF ("%d%d%d", &a,&b,&c)! =EOF) { the         if(A = =0&& b = =0&& c = =0) Break; *         intD =gcd (A, b); $A/=D;Panax Notoginsengb/=D; -C/=D; the EX_GCD (a,b,x,y); +         intxx = x*C; Axx = (xx%b+b)%b; the         intyy = (c-a*xx)/b; +         if(YY <0) yy =-yy; -y = y*C; $y = (y%a+a)%A; $x = (c-b*y)/A; -         if(X <0) x =-x; -         if(X+y > xx+yy) { thex =xx; -y =yy;Wuyi         } theprintf"%d%d\n", x, y); -     } Wu  -     return 0;
View Code

One-time indefinite equation of two elements

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.