编程中常用到的一些代码
本文都是些很基本的东西,但每次用都要找,索性整理在一起,方便以后查(Ctrl+C)阅(Ctrl+V)
1、手动扩栈(C++)
1 |
#pragma comment(linker, "/STACK:102400000,102400000") |
2、memset
1 2 3 4 |
memset(a,0,sizeof(a));//a[0]=0 memset(a,255,sizeof(a));//a[0]=-1 memset(a,0x3f,sizeof(a));//a[0]=0x3f3f3f3f=1061109567≈INT_MAX/2 memset(a,0xc0,sizeof(a));//a[0]=0xc0c0c0c0=-1061109568≈-INT_MAX/2 |
3、优先队列priority_queue
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
priority_queue<int> q1;//默认大的先出队 priority_queue<int,vector<int>,greater<int> > q2;//小的先出队 //自定义比较函数 struct cmp{ bool operator ()(int x,int y){ return x>y;//x小的优先级高,结构体同 } }; priority_queue<int,vector<int>,cmp > q3;//定义 //重载运算符 struct node{ int x,y; friend bool operator < (node a,node b){//友元函数,如果用成员函数记得加const return a.x>b.x;//x小的优先级高 } }; priority_queue<node> q4;//定义 //最好重载'<' |
4、输入输出挂
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
//输入挂 inline int scan() { int res = 0, ch, flag = 0; if((ch = getchar()) == '-') flag = 1; else if(ch >= '0' && ch <= '9') res = ch - '0'; while((ch = getchar()) >= '0' && ch <= '9' ) res = res * 10 + ch - '0'; return flag ? -res : res; } //输出挂 inline void out(int x) { if(x>9) out(x/10); putchar(x%10+'0'); } |
fread读入挂
1 2 3 4 5 6 7 8 9 |
const int BUFSIZE=120<<20; //根据题目数据而定 char Buf[BUFSIZE+1],*buf=Buf; template<class T> inline void scan(T&a){ for (a=0;*buf<'0'||*buf>'9';buf++); while (*buf>='0'&&*buf<='9'){a=a*10+(*buf-'0');buf++; } } fread(Buf,1,BUFSIZE,stdin); //加到main函数第一行 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
struct FastIO { static const int S = 1000000; int wpos, pos, len; char wbuf[S]; FastIO():wpos(0){} inline int xchar() { static char buf[S]; if (pos == len) pos = 0, len = fread(buf, 1, S, stdin); if (pos == len) return -1; return buf[pos++]; } inline int xint() { int c = xchar(), x = 0; while (c <= 32 && ~c) c = xchar(); if (c == -1) return -1; for (; c >= '0' && c <= '9'; c = xchar()) x = x * 10 + (c - '0'); return x; } } io; |
5、lower_bound()&upper_bound()
1 2 3 |
lower_bound(__first, __last, __val)//返回大于等于某值的第一个元素的迭代器 upper_bound(__first, __last, __val)//返回大于某值的第一个元素的迭代器 |
6、内联汇编快速乘法(G++)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
inline long long mulmod(long long x, long long y, long long mod) { long long ret = 0; __asm__("movq %1,%%rax\n imulq %2\n idivq %3\n":"=d"(ret):"m"(x),"m"(y),"m"(mod):"%rax"); return ret; } //非汇编版本 long long mulmod(long long x, long long y, long long mod) { long long ret = 0,tmp=x; while(y){ if(y&1) ret=(ret+tmp)%mod; y>>=1; tmp=(tmp+tmp)%mod; } return ret; } |
7、快速开方
1 2 3 4 5 6 7 8 9 10 11 12 |
float InvSqrt(float x) { int i; float xhalf=0.5*x,y=x; i=*(int *) &y; i=0x5f375a86 - ( i >> 1 ); y=*(float *) &i; y=y*(1.5-(xhalf*y*y)); y=y*(1.5-(xhalf*y*y)); y=y*(1.5-(xhalf*y*y)); return x*y; } |
8、拓展欧几里得算法
1 2 3 4 5 6 7 8 9 10 11 |
int extend_gcd(int a,int b,int &x,int &y) { int d; if(b==0){ x=1;y=0; return a; } d=extend_gcd(b,a%b,y,x); y-=a/b*x; return d; } |
9、中国剩余定理(CRT)
1 2 3 4 5 6 7 8 9 10 11 12 13 |
long long crt(long long a[],long long m[],long long n)//a[]为余数 { long long M=1; for(int i=1;i<=n;i++) M*=m[i]; long long ret=0; for(int i=1;i<=n;i++){ long long x,y; long long tm=M/m[i]; extend_gcd(tm,m[i],x,y); ret=(ret+mulmod(mulmod(tm,x,M),a[i],M))%M; } return (ret+M)%M; } |
待补充……
本文出自shad0w_walker,转载时请注明出处及相应链接。
本文永久链接: https://www.sdwalker.com/archives/305.html
5条评论
仰慕啊
仰慕学弟Orz
仰慕学长
仰慕学长
仰慕大佬