ACM代碼模板

功能介紹

寫了I/O函數,支持如下幾種方式c++

 1 read(num);    //讀入一個數num(任意整數類型,下同)
 2 read(num1,num2,num3,num4); //讀入任意多個數
 3 read(arr,n); //對一個整數數組arr讀入n個值,[0,n-1]
 4 read(arr,first,last); //對一個整數數組arr讀入區間last-first+1個值,[first,last]
 5 read(s); //讀入一個字符串數組(string和char數組都支持)
 6 print(num); //輸出一個數num
 7 print(num1,num2,num3,num4); //輸出任意多個數,中間用空格隔開
 8 print(arr,n); //對一個整數數組arr輸出n個值,[0,n-1],中間用空格隔開
 9 print(arr,first,last); //對一個整數數組arr輸出last-first+1個值,[first,last],中間用空格隔開
10 //read()有返回值,遇到EOF返回0。多組數據時能夠放心使用while(read(n)){}
11 //至於字符串的輸出沒有進行優化,測試顯示直接cout或者printf比較快

關於define nc getchar,是由於用到了fread,本地調試時候請不要註釋,不然沒法從鍵盤讀入數據,提交時註釋這句話便可。數組

支持如下幾個函數less

 1 gcd(a,b); //求兩數gcd(任意整數類型,下同)
 2 lowbit(x); //求一個整數的lowbit
 3 mishu(x); //判斷一個數是否是2的冪
 4 q_mul(a,b,p); //求(a*b)%p的值,防止溢出,O(logN)
 5 f_mul(a,b,p); //求(a*b)%p的值,防止溢出,O(1),可能丟失精度
 6 q_pow(a,b,p); //求(a^b)%p的值,不防溢出,O(logN)
 7 s_pow(a,b,p); //求(a^b)%p的值,防溢出,O(logN*logN)
 8 ex_gcd(a,b,x,y); //擴展GCD
 9 com(m,n); //求C(m,n)
10 isprime(num); //判斷一個數是否質數

其餘一些宏定義自行查看便可瞭解,大部分的東西我都儘量的進行了優化,以前有個很冗長的,如今修改爲這樣了,基本也就是最終版本。函數

 

 1 //XDDDDDDi
 2 #include <bits/stdc++.h>
 3 using namespace std;
 4 #define PB push_back
 5 #define MT make_tuple
 6 #define MP make_pair
 7 #define pii pair<int,int>
 8 #define pdd pair<double,double>
 9 #define F first
