XTUOJ 1189 Root

题目链接

题目描述

求方程 \(\displaystyle\frac{(x+a)}{(x+b)} = c\) 的非负整数根,其中 \(a,b,c\) 都是非负整数。

输入

第一行是一个整数 \(K\),表示样例的个数\((K≤30000)\)
以后的 \(K\) 行每行为以一个样例,包含三个非负整数\(a,b,c (a,b,c ≤ 10^9)\)

输出

每行输出一个样例的结果。如果方程没有根,输出“\(None\)”,否则输出根的值,如果存在多个根,输出最小的那个。

Sample Input

3
1 1 1
1 1 2
3 1 2

Sample Output

0
None
1

题解

\[(x+a)=c(x+b) \]

\[x+a=cx+cb \]

\[a-bc=x(c-1) \]

\[x=\displaystyle \frac{a-bc}{c-1} \]

通过计算 \(x\) 的式子,发现 \(c\neq1\)

则需要对 \(c=1\) 的情况 进行讨论处理:

  • \(a=b=0,c=1:\) 由于原式分母 \(x+b\neq0\) ,因此 \(Min(x)=1\)
  • \(a=b\neq0,c=1:\) \(Min(x)=0\)
  • \(a\neq b,c=1:\) 无解

其他两种 无解 的特殊情况:

  • \(x+b\neq 0:\)\(x=0\)\(b=0\) 时,无解
  • 题目要求 非负整数根,当计算得到的 \(x<0\)\(x\) 不是整数时,无解

其余情况按照 \(x\) 计算式 计算即可

#include <iostream>
#include <cstdio>
#include <cmath>
#include <algorithm>
#define LL __int64
using namespace std;
int solve() {
	LL a, b, c; cin >> a >> b >> c;
	if(a == b && a == 0 && c == 1) return puts("1"), 0;
	if(a == b && c == 1) return puts("0"), 0;
	if(a != b && c == 1) return puts("None"), 0;
	LL ans = (LL)(a - b * c) / (c - 1);
	if(ans == 0 && b == 0) return puts("None"), 0;
	if(ans < 0 || (a - b * c) % (c - 1) != 0) return puts("None"), 0;
	else printf("%lld\n", ans);
	return 0;
}
int main() {
	int T; cin >> T;
	while(T --> 0) solve();
	return 0;
}
/*
(x + a) = c (x + b)
x + a = cx + bc
a - bc = cx - x
a - bc = x(c - 1)
x = (a - bc) / (c - 1) 
*/
posted @ 2021-10-18 15:44  计网君  阅读(85)  评论(0)    收藏  举报