Codeforces Round 65 C. Round Table Knights(71)

https://codeforces.com/contest/71/problem/C

这道题,比较难,现在还没有完全吃透,参考别人的代码,大概意思

1)将圆桌n个骑士,分成 i (>3) 部分,每一份 step 个骑士。

2)然后每一部分执行step步,寻找是否=0,如果最后一个是0,那么结果就是0

注意:代码还没有完全理解,我参考别人的代码,理解到这里,还是不完全理解,暂时存放这里,方便手机查看和再思考。

C. Round Table Knights

代码

分析代码

#include <iostream>

using namespace std;

int main()

{

int n;

bool mood_good = true;

int step = 0;

int *list;

cin >> n;

list=new intn;

for (int i = 0; i < n; i++)

{

cin >> listi;

}

for (int i = 3; i <= n; i++)

{

//You must make N elements split i equally into parts. 将元素分i份。

if (n % i == 0)

{

//cout << "n%i= "<<n<<" % "<<i<<" = "<< n%i << endl;

step = n / i;//每一份step个

//cout <<"i="<< i << ",step= " << step << endl;

//every part: to check

for (int start = 0; start < step; start++)

{ //然后在每份的元素里操作

mood_good = true;

//cout << "start=" << start << endl;

// i is i parts

//这里是每份的元素。

for (int k = 0; k < i; k++)

{

int pos = start + k * step;

//cout << "listpos("\<\=" << listpos << endl;

if (pos >= n)

{

pos -= n;

//cout << "restart begin pos=" << pos << endl;

}

if (1!=listpos)

{

//cout << "bad" << endl;

mood_good = false;

}

}

if (true == mood_good)

{

cout <<"YES"<<endl;

i = n + 10000; //to exit

break;

}

}

}

}

if (mood_good == false)

{

cout << "NO" << endl;

}

delete\[\] list;

return 0;

}

cpp 复制代码
/*
ref code:DhvanitVaghani

#include <iostream>
using namespace std;

int main() {
int n;
cin >> n;
int a[n];
for (int i = 0; i < n; i++) cin >> a[i];

for (int d = 3; d <= n; d++) {
if (n % d == 0) {
int step = n / d;
for (int start = 0; start < step; start++) {
bool ok = true;
for (int i = 0; i < d; i++) {
int pos = start + i * step;
if (pos >= n) pos -= n;
if (a[pos] != 1) ok = false;
}
if (ok) {
cout << "YES\n";
return 0;
}
}
}
}
cout << "NO\n";
return 0;
}
*/
#include <iostream>
using namespace std;

int main() 
{
	int n;
	bool mood_good = true;
	int step = 0;
	int *list;
	
	
	cin >> n;

	list=new int[n];
	
	for (int i = 0; i < n; i++)
	{
		cin >> list[i];
	}

	for (int i = 3; i <= n; i++) 
	{
		//You must make N elements split i equally into parts.
		if (n % i == 0) 
		{ 
			//cout << "n%i= "<<n<<" % "<<i<<" = "<< n%i << endl;
			step = n / i;
			
			//cout <<"i="<< i << ",step= " << step << endl;

			//every part: to check
			for (int start = 0; start < step; start++) 
			{
				mood_good = true;
				//cout << "start=" << start << endl;
				// i is i parts
				for (int k = 0; k < i; k++) 
				{
					int pos = start + k * step;
					//cout << "list[pos("<<pos<<")]=" << list[pos] << endl;

					if (pos >= n)
					{
						pos -= n;
						//cout << "restart begin pos=" << pos << endl;
					}
					
					if (1!=list[pos])
					{
						//cout << "bad" << endl;
						mood_good = false;
					}
				}

				if (true == mood_good)
				{
					cout <<"YES"<<endl;
					i = n + 10000; //to exit
					break;
				}
			}
		}
	}
	if (mood_good == false)
	{
		cout << "NO" << endl;
	}
	delete[] list;
	return 0;
}

/*
Examples
InputCopy
3
1 1 1
OutputCopy
YES
InputCopy
6
1 0 1 1 1 0
OutputCopy
YES
InputCopy
6
1 0 0 1 0 1
OutputCopy
NO


*/
相关推荐
灯澜忆梦18 分钟前
GO_面向对象_方法
开发语言·golang
布朗克16825 分钟前
Go 入门到精通-16-字符串深入
开发语言·后端·golang·字符串
北冥you鱼36 分钟前
Go flag 包详解:从命令行解析到实战应用
开发语言·后端·golang
大鱼>41 分钟前
多宠物家庭智能管理平台:云端架构与多设备协同实战
python·算法·iot·宠物
炸膛坦客1 小时前
单片机/C/C++八股:(二十四)编译文件( .bin 和 .hex ,包括 .elf 和 .axf )
c语言·c++·单片机
To_OC1 小时前
LC 22 括号生成:刷完这道题,我终于搞懂回溯剪枝了
javascript·算法·leetcode
web守墓人1 小时前
【python】uv解决依赖安装缓慢的问题
开发语言·python·uv
To_OC1 小时前
LC 39 组合总和:回溯入门必刷题,我踩过的两个坑都在这了
javascript·算法·leetcode
数字杂技师2 小时前
每条推荐都很准,为什么我还是越刷越无聊?
算法
兰令水2 小时前
hot100【acm版】【2026.7.14打卡-java版本】
java·数据结构·算法·leetcode·面试