[SNCPC2019] K-hour Clock
题面翻译
给定三个数 x , y , z x,y,z x,y,z,求一个数 k k k 使得 x + y ≡ z ( m o d k ) x+y\equiv z\pmod k x+y≡z(modk)。
题目描述
A k k k-hour clock is a day keeping method which follows the rules below:
- A day is divided into k k k hours, where the i i i-th hour is called the ( i − 1 ) (i-1) (i−1) o’ clock;
- If it’s x x x o’clock now, it will be ( x + 1 ) (x+1) (x+1) o’clock after 1 1 1 hour if 0 ≤ x < k − 1 0 \le x < k - 1 0≤x<k−1;
- If it’s ( k − 1 ) (k - 1) (k−1) o’clock now, it will be 0 0 0 o’clock after 1 1 1 hour.
We know that it’s x x x o’clock now, and after y y y hours it will be z z z o’clock. What’s the value of k k k?
输入格式
There are multiple test cases. The first line of the input is an integer T T T (about 1 0 5 10^5 105), indicating the number of test cases. For each test case:
The first and only line contains three integers x x x, y y y and z z z ( 0 ≤ x , z ≤ 1 0 9 0 \le x, z \le 10^9 0≤x,z≤109, 1 ≤ y ≤ 1 0 9 1 \le y \le 10^9 1≤y≤109).
输出格式
For each test case output one line containing one integer, indicating the value of
k
k
k. Note that there must be
1
≤
k
≤
2
×
1
0
9
1 \le k \le 2 \times 10^9
1≤k≤2×109. If there are multiple valid answers, you can print any of them; If there is no valid answer, print -1
(without quotes) instead.
样例 #1
样例输入 #1
4
11 18 5
3 49 4
1 9 1
1 3 10
样例输出 #1
12
24
3
-1
解析
问题转换为 a+b和c同余k,所以我们分类讨论
1.a+b=c的时候,取k为c+1即可
2.a+b不等于c,特判 k 必须大于a和大于c,否则输出 a+b-c即可
#include<bits/stdc++.h>
using namespace std;
#define int long long
const int N=2e5+5;
//int n,a[N];
void solve(){
int a,b,c;
scanf("%lld%lld%lld",&a,&b,&c);
if(a+b==c) printf("%lld\n",c+1);
else{
int k=a+b-c;
if(a>=k||c>=k) puts("-1");
else printf("%lld\n",k);
}
}
signed main(){
int t=1;
scanf("%lld",&t);
while(t--) solve();
return 0;
}