cf Educational Codeforces Round 77 C. Infinite Fence

原题:
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,那么只要找出区间 ( b x , b ( x + 1 ) ) (bx,b(x+1)) (bx,b(x+1))之间最多个a的倍数n是否大于k即可。

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

b x , a y , 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)

现在只要让 a y − b x ay-bx aybx的值最小,然后判断n+1和k的关系就是答案

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

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

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

第一个a的倍数 a y ay ay的位置应该是 b x + c bx+c bx+c

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

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值