_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的多个库顺序可有以下两种:

  1. C++标准库在前,之后是C标准库,再后为其它(如交互库等)。(工程代码中,本cpp所对应的.h文件应置于开头。)
  2. (仅适用于OI)按字典序依次排列。

可以使用#include <foo>绝不使用#include "foo"

如果有多层嵌套#if #endif,#endif后应有对应的注释标识出与其对应的#if

尽量不要适用#define而使用const, typedef, inline

所有预编译命令不应缩进(见下)。

缩进

每个代码块采用2空格缩进。

空格及换行

大括号换行。

需要加空格的地方:

  1. 二元运算符(包括赋值运算符)两侧(,运算符例外,见下);
  2. ,;的右边(如果其不处于行尾);
  3. if, for等控制流关键字与其后的左括号之间 ;
  4. do-while中的whileif-else中的else与其前面的右大括号之间;
  5. 所有左大括号的左侧(根据不换行的策略,左大括号不应处于行首);
  6. ? :的两侧(包括构造函数初始化列表中的:);
  7. 类型中*,&的左侧(如:const int &a, int A(int *&a)。);
  8. 花括号与其内部语句/数组初始化列表之间(如果在同一行);
  9. 常成员函数的const两侧。

一定不能加空格的地方:

  1. 小括号及中括号与其内部的表达式/参数列表之间;
  2. 函数名与左括号之间(包括声明/定义/使用);
  3. 单目运算符(!,-,*,&,~)之后(或自增自减运算符与其操作数之间);
  4. ,;的左侧;
  5. 类型中*,&的右侧;
  6. .,->,::的两侧;
  7. operator与所要重载的运算符之间(运算符与参数列表之间,根据第2条,也不应空格)。

若表达式过长内部可以换行,运算符处于行首(而非行尾);缩进以使表达式对齐为准;换行的优先级较高的子表达式也应加括号以避免误读。

参数列表/初始化列表过长时内部也可换行,逗号处于行尾;缩四空格。

例子:

  1. struct AVeryVeryVeryVeryVeryVeryVeryVeryVeryLongStruct{
  2. int aVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongVariable, d;
  3. AVeryVeryVeryVeryVeryVeryVeryVeryVeryLongStruct(int a, int b, int c, int d)
  4. : aVeryVeryVeryVeryVeryVeryVeryVeryVeryVeryLongVariable(a) {
  5. this->d = b + c * d;
  6. }
  7. };
  8. int main() {
  9. int thisVariableIsToBeLong = 2, thisVariableIsToBeLongerAndLonger = 23;
  10. AVeryVeryVeryVeryVeryVeryVeryVeryVeryLongStruct s(
  11. thisVariableIsToBeLong, thisVariableIsToBeLong,
  12. thisVariableIsToBeLongerAndLonger,
  13. thisVariableIsToBeLongerAndLonger
  14. );
  15. printf("%d\n", s.d);
  16. }

空行

所有#include <foobar>using foo::bar;之间不应空行,之后应空一行。

一系列常量定义的上下应有空行。

函数/结构体定义两侧应有空行(一系列联系密切的模板函数,如min,max,之间可以不空行)。

一系列全局变量定义的上下应有空行。

语句之间可根据其意义酌情空行。

任何位置不能出现连续的两个(或以上)空行。

函数定义

main函数返回值必须为int,return 0不可忽略;

类/结构体传参在大多数情况下不应传值(除非难以避免地产生拷贝,或一些特殊要求),而应传引用。

极其简短的函数可以写作一行(但绝不能超过80字符),此时花括号内部应有空格(空函数体{}除外)。

单个函数的长度不应过长(例如超过100行)。

命名规则

一般情况下应采用驼峰命名法,变量开头小写,函数/类/结构体开头大写。

特例:

  1. main函数;
  2. 变量可以以一个小写字母命名;
  3. 全局数组名可使用1个大写字母+0~2个数字命名,如A, T1,F01
  4. 模板。如readInt, min, max,pow_mod;
  5. 采用对应算法缩写,如KMP, CRT, NTT,CDQ
  6. 常量可以大写字母命名,如N, M
  7. 临时变量可以以下划线开头。

