cf Educational Codeforces Round 77 C. Infinite Fence

探讨在一个无限长的篱笆上,根据特定规则涂色,分析是否会连续出现相同颜色的篱笆段超过设定值k。通过数学算法和编程实现,解决这一复杂问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

原题:
C. Infinite Fence
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are a rebel leader and you are planning to start a revolution in your country. But the evil Government found out about your plans and set your punishment in the form of correctional labor.

You must paint a fence which consists of 10100 planks in two colors in the following way (suppose planks are numbered from left to right from 0):

if the index of the plank is divisible by r (such planks have indices 0, r, 2r and so on) then you must paint it red;
if the index of the plank is divisible by b (such planks have indices 0, b, 2b and so on) then you must paint it blue;
if the index is divisible both by r and b you can choose the color to paint the plank;
otherwise, you don’t need to paint the plank at all (and it is forbidden to spent paint on it).
Furthermore, the Government added one additional restriction to make your punishment worse. Let’s list all painted planks of the fence in ascending order: if there are k consecutive planks with the same color in this list, then the Government will state that you failed the labor and execute you immediately. If you don’t paint the fence according to the four aforementioned conditions, you will also be executed.

The question is: will you be able to accomplish the labor (the time is not important) or the execution is unavoidable and you need to escape at all costs.

Input
The first line contains single integer T (1≤T≤1000) — the number of test cases.

The next T lines contain descriptions of test cases — one per line. Each test case contains three integers r, b, k (1≤r,b≤10^9, 2≤k≤10 ^9) — the corresponding coefficients.

Output
Print T words — one per line. For each test case print REBEL (case insensitive) if the execution is unavoidable or OBEY (case insensitive) otherwise.

Example
input
4
1 1 2
2 10 4
5 2 3
3 2 2
output
OBEY
REBEL
OBEY
OBEY

中文:

有一个无限长的篱笆,给你两个数r和b,每次可以在0,r,2r,… 以及 0,b,2b,…的篱笆段上涂上红色活蓝色,如果第x段,xr等于xb,那么当前这段篱笆涂r或者涂b任意,现在问你是否会出现连续k个颜色相同的篱笆。

代码:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<vector>
#include<algorithm>
#include<stack>
#include<queue>
#include<memory>
#include<set>
#include<list>
#include<cstring>
#include <unordered_map>
#include<map>
#include<sstream>
#include<iterator>
#include<fstream>
using namespace std;
 
typedef long long ll;
typedef pair<int, int> pii;
 
const int maxn = 1e6 + 1;
ll gcd(ll a, ll b) { return b == 0 ? a : gcd(b, a%b); }
 

 
ll quick(ll a, ll b, ll c)//快速幂取模 
{
	ll ans = 1;
	a %= c;
	while (b)
	{
		if (b & 1) ans = ans*a%c;
		a = a*a%c;
		b >>= 1;
	}
	return ans%c;
}
 
ll divi(ll a, ll b, ll p)
{
	b = quick(b, p - 2, p); //b的逆元 
 
	return a*b%p;
}
 
int main()
{
	ios::sync_with_stdio(false);
 
	int n;
	ll a, b,k;
	cin >> n;
	while (n--)
	{
		cin >> a >> b >> k;
		if (a > b)
			swap(a, b);
		ll c = gcd(a, b);
		if ((b - 1 - c) / a + 1 >= k)
			cout << "REBEL" << endl;
		else
			cout << "OBEY" << endl;
	}
	return 0;
}

思路:

假设b大于a,那么只要找出区间(bx,b(x+1))(bx,b(x+1))(bx,b(x+1))之间最多个a的倍数n是否大于k即可。

要想在区间(bx,b(x+1))(bx,b(x+1))(bx,b(x+1))中塞入最多的a的倍数ayayay,如下列序列

bx,ay,a(y+1),a(y+2),...a(y+n),b(x+1) bx,ay,a(y+1),a(y+2),...a(y+n),b(x+1) bx,ay,a(y+1),a(y+2),...a(y+n),b(x+1)

现在只要让ay−bxay-bxaybx的值最小,然后判断n+1和k的关系就是答案

ay−bx=cay-bx=caybx=c,若要使c最小,那么就相当于计算a和b的最大公约数(裴蜀定理)

由此可以得到c=gcd(a,b)c=gcd(a,b)c=gcd(a,b)

那么,在区间(bx,b(x+1))(bx,b(x+1))(bx,b(x+1))中(注意是两个开区间),整数的个数是b−1b-1b1

第一个a的倍数ayayay的位置应该是bx+cbx+cbx+c

所以最后有多少个a的倍数为

(b−1−c)/a+1(b - 1 - c) / a + 1(b1c)/a+1

### Codeforces Round 927 Div. 3 比赛详情 Codeforces是一个面向全球程序员的比赛平台,定期举办不同级别的编程竞赛。Div. 3系列比赛专为评级较低的选手设计,旨在提供更简单的问题让新手能够参与并提升技能[^1]。 #### 参赛规则概述 这类赛事通常允许单人参加,在规定时间内解决尽可能多的问题来获得分数。评分机制基于解决问题的速度以及提交答案的成功率。比赛中可能会有预测试案例用于即时反馈,而最终得分取决于系统测试的结果。此外,还存在反作弊措施以确保公平竞争环境。 ### 题目解析:Moving Platforms (G) 在这道题中,给定一系列移动平台的位置和速度向量,询问某时刻这些平台是否会形成一条连续路径使得可以从最左端到达最右端。此问题涉及到几何学中的线段交集判断和平面直角坐标系内的相对运动分析。 为了处理这个问题,可以采用如下方法: - **输入数据结构化**:读取所有平台的数据,并将其存储在一个合适的数据结构里以便后续操作。 - **时间轴离散化**:考虑到浮点数精度误差可能导致计算错误,应该把整个过程划分成若干个小的时间间隔来进行模拟仿真。 - **碰撞检测算法实现**:编写函数用来判定任意两个矩形之间是否存在重叠区域;当发现新的连接关系时更新可达性矩阵。 - **连通分量查找技术应用**:利用图论知识快速求解当前状态下哪些节点属于同一个集合内——即能否通过其他成员间接相连。 最后输出结果前记得考虑边界条件! ```cpp // 假设已经定义好了必要的类和辅助功能... bool canReachEnd(vector<Platform>& platforms, double endTime){ // 初始化工作... for(double currentTime = startTime; currentTime <= endTime ;currentTime += deltaT){ updatePositions(platforms, currentTime); buildAdjacencyMatrix(platforms); if(isConnected(startNode,endNode)){ return true; } } return false; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值