UVA 10548 - Find the Right Changes(数论)

UVA 10548 - Find the Right Changes

题目链接

题意:给定a,b,c,表示货物的价值,求由A货物和B货物组成C货物有几种方法,判断有无解和是否有无限多种

思路:扩展欧几里得求通解,去计算上限和下限就能判断

代码:

#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));
    else
	up = 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));
    else
	down = 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;
}

UVA 10548 - Find the Right Changes(数论),布布扣,bubuko.com

时间: 2024-11-03 01:21:51

UVA 10548 - Find the Right Changes(数论)的相关文章

UVA 10791 Minimum Sum LCM (数论)

LCM (Least Common Multiple) of a set of integers is defined as the minimum number, which is a multiple of all integers of that set. It is interesting to note that any positive integer can be expressed as the LCM of a set of positive integers. For exa

UVA 10375 Choose and divide(数论)

The binomial coefficient C(m,n) is defined as m! C(m,n) = -------- n!(m-n)! Given four natural numbers p, q, r, and s, compute the the result of dividing C(p,q) by C(r,s). The Input Input consists of a sequence of lines. Each line contains four non-n

uva 10548 - Find the Right Changes(拓展欧几里得)

题目链接:uva 10548 - Find the Right Changes 题目大意:给定A,B,C,求x,y,使得xA+yB=C,求有多少种解. 解题思路:拓展欧几里得,保证x,y均大于等于0,确定通解中t的取值. #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; typedef long long ll; co

UVA 11246 - K-Multiple Free set(数论推理)

UVA 11246 - K-Multiple Free set 题目链接 题意:一个{1..n}的集合,求一个子集合,使得元素个数最多,并且不存在有两个元素x1 * k = x2,求出最多的元素个数是多少 思路:推理一下, 一开始n个 先要删除k倍的,删除为{k, 2k, 3k, 4k, 5k, 6k...},会删掉多余的k^2,因此在加回k^2倍的数 然后现在集合中会出现情况的只有k^2的倍数,因此对k^2倍的数字看成一个新集合反复做这个操作即可,因此最后答案为n - n / k + n /

uva 10773 - Back to Intermediate Math(数论)

题目链接:uva 10773 - Back to Intermediate Math 题目大意:有一天河,宽d,水流速度v,船速u,问说垂直过河和最快过河的时间差,如果不能过河输出"can't determine". 解题思路:将u的速度分解成水平方向和竖直方向的两个速度,使水平方向速度恰好为v,船即可垂直过河,速度为竖直方向速度. #include <cstdio> #include <cstring> #include <cmath> const

uva 12119 - The Bells are Ringing(数论+枚举)

题目链接:uva 12119 - The Bells are Ringing 题目大意:有三个钟,分别间隔t1,t2,t3秒响一次,0时刻同时响,给定M,问有没又满足的三个数,最小公倍数为M.并且t3-t1<=25 解题思路:因为M为t1,t2,t3的最小公倍数,所以ti一定为M的因子,所以只要枚举因子判断即可. #include <cstdio> #include <cstring> #include <algorithm> using namespace st

UVa 106 - Fermat vs Pythagoras(数论题目)

题目来源:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=3&page=show_problem&problem=42  Fermat vs. Pythagoras  Background Computer generated and assisted proofs and verification occupy a small niche in the realm

UVA - 10791 - Minimum Sum LCM (数论相关!)

题目链接:Minimum Sum LCM UVA - 10791 Minimum Sum LCM Time Limit:3000MS   Memory Limit:Unknown   64bit IO Format:%lld & %llu SubmitStatus Description  Minimum Sum LCM  LCM (Least Common Multiple) of a set of integers is defined as the minimum number, whic

UVA - 138 - Street Numbers (简单数论)

题目传送:UVA - 138 思路:题意好难懂,看了半天,还是搜了题解才搞懂题意,真是给英语跪啦 m在家的位置,而n是最后一个位置,直接输出前10组满足1~m-1的和 == m+1 ~ n的和,这是题意: 然后通过题意可以得到n*(n+1)/2 - m - m*(m-1)/2 = m*(m-1)/2; 化简可得2*m*m = n*(n+1); 然后可以通过枚举n来求得m,看m是否为整数(可由double到int判断),是整数则满足情况 打表代码: #include <cstdio> #incl