Ultraviolet A 10548-find the right changes (number theory)

Source: Internet
Author: User
Ultraviolet A 10548-find the right changes

Question Link

Given A, B, and C, it indicates the value of the goods. There are several methods to determine whether there are unsolvable and unlimited types of goods A, B, and C.

Train of Thought: Extend Euclidean for a general solution, and calculate the upper limit and lower limit to determine


#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>using namespace std;const long long INF = 10000000000000000LL;int t;long long a, b, c;long long exgcd(long long a, long long b, long long &x, long long &y) {    if (!b) {x = 1; y = 0; return a;}    long long d = exgcd(b, a % b, y, x);    y -= a / b * x;    return d;}long long solve() {    long long x, y, d;    d = exgcd(a, b, x, y);    if (c % d) return 0;    long long down = -INF, up = INF;    if (b / d > 0)down = max(down, (long long)ceil((-x * 1.0 * c) / b));    elseup = min(up, (long long)floor((-x * 1.0 * c) / b));    if (a / d > 0)up = min(up, (long long )floor((y * 1.0 * c) / a));    elsedown = max(down, (long long)ceil((y * 1.0 * c) / a));    if (up == INF || down == -INF) return -1;    if (down <= up) return up - down + 1;    return 0;}int main() {    scanf("%d", &t);    while (t--) {scanf("%lld%lld%lld", &a, &b, &c);long long ans = solve();if (ans == 0) printf("Impossible\n");else if (ans < 0) printf("Infinitely many solutions\n");else printf("%lld\n", ans);    }    return 0;}

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.