P9645 [SNCPC2019] K-hour Clock(同余&数学)

[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+yz(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) (i1) 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 0x<k1;
  • If it’s ( k − 1 ) (k - 1) (k1) 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 0x,z109, 1 ≤ y ≤ 1 0 9 1 \le y \le 10^9 1y109).

输出格式

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 1k2×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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值