10 #define S second
11 
12 #define MOD 1000000007
13 #define PI (acos(-1.0))
14 #define EPS 1e-6
15 #define MMT(s,a) memset(s, a, sizeof s)
16 #define GO(i,a,b) for(int i = (a); i < (b); ++i)
17 #define GOE(i,a,b) for(int i = (a); i <= (b); ++i)
18 #define OG(i,a,b) for(int i = (a); i > (b); --i)
19 #define OGE(i,a,b) for(int i = (a); i >= (b); --i)
20 
21 typedef unsigned long long ull;
22 typedef long long ll;
23 typedef double db;
24 typedef long double ldb;
25 typedef stringstream sstm;
26 int fx[8][2] = {{1,0},{-1,0},{0,1},{0,-1},{1,1},{1,-1},{-1,-1},{-1,1}};
27 
28 template<typename T> using maxHeap = priority_queue<T, vector<T>, less<T> >;
29 template<typename T> using minHeap = priority_queue<T, vector<T>, greater<T> >;
30 
31 inline char nc(){ static char buf[1000000], *p1 = buf, *p2 = buf; return p1 == p2 && (p2 = (p1 = buf) + fread(buf,1,1000000,stdin),p1 == p2) ? EOF : *p1++; }
32 #define nc getchar
33 template<typename T> inline int read(T& sum){ char ch = nc(); if(ch == EOF || ch == -1) return 0; int tf = 0; sum = 0; while((ch < '0' || ch > '9') && (ch != '-')) ch = nc(); tf = ((ch == '-') && (ch = nc())); while(ch >= '0' && ch <= '9') sum = sum*10 + (ch-48), ch = nc(); (tf) && (sum = -sum); return 1; }
34 template<typename T,typename... Arg> inline int read(T& sum,Arg&... args){ int ret = read(sum); if(!ret) return 0; return read(args...); }
35 template<typename T1,typename T2> inline void read(T1* a,T2 num){ for(int i = 0; i < num; i++){read(a[i]);} }
36 template<typename T1,typename T2> inline void read(T1* a,T2 bn,T2 ed){ for(;bn <= ed; bn++){read(a[bn]);} }
37 inline void read(char* s){ char ch = nc(); int num = 0; while(ch != ' ' && ch != '\n' && ch != '\r' && ch != EOF){s[num++] = ch;ch = nc();} s[num] = '\0'; }
38 inline void read(string& s){ static char tp[1000005]; char ch = nc(); int num = 0; while(ch != ' ' && ch != '\n' && ch != '\r' && ch != EOF){tp[num++] = ch;ch = nc();} tp[num] = '\0';    s = (string)tp; }
39 template<typename T> inline void print(T k){ int num = 0,ch[20]; if(k == 0){ putchar('0'); return ; } (k<0)&&(putchar('-'),k = -k); while(k>0) ch[++num] = k%10, k /= 10; while(num) putchar(ch[num--]+48); }
40 template<typename T,typename... Arg> inline void print(T k,Arg... args){ print(k),putchar(' '); print(args...);}
41 template<typename T1,typename T2> inline void print(T1* a,T2 num){ print(a[0]); for(int i = 1; i < num; i++){putchar(' '),print(a[i]);} }
42 template<typename T1,typename T2> inline void print(T1* a,T2 bn,T2 ed){ print(a[bn++]); for(;bn <= ed; bn++){putchar(' '),print(a[bn]);} }
43 /*math*/
44 template<typename T> inline T gcd(T a, T b){ return b==0 ? a : gcd(b,a%b); }
45 template<typename T> inline T lowbit(T x){ return x&(-x); }
46 template<typename T> inline bool mishu(T x){ return x>0?(x&(x-1))==0:false; }
47 template<typename T1,typename T2, typename T3> inline ll q_mul(T1 a,T2 b,T3 p){ ll w = 0; while(b){ if(b&1) w = (w+a)%p; b>>=1; a = (a+a)%p; } return w; }
48 template<typename T,typename T2> inline ll f_mul(T a,T b,T2 p){ return (a*b - (ll)((long double)a/p*b)*p+p)%p; }
49 template<typename T1,typename T2, typename T3> inline ll q_pow(T1 a,T2 b,T3 p){ ll w = 1; while(b){ if(b&1) w = (w*a)%p; b>>=1; a = (a*a)%p;} return w; }
50 template<typename T1,typename T2, typename T3> inline ll s_pow(T1 a,T2 b,T3 p){ ll w = 1; while(b){ if(b&1) w = q_mul(w,a,p); b>>=1; a = q_mul(a,a,p);} return w; }
51 template<typename T> inline ll ex_gcd(T a, T b, T& x, T& y){ if(b == 0){ x = 1, y = 0; return (ll)a; } ll r = exgcd(b,a%b,y,x); y -= a/b*x; return r;/*gcd*/ }
52 template<typename T1,typename T2> inline ll com(T1 m, T2 n) { int k = 1;ll ans = 1; while(k <= n){ ans=((m-k+1)*ans)/k;k++;} return ans; }
53 template<typename T> inline bool isprime(T n){ if(n <= 3) return n>1; if(n%6 != 1 && n%6 != 5) return 0; T n_s = floor(sqrt((db)(n))); for(int i = 5; i <= n_s; i += 6){ if(n%i == 0 || n%(i+2) == 0) return 0; } return 1; }
54 /* ----------------------------------------------------------------------------------------------------------------------------------------------------------------- */
55 
56 int main() {
57 
58 
59     return 0;
60 }
相關文章
相關標籤/搜索