
题目传送门前言你们写的第一个程序是什么我相信这道题是很多人OI之路的开端。可是这道题到底要怎么做第一种蒟蒻写法大多数人都会用这种写法不过实际上这种写法思维难度极高无疑是最优代码在时间、空间等多个方面都远超其他写法#includeiostream using namespace std; int main(){ int a,b; cinab; coutab; return 0; }时间复杂度O(1) 空间复杂度O(1)华丽而朴实的7行就可以解决 -1000000000a,b1000000000 范围内的加法但是这种写法的代码无疑是“臃肿”的。首先看到头文件#includeiostream不不不这无疑是丑陋的……我们引入一个万能头文件几乎涵盖除windows.h等以外的所有头文件。#includebits/stdc.h实际上这种写法已经是大部分人的常态了可是还有缺陷我们在下一只种写法拭目以待第二种输入输出优化上种结尾我们讲到这种写法不够好为什么呢问题出在cin和cout。在我们的C中cin和cout的输入输出会打开一种名为同步流的东西。当你在程序中同时使用文件流和标准输出流并且希望它们输出的内容保持一致就需要同步这些流。或者是你需要程序与多文件并行时也需要这样。但这种行为有种致命缺陷C标准库的输入输出流cin和cout与C标准库printf和scanf是同步的。这意味着当你使用printf或scanf时C的标准流可能会等待C流操作完成反之亦然。这种同步可能会导致性能问题特别是在处理大量输入输出时。如何解决这种问题呢擦亮你们的眼睛因为接下来的内容可能对你们有很大帮助我们可以在主函数中添加一串代码进行优化。#includebits/stdc.h using namespace std; int main(){ ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); int a,b; cinab; coutab; return 0; }唉这时候就有人会问了“这什么ios有什么用啊”用处可大了上文说到过同步流在处理输入输出时会占用大量时间以上代码可以关闭同步流为输入输出节省大量时间可这对AB Problem的极少输入输出又能优化多少呢我们不妨试试更优的写法手写输入输出同过纯字符模拟级手写超快输入输出达到最优效果实战有难度赛时不建议#includebits/stdc.h using namespace std; int a,b; inline void read(int x){ int neg0; x0; char chgetchar_unlocked(); while(ch!-(ch0||ch9)){ chgetchar_unlocked(); } if(ch-){ neg1; chgetchar_unlocked(); } while(ch0ch9){ x(x3)(x1)(ch-0); chgetchar_unlocked(); } if(neg){ x-x; } } inline void write(int x){ if(x0){ putchar(-); x-x; } static int sta[40]; int top0; if(x0){ putchar(0); return; } do{ sta[top]x%10; x/10; }while(x); while(top){ putchar(0sta[--top]); } } int main(){ read(a); read(b); write(ab); return 0; }注意了以上是我的个人快读写法有点与众不同为应对一部分特殊输入相对复杂不太建议直接复制大家也可以自己去找快读模板至此输入输出优化结束。第三种超超超超超超超超超超超超超超超超超超超超超级大巨佬写法题解传送门我的写法通过位运算模拟位级加法手写快读优化#includebits/stdc.h using namespace std; #define int long long int a,b,ans; inline void read(int x){ int neg0; x0; char chgetchar_unlocked(); while(ch!-(ch0||ch9)){ chgetchar_unlocked(); } if(ch-){ neg1; chgetchar_unlocked(); } while(ch0ch9){ x(x3)(x1)(ch-0); chgetchar_unlocked(); } if(neg){ x-x; } } inline void write(int x){ if(x0){ putchar(-); x-x; } static int sta[40]; int top0; if(x0){ putchar(0); return; } do{ sta[top]x%10; x/10; }while(x); while(top){ putchar(0sta[--top]); } } int pluser(int x,int y){ while(y!0){ int ncsx^y; int c(unsigned long long)(xy)1; xncs; yc; } return x; } signed main(){ read(a); read(b); anspluser(a,b); write(ans); return 0; }大家可以自行查看题解区真正大佬的解法开阔眼界我是典狱长糖人关注我带你见遍极致题解