代码开头通用模板

本文介绍了代码开头的通用模板,强调了使用对拍、datamaker以及为OOP课程准备的头文件在编程中的重要性。同时,探讨了如何通过特定的函数如add、mul、sub来减少错误并优化常数,以提升代码质量。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

代码开头通用模板

#include<bits/stdc++.h>
using namespace std;
#define PB push_back
#define lowbit(x) (x&(-x))
#define MP make_pair
#define fi first
#define se second
#define ls(x) (x << 1)
#define rs(x) ((x << 1) | 1)
#define rep(i,l,r) for (int i = l ; i <= r ; i++)
#define down(i,r,l) for (int i = r ; i >= l ; i--)
#define fore(i,x) for (int i = head[x] ; i ; i = e[i].next)
#define SZ(v) (int)v.size()

typedef long long ll;
typedef pair <int,int> pr;
const int maxn = 1e6 + 10;

int main(){
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);

}
对拍
#include<bits/stdc++.h>
#include<ctime>
using namespace std;

double times,times2;
int main(){

	int t = 0;
	while ( true ){
		system("datamaker.exe");
		times = clock();
		system("1252D.exe < input.txt > 1.out"); //linux need modification
		times = clock() - times;	
		times2 = clock();
		system("std.exe < input.txt > 2.out");
		times2 = clock() - times2;
		if ( system("fc 1.out 2.out > log.txt") ){ //linux use : "diff" instead of "fc"
			system("pause");
			break;
		}	
		cout<<"correct "<<++t<<endl;
		cout<<times / 1000<<" "<<times2 / 1000<<endl;

		}
	return 0;
}

datamaker
#include<bits/stdc++.h>
using namespace std;
#define PB push_back
#define lowbit(x) (x&(-x))
#define MP make_pair
#define fi first
#define se second
#define ls(x) (x << 1)
#define rs(x) ((x << 1) | 1)
#define rep(i,l,r) for (int i = l ; i <= r ; i++)
#define down(i,r,l) for (int i = r ; i >= l ; i--)
#define fore(i,x) for (int i = head[x] ; i ; i = e[i].next)
#define SZ(v) (int)v.size()

typedef long long ll;
typedef pair <int,int> pr;
const int maxn = 500 + 10;
const int ninf = -2147483648;

int main(){
	freopen("1.cnt","r",stdin);
	int cnt;
	scanf("%d",&cnt);
	fclose(stdin);
	freopen("1.cnt","w",stdout);
	cout<<++cnt;
	fclose(stdout);
	srand(cnt + time(0));
	freopen("input.txt","w",stdout);
	
	ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);

	return 0;
}

为OOP课程准备的头文件
#include <vector>
#include <stack>
#include <iostream>
#include <fstream>
#include <unordered_map>
#include <unordered_set>
#include <map>
#include <iomanip>
#include <set>
#include <functional>
#include <algorithm>
#include <numeric>
#include <cassert>
#include <cmath>
#include <string>
#include <queue>
#include <array>
#include <bitset>
数学、计数题

虽然用add,mul,sub函数很烦。但是可以避免很多取模错误、也可以优化常数。
所以应该渐渐学会这样减少错误的代码风格

//NOTES: 任意乘法需要用mul,或者强制用long long。
//注意取模
//====================================basic operation===============================
inline void add(int &x, int y) {
      x += y;
      if (x >= mod) {
          x -= mod;
      } 
}

inline void sub(int &x, int y) {
      x -= y;
      if (x < 0) {
          x += mod;
      }
}

inline int mul(int x, int y) {
      return (int) ((long long) x * y % mod);
}

inline int power(int x, int y) {
    int res = 1;
    while (y) {
    if (y & 1) {
        res = mul(res, x);
    }
    x = mul(x, x);
    y >>= 1;
  }
  return res;
}

inline int inv(int a) {
    int b = mod, u = 0, v = 1;
    while (a) {
        int t = b / a;
        b -= t * a;
        swap(a, b);
        u -= t * v;
        swap(u, v);
    }
    if (u < 0) {
        u += mod;
    }
    return u;
}
//=======================================================================================

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值