小白月赛 F.草方块与牛排(构造)

2023-12-01 15:20

本文主要是介绍小白月赛 F.草方块与牛排(构造),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

小白月赛 F.草方块与牛排(构造)

显然 r = n 2 − 4 4 = n 2 4 − 1 r=\dfrac{n^2-4}{4}=\dfrac{n^2}{4}-1 r=4n24=4n21

使用的个数 r r r为整数, n n n必须为偶数。

将行按照奇偶染色(第一行染0,第二行染1…)。那么牛排的类型只能为3个1、1个0 或者是3个0,1个1。

因为0和1的个数相等。

3 x + y = 3 y + x 3x+y=3y+x 3x+y=3y+x,所以 x = y x=y x=y

r = 2 x r=2x r=2x r r r为偶数。

假设 n = 4 k n=4k n=4k,那么 r = 4 k 2 − 1 r=4k^2-1 r=4k21为奇数,矛盾。

因此 n = 4 k + 2 n=4k+2 n=4k+2型。

在这里插入图片描述

// Problem: 草方块与牛排
// Contest: NowCoder
// URL: https://ac.nowcoder.com/acm/contest/41173/F
// Memory Limit: 524288 MB
// Time Limit: 2000 ms
// 
// Powered by CP Editor (https://cpeditor.org)#include<bits/stdc++.h>
using namespace std;
#define lowbit(x) ((x)&(-(x)))
#define lg2(x) ((int)(__lg(x)/__lg(2)))
typedef long long LL;
typedef unsigned long long ull;
const int N=1e6+10,INF=2e9+10;
inline int read(){int ret=0,f=1; char ch=getchar();while(ch<'0'||ch>'9'){ if(ch=='-') f=-f; ch=getchar(); }while(ch>='0'&&ch<='9') ret=ret*10+ch-'0',ch=getchar();return ret*f;
}
inline void write(int x){        if(x<0){ putchar('-'); x=-x; }      if(x>9) write(x/10);     putchar(x%10+'0');     
}
int n;
inline int get(int x,int y){return (x-1)*n+y;
}
void print1(int x,int y){write(get(x,y)),putchar(' ');write(get(x,y+1)),putchar(' ');write(get(x,y+2)),putchar(' ');write(get(x+1,y)),putchar('\n');write(get(x+1,y+3)),putchar(' ');write(get(x+1,y+2)),putchar(' ');write(get(x+1,y+1)),putchar(' ');write(get(x,y+3)),putchar('\n');
}
void print2(int x,int y){write(get(x,y)),putchar(' ');write(get(x+1,y)),putchar(' ');write(get(x+2,y)),putchar(' ');write(get(x,y+1)),putchar('\n');write(get(x+3,y+1)),putchar(' ');write(get(x+2,y+1)),putchar(' ');write(get(x+1,y+1)),putchar(' ');write(get(x+3,y)),putchar('\n');
}
int main(){n=read();if(n%4==2){write((n*n-4)/4),putchar('\n');for(int j=3;j<=n;j+=4)for(int i=1;i<=n;i+=2)print1(i,j);for(int i=3;i<=n;i+=4) print2(i,1);}else write(-1);return 0;
}

这篇关于小白月赛 F.草方块与牛排(构造)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/441681

相关文章

leetcode105 从前序与中序遍历序列构造二叉树

根据一棵树的前序遍历与中序遍历构造二叉树。 注意: 你可以假设树中没有重复的元素。 例如,给出 前序遍历 preorder = [3,9,20,15,7]中序遍历 inorder = [9,3,15,20,7] 返回如下的二叉树: 3/ \9 20/ \15 7   class Solution {public TreeNode buildTree(int[] pr

C++实现俄罗斯方块(Windows控制台版)

C++实现俄罗斯方块(Windows控制台版) 在油管上看到一个使用C++控制台编写的俄罗斯方块小游戏,源代码200多行,B站上也有相关的讲解视频,非常不错,值得学习。 B站讲解视频地址为:【百万好评】国外技术大神C++游戏编程实战教程,油管580W收藏,新手10小时入门,并快速达到游戏开发能力(中英字幕) B站 CSDN博主千帐灯无此声还为此写了一篇博客:C++实现俄罗斯方块(源码+详解),讲

牛客小白月赛100部分题解

比赛地址:牛客小白月赛100_ACM/NOI/CSP/CCPC/ICPC算法编程高难度练习赛_牛客竞赛OJ A.ACM中的A题 #include<bits/stdc++.h>using namespace std;#define ll long long#define ull = unsigned long longvoid solve() {ll a,b,c;cin>>a>>b>

C++中类的构造函数调用顺序

当建立一个对象时,首先调用基类的构造函数,然后调用下一个派生类的 构造函数,依次类推,直至到达派生类次数最多的派生次数最多的类的构造函数为止。 简而言之,对象是由“底层向上”开始构造的。因为,构造函数一开始构造时,总是 要调用它的基类的构造函数,然后才开始执行其构造函数体,调用直接基类构造函数时, 如果无专门说明,就调用直接基类的默认构造函数。在对象析构时,其顺序正好相反。

牛客小白月赛100(A,B,C,D,E,F三元环计数)

比赛链接 官方讲解 这场比较简单,ABC都很签到,D是个不太裸需要预处理的 B F S BFS BFS 搜索,E是调和级数暴力枚举,F是三元环计数。三元环考的比较少,没见过可能会偏难。 A ACM中的A题 思路: 就是枚举每个边变成原来的两倍,然后看看两短边之和是否大于第三边即可。 不能只给最短边乘 2 2 2,比如 1 4 8 这组数据,也不能只给第二短边乘 2 2 2,比

Java构造和解析Json数据的两种方法(json-lib构造和解析Json数据, org.json构造和解析Json数据)

在www.json.org上公布了很多JAVA下的json构造和解析工具,其中org.json和json-lib比较简单,两者使用上差不多但还是有些区别。下面首先介绍用json-lib构造和解析Json数据的方法示例。 一、介绍       JSON-lib包是一个beans,collections,maps,java arrays 和XML和JSON互相转换的包,主要就是用来解析Json

CF #278 (Div. 2) B.(暴力枚举+推导公式+数学构造)

B. Candy Boxes time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output 题目链接: http://codeforces.com/contest/488/problem/B There

MemSQL Start[c]UP 2.0 - Round 1A(构造)

题目链接:http://codeforces.com/problemset/problem/452/A 解题思路: 打个表暴力查找匹配。 完整代码: #include <algorithm>#include <iostream>#include <cstring>#include <complex>#include <cstdio>#include <strin

Codeforces Round #281 (Div. 2)A(构造+暴力模拟)

题目链接:http://codeforces.com/problemset/problem/493/A 解题思路: 暴力的判断,分三种情况去判断即可。注意如果之前已经被罚下场后,那么在后面的罚下情况不应该算在输出结果内。 完整代码: #include <algorithm>#include <iostream>#include <cstring>#include <co

Codeforces Round #233 (Div. 2)A(构造)

题目链接:http://codeforces.com/contest/399/problem/A 解题思路: 构造出来即可,考虑p-k和p+k两个边界分别于1和n作比较,对左右符号特殊处理。 完整代码: #include <algorithm>#include <iostream>#include <cstring>#include <complex>#include