Example Code

  1. #include <algorithm>
  2. #include <cctype>
  3. #include <cstdio>
  4. typedef long long LL;
  5. inline int readInt() {
  6. int ans = 0, c;
  7. while (!isdigit(c = getchar()));
  8. do ans = ans * 10 + c - '0';
  9. while (isdigit(c = getchar()));
  10. return ans;
  11. }
  12. const int mod = 998244353;
  13. const int g = 3;
  14. const int N = 200050;
  15. inline LL pow_mod(LL x, int p) {
  16. LL ans = 1;
  17. for ((p += mod - 1) %= (mod - 1); p; p >>= 1, (x *= x) %= mod)
  18. if (p & 1) (ans *= x) %= mod;
  19. return ans;
  20. }
  21. LL inv[N];
  22. int n;
  23. void NTT(LL *A, int len, int opt) {
  24. for (int i = 1, j = 0; i < len; ++i) {
  25. int k = len;
  26. while (~j & k) j ^= (k >>= 1);
  27. if (i < j) std::swap(A[i], A[j]);
  28. }
  29. for (int h = 2; h <= len; h <<= 1) {
  30. LL wn = pow_mod(g, (mod - 1) / h * opt);
  31. for (int j = 0; j < len; j += h) {
  32. LL w = 1;
  33. for (int i = j; i < j + (h >> 1); ++i) {
  34. LL _tmp1 = A[i], _tmp2 = A[i + (h >> 1)] * w;
  35. A[i] = (_tmp1 + _tmp2) % mod;
  36. A[i + (h >> 1)] = (_tmp1 - _tmp2) % mod;
  37. (w *= wn) %= mod;
  38. }
  39. }
  40. }
  41. if (opt == -1)
  42. for (int i = 0; i < len; ++i)
  43. (A[i] *= -(mod - 1) / len) %= mod;
  44. }
  45. LL F[N], G[N];
  46. LL T1[N * 4], T2[N * 4];
  47. void Conv(LL *A, int n, LL *B, int m) {
  48. int len = 1;
  49. while (len <= n + m) len <<= 1;
  50. for (int i = 0; i < len; ++i)
  51. T1[i] = (i < n ? A[i] : 0);
  52. for (int i = 0; i < len; ++i)
  53. T2[i] = (i < m ? B[i] : 0);
  54. NTT(T1, len, 1);
  55. NTT(T2, len, 1);
  56. for (int i = 0; i < len; ++i)
  57. (T1[i] *= T2[i]) %= mod;
  58. NTT(T1, len, -1);
  59. }
  60. void Solve(int l, int r) {
  61. if (l == r - 1) {
  62. F[l] = (l == 0 ? 1 : F[l] * inv[l] % mod);
  63. return;
  64. }
  65. int mid = (l + r) >> 1;
  66. Solve(l, mid);
  67. Conv(F + l, mid - l, G, r - l);
  68. for (int i = mid; i < r; ++i)
  69. (F[i] += T1[i - l]) %= mod;
  70. Solve(mid, r);
  71. }
  72. int main() {
  73. n = readInt();
  74. inv[1] = 1;
  75. for (int i = 2; i <= n; ++i)
  76. inv[i] = -(mod / i) * inv[mod % i] % mod;
  77. for (int i = 1; i <= n; ++i) {
  78. scanf("%lld", &G[i]);
  79. (G[i] *= i) %= mod;
  80. }
  81. Solve(0, n + 1);
  82. for (int i = 1; i <= n; ++i)
  83. printf("%lld\n", (F[i] + mod) % mod);
  84. return 0;
  85. }

注:此为多项式\(\exp\)模板。

