CF1560B Who's Opposite?

题意

2k2k 个人排成一个圈,编号依次为 1,2,3,,2k1,2,3,…,2k,每个人可以透过圆心看到对面的人。

如在下图中,11 可以看到 4455 可以看到 22

图片

已知 aa 可以看到 bb,问 cc 可以看到谁?若无解输出 1-1

分析

分析题目的例子可以发现,14=3=k,52=3=k|1-4|=3=k,|5-2|=3=k,也就是说 ab=k|a-b|=k,这样我们就可以求出圈内人的最大编号 2k2k,如果 a,b,ca,b,c 中某一个大于 2k2k,显然无解。

若有解,则 cc 可以看到的人是 c+kc+k,如果溢出就对 2k2k 取模。

注意一下细节。

代码

#include<bits/stdc++.h>
using namespace std;
int read(){
	int x=0,f=1;char ch=getchar();
	while (!isdigit(ch)){if (ch=='-') f=-1;ch=getchar();}
	while (isdigit(ch)){x=x*10+ch-48;ch=getchar();}
	return x*f;
}
int t,a,b,c,k;
int main()
{
    t=read();
    while(t--){
    	a=read();b=read();c=read();
    	k=abs(a-b);
    	if(a>2*k||b>2*k||c>2*k){//无解
    		cout<<-1<<endl;
    		continue;
		}
    	int ans=c+k;
    	if(c+k>2*k)//溢出就取模
    		ans%=2*k;
    	cout<<ans<<endl;
	}
	return 0;
}
posted @ 2021-08-19 14:00  luckydrawbox  阅读(18)  评论(0)    收藏  举报  来源