_rqy's Code Style for OI
_rqy's Code Style for OI
Inspired by Menci's Code Style for OI
本文介绍_rqy的OI中的代码规范。其来源主要为_rqy的长期积累及参考Google代码规范、Menci的规范。
可能会update。
概述
#include语句必须置于整个程序的开头。
不应using namespace foo;。若有必要可以using foo::bar;。
单行字符数必须不超过80。
预编译
#include的多个库顺序可有以下两种:
- C++标准库在前,之后是C标准库,再后为其它(如交互库等)。(工程代码中,本
cpp所对应的.h文件应置于开头。) - (仅适用于OI)按字典序依次排列。
可以使用#include <foo>时绝不使用#include "foo"。
如果有多层嵌套#if #endif,#endif后应有对应的注释标识出与其对应的#if。
尽量不要适用#define而使用const, typedef, inline。
所有预编译命令不应缩进(见下)。
缩进
每个代码块采用2空格缩进。
空格及换行
大括号不换行。
需要加空格的地方:
- 二元运算符(包括赋值运算符)两侧(
,运算符例外,见下); ,及;的右边(如果其不处于行尾);if,for等控制流关键字与其后的左括号之间 ;do-while中的while、if-else中的else与其前面的右大括号之间;- 所有左大括号的左侧(根据不换行的策略,左大括号不应处于行首);
?:的两侧(包括构造函数初始化列表中的:);- 类型中
*,&的左侧(如:const int &a,int A(int *&a)。); - 花括号与其内部语句/数组初始化列表之间(如果在同一行);
- 常成员函数的
const两侧。
一定不能加空格的地方:
- 小括号及中括号与其内部的表达式/参数列表之间;
- 函数名与左括号之间(包括声明/定义/使用);
- 单目运算符(
!,-,*,&,~)之后(或自增自减运算符与其操作数之间); ,及;的左侧;- 类型中
*,&的右侧; .,->,::的两侧;operator与所要重载的运算符之间(运算符与参数列表之间,根据第2条,也不应空格)。
若表达式过长内部可以换行,运算符处于行首(而非行尾);缩进以使表达式对齐为准;换行的优先级较高的子表达式也应加括号以避免误读。
参数列表/初始化列表过长时内部也可换行,逗号处于行尾;缩四空格。
例子:
struct AVeryVeryVeryVeryVeryVeryVeryVeryVeryLongStruct{
int aVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongVariable, d;
AVeryVeryVeryVeryVeryVeryVeryVeryVeryLongStruct(int a, int b, int c, int d)
: aVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongVariable(a) {
this->d = b + c * d;
}
};
int main() {
int thisVariableIsToBeLong = 2, thisVariableIsToBeLongerAndLonger = 23;
AVeryVeryVeryVeryVeryVeryVeryVeryVeryLongStruct s(
thisVariableIsToBeLong, thisVariableIsToBeLong,
thisVariableIsToBeLongerAndLonger,
thisVariableIsToBeLongerAndLonger
);
printf("%d\n", s.d);
}
空行
所有#include <foobar>与using foo::bar;之间不应空行,之后应空一行。
一系列常量定义的上下应有空行。
函数/结构体定义两侧应有空行(一系列联系密切的模板函数,如min,max,之间可以不空行)。
一系列全局变量定义的上下应有空行。
语句之间可根据其意义酌情空行。
任何位置不能出现连续的两个(或以上)空行。
函数定义
main函数返回值必须为int,return 0不可忽略;
类/结构体传参在大多数情况下不应传值(除非难以避免地产生拷贝,或一些特殊要求),而应传引用。
极其简短的函数可以写作一行(但绝不能超过80字符),此时花括号内部应有空格(空函数体{}除外)。
单个函数的长度不应过长(例如超过100行)。
命名规则
一般情况下应采用驼峰命名法,变量开头小写,函数/类/结构体开头大写。
特例:
main函数;- 变量可以以一个小写字母命名;
- 全局数组名可使用1个大写字母+0~2个数字命名,如
A,T1,F01; - 模板。如
readInt,min,max,pow_mod; - 采用对应算法缩写,如
KMP,CRT,NTT,CDQ; - 常量可以大写字母命名,如
N,M; - 临时变量可以以下划线开头。
Example Code
#include <algorithm>
#include <cctype>
#include <cstdio>
typedef long long LL;
inline int readInt() {
int ans = 0, c;
while (!isdigit(c = getchar()));
do ans = ans * 10 + c - '0';
while (isdigit(c = getchar()));
return ans;
}
const int mod = 998244353;
const int g = 3;
const int N = 200050;
inline LL pow_mod(LL x, int p) {
LL ans = 1;
for ((p += mod - 1) %= (mod - 1); p; p >>= 1, (x *= x) %= mod)
if (p & 1) (ans *= x) %= mod;
return ans;
}
LL inv[N];
int n;
void NTT(LL *A, int len, int opt) {
for (int i = 1, j = 0; i < len; ++i) {
int k = len;
while (~j & k) j ^= (k >>= 1);
if (i < j) std::swap(A[i], A[j]);
}
for (int h = 2; h <= len; h <<= 1) {
LL wn = pow_mod(g, (mod - 1) / h * opt);
for (int j = 0; j < len; j += h) {
LL w = 1;
for (int i = j; i < j + (h >> 1); ++i) {
LL _tmp1 = A[i], _tmp2 = A[i + (h >> 1)] * w;
A[i] = (_tmp1 + _tmp2) % mod;
A[i + (h >> 1)] = (_tmp1 - _tmp2) % mod;
(w *= wn) %= mod;
}
}
}
if (opt == -1)
for (int i = 0; i < len; ++i)
(A[i] *= -(mod - 1) / len) %= mod;
}
LL F[N], G[N];
LL T1[N * 4], T2[N * 4];
void Conv(LL *A, int n, LL *B, int m) {
int len = 1;
while (len <= n + m) len <<= 1;
for (int i = 0; i < len; ++i)
T1[i] = (i < n ? A[i] : 0);
for (int i = 0; i < len; ++i)
T2[i] = (i < m ? B[i] : 0);
NTT(T1, len, 1);
NTT(T2, len, 1);
for (int i = 0; i < len; ++i)
(T1[i] *= T2[i]) %= mod;
NTT(T1, len, -1);
}
void Solve(int l, int r) {
if (l == r - 1) {
F[l] = (l == 0 ? 1 : F[l] * inv[l] % mod);
return;
}
int mid = (l + r) >> 1;
Solve(l, mid);
Conv(F + l, mid - l, G, r - l);
for (int i = mid; i < r; ++i)
(F[i] += T1[i - l]) %= mod;
Solve(mid, r);
}
int main() {
n = readInt();
inv[1] = 1;
for (int i = 2; i <= n; ++i)
inv[i] = -(mod / i) * inv[mod % i] % mod;
for (int i = 1; i <= n; ++i) {
scanf("%lld", &G[i]);
(G[i] *= i) %= mod;
}
Solve(0, n + 1);
for (int i = 1; i <= n; ++i)
printf("%lld\n", (F[i] + mod) % mod);
return 0;
}
注:此为多项式\(\exp\)模板。
_rqy's Code Style for OI的更多相关文章
- Code Style for OI
Code Style for OI #include #define 尽量少用 #include 能#include <foo>就不#include "foo" #if ...
- 与你相遇好幸运,The Moe Node.js Code Style Guide
The Moe Node.js Code Style Guide By 一个最萌的开发者 @2016.9.21 >>代码是人来阅读的,格式规范的代码是对编程人员最好的礼物 :) > ...
- Eclipse setting Java code style and codetemplate
1.open the eclipse tool window First click the Window menu,then check the children's menu which name ...
- Java Code Style
近期困惑于团队成员代码风格迥异,代码质量不可控,作为一名老司机,忧患于后期服务的可维护性,多次一对一的代码Review,耗时耗力不说,效果也不明显.痛定思痛,多次反思之后得出结论:无规矩不成方圆,可靠 ...
- 【Idea】idea code style配置eclipse code formatter
在eclipse中有自动配置code style的功能 ,但是idea上却没有,这个时候需要自己手工配置 1. 在idea中找到Preference->Plugins->Browse re ...
- checkstyle.xml Code Style for Eclipse
1. Code Templates [下载 Code Templates] 打开 Eclipse -> Window -> Preferences -> Java -> Cod ...
- Javascript Code Style Guide
本指南采用的Airbnb发布的基于ES5的JavaScript Code Style. ES5 英文版:https://github.com/airbnb/javascript/tree/es5-de ...
- 在IntelliJ IDEA中配置Google Java Code Style及代码格式化快捷键
google-java-format plugin should intercept the “Reformat Code” action in IDEA (Ctrl+Alt+L) and apply ...
- intelij IDEA设置goole code style风格
1.安装google-java-format 插件 file ->Setings... ->pligins 输入上诉插件安装 2.下载IntelliJ Java Goog ...
随机推荐
- FTP服务安装与端口说明
FTP服务安装与端口说明 FTP端口修改安装部署windowswindows 2012文件服务 1. FTP服务介绍 1.1 什么是FTP FTP(File Transfer Protocol)是文件 ...
- Python staticmethod classmethod 普通方法 类变量 实例变量 cls self 概念与区别
类变量 1.需要在一个类的各个对象间交互,即需要一个数据对象为整个类而非某个对象服务. 2.同时又力求不破坏类的封装性,即要求此成员隐藏在类的内部,对外不可见. 3.有独立的存储区,属于整个类. ...
- zookeeper 备忘
cd /bin $ ./zkCli.sh ls /a 查看 ls /a true 查看并watch create –e /a 临时节点 create –s /a 顺序节点 create –e –s / ...
- python中@staticmethod与@classmethod
@ 首先这里介绍一下‘@’的作用,‘@’用作函数的修饰符,是python2.4新增的功能,修饰符必须出现在函数定义前一行,不允许和函数定义在同一行.只可以对模块或者类定义的函数进行修饰,不允许修饰一个 ...
- Python -----issubclass和isinstance
issubclass用于判断一个类是否为另一个类的子类,isinstance用于判断一个对象是否某类的一个实例 import math class Point: def __init__(self, ...
- StreamSets学习系列之启动StreamSets时出现Caused by: java.security.AccessControlException: access denied ("java.util.PropertyPermission" "test.to.ensure.security.is.configured.correctly" "read")错误的解决办法
不多说,直接上干货! 问题详情 [hadoop@master streamsets-datacollector-]$ ./bin/streamsets dc Java 1.8 detected; ad ...
- php -- 正则替换
----- 019-regex_replace.php ----- <!DOCTYPE html> <html> <head> <meta http-equi ...
- JavaScript -- History
-----042-History.html----- <!DOCTYPE html> <html> <head> <meta http-equiv=" ...
- wordpress添加面包屑
第一步:在functions.php中添加如下代码 // 面包屑导航 function get_breadcrumbs() { global $wp_query; if ( !is_home() ){ ...
- TensorFlow的梯度裁剪
在较深的网络,如多层CNN或者非常长的RNN,由于求导的链式法则,有可能会出现梯度消失(Gradient Vanishing)或梯度爆炸(Gradient Exploding )的问题. 原理 问题: ...