_rqy's Code Style for OI的更多相关文章

  1. Code Style for OI

    Code Style for OI #include #define 尽量少用 #include 能#include <foo>就不#include "foo" #if ...

  2. 与你相遇好幸运,The Moe Node.js Code Style Guide

    The Moe Node.js Code Style Guide  By 一个最萌的开发者 @2016.9.21 >>代码是人来阅读的,格式规范的代码是对编程人员最好的礼物 :) > ...

  3. 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 ...

  4. Java Code Style

    近期困惑于团队成员代码风格迥异,代码质量不可控,作为一名老司机,忧患于后期服务的可维护性,多次一对一的代码Review,耗时耗力不说,效果也不明显.痛定思痛,多次反思之后得出结论:无规矩不成方圆,可靠 ...

  5. 【Idea】idea code style配置eclipse code formatter

    在eclipse中有自动配置code style的功能 ,但是idea上却没有,这个时候需要自己手工配置 1. 在idea中找到Preference->Plugins->Browse re ...

  6. checkstyle.xml Code Style for Eclipse

    1. Code Templates [下载 Code Templates] 打开 Eclipse -> Window -> Preferences -> Java -> Cod ...

  7. Javascript Code Style Guide

    本指南采用的Airbnb发布的基于ES5的JavaScript Code Style. ES5 英文版:https://github.com/airbnb/javascript/tree/es5-de ...

  8. 在IntelliJ IDEA中配置Google Java Code Style及代码格式化快捷键

    google-java-format plugin should intercept the “Reformat Code” action in IDEA (Ctrl+Alt+L) and apply ...

  9. intelij IDEA设置goole code style风格

    1.安装google-java-format 插件      file ->Setings... ->pligins     输入上诉插件安装 2.下载IntelliJ Java Goog ...

随机推荐

  1. java后端树形菜单使用递归方法

    数据库的设计 使用ssm 实体类 mapper映射文件查询出所有的菜单 使用递归方法

  2. SQLServer 在Visual Studio的2种连接方法

    一.Sql Server 在Visual Studio的连接有两种方法: (1)本地计算机连接; string s = "Data Source=计算机名称;initial Catalog= ...

  3. iOS开发-实现相机app的方法[转载自官方]

    This brief code example to illustrates how you can capture video and convert the frames you get to U ...

  4. linux(乌班图)下执行pip没有问题,执行sudo pip报错的问题

    最近刚装好linux的虚拟机,在装一个套件时提示权限不足,于是添加上了 sudo 命令,结果直接报以下错误, Traceback (most recent call last): File " ...

  5. typescript-koa-postgresql 实现一个简单的rest风格服务器 —— typescript 开发环境配置

    最近需要用 nodeJS 写一个后台程序,为了能够获得 IDE 的更多代码提示,决定用 typescript 来编写,随便也学习下 ts,在这记录下实现过程. 1.新建文件夹 typescript-k ...

  6. Java代码操作HDFS测试类

    1.Java代码操作HDFS需要用到Jar包和Java类 Jar包: hadoop-common-2.6.0.jar和hadoop-hdfs-2.6.0.jar Java类: java.net.URL ...

  7. Elasticsearch聚合 Date Histogram聚合

    转 http://www.cnblogs.com/xing901022/p/4951603.html Elasticsearch的聚合主要分成两大类:metric和bucket,2.0中新增了pipe ...

  8. JavaSE-异常

    1.try catch finally 异常捕获 public class ExceptionTest { public static void main(String[] args) { int a ...

  9. vuejs之Vue Devtools

    Vue Devtools大法好 这是一篇小白friendly教程 Vue Devtools是一款谷歌浏览器插件,专门为调试vue而设计.假设你做了一个vue应用,当你在调试的过程中,打开的控制台是这样 ...

  10. 前端通信:ajax设计方案(七)--- 增加请求错误监控、前端负载均衡以、请求宕机切换以及迭代问题修复

    距离上个迭代过了很长时间,中间经历了很多事情,也在每个空余时间构思了这个迭代的东西以及下个迭代要做的东西.时间周期稍微长了,望见谅. 而且,至今这个开源库的start也已经到了165个了,会支持关注和 ...