方便以后用---可能有错---
1.判断素数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 void isprime(){ 2 isp[1]=isp[0]=1; 3 for(int i=2;i<100010;i++){ 4 if(isp[i]==0) 5 for(int j=i*2;j<100010;j+=i) 6 isp[j]=1; 7 } 8 }
2.海伦公式
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 s=sqrt(p*(p-a)*(p-b)*(p-c)); 2 p=(a+b+c)/2;
3.并查集
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 int find(int root){ 2 return root == pre[root] ? root : pre[root] = find(pre[root]); } 3 4 void unionroot(int x,int y){ 5 int root1=find(x); 6 int root2=find(y); 7 if(root1!=root2) pre[root1]=root2; 8 }
4 __builtin_popcount()
计算32位无符号整数中共有多少个1
5.输入输出挂
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 void scan( int& x ) 2 { 3 char c; 4 while( c = getchar(), c < '0' || c > '9' ); 5 x = c - '0'; 6 while( c = getchar(), c >= '0' && c <= '9' ) x = x * 10 + c - '0'; 7 } 8 9 void out( int x ) 10 { 11 if( x > 9 ) out( x/10 ); 12 putchar( x % 10 + '0' ); 13 }
6.判断回文串
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 bool is(){ 2 for(int l=0,r=n-1;l<r;l++,r--){ 3 if(e[l]!=e[r]) 4 return false; 5 } 6 return true; 7 }
7.幂取模
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 int pow_mod(ULL a, ULL b, int n) { 2 if(!b) return 1; 3 int k = pow_mod(a, b/2, n); 4 k = k * k % n; 5 if(b % 2) k = k * a % n; 6 return k; 7 }
8.int,long long等的范围
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 unsigned int 0~4294967295 2 int 2147483648~2147483647 3 unsigned long 0~4294967295 4 long 2147483648~2147483647 5 long long的最大值:9223372036854775807 6 long long的最小值:-9223372036854775808 7 unsigned long long的最大值:18446744073709551615 8 9 __int64的最大值:9223372036854775807 10 __int64的最小值:-9223372036854775808 11 unsigned __int64的最大值:18446744073709551615
9.fmod可对浮点数取模
10.最大公约数,最小公倍数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 ll gcd(ll a, ll b){ 2 return (!b) ? a : gcd(b, a % b); 3 } 4 5 ll lcm(ll a, ll b){ 6 return a / gcd(a, b) * b; 7 }
11.素数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 一百以内有 25 个素数,最大为97 2 一千以内有 168 个素数,最大为997 3 一万以内有 1229 个素数,最大为9973 4 十万以内有 9592 个素数,最大为99991 5 百万以内有 78498 个素数,最大为999983 6 千万以内有 664579个素数,最大为9999991 7 一亿以内有5761455个素数,最大为99999989
12.欧拉函数
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <cstdio> 2 #include <cstdlib> 3 using namespace std; 4 #define N 1000005 5 int phi[N]; 6 7 void eula(){ 8 for(int i=1;i<=N;i++) 9 phi[i] = i; 10 for(int i=2;i<=N;i++) 11 if(phi[i]==i) //如果phi[i]==i的话说明还没有进行过计算,为素数 12 //没有进行过计算,说明在当前i值之前(即比i小的数中)没有i的因数,所以i为素数 13 for(int j=i;j<=N;j+=i) 14 phi[j]=phi[j]/i*(i-1);//将非素数进行计算 15 return; 16 } 17 18 int main(){ 19 eula(); 20 int T; 21 scanf("%d",&T); 22 while(T--){ 23 int n; 24 scanf("%d",&n); 25 printf("%d\n",phi[n+1]); 26 } 27 return 0; 28 }
13.数的二进制表示定理
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 定理:一个正整数n可以被分解成1,2,4,…,2^(k-1),n-2^k+1(k是满足n-2^k+1>0的最大整数)的形式,且1~n之内的所有整数均可以唯一表示成1,2,4,…,2^(k-1),n-2^k+1中某几个数的和的形式。 2 证明如下: 3 (1) 数列1,2,4,…,2^(k-1),n-2^k+1中所有元素的和为n,所以若干元素的和的范围为:[1, n]; 4 (2)如果正整数t<= 2^k – 1,则t一定能用1,2,4,…,2^(k-1)中某几个数的和表示,这个很容易证明:我们把t的二进制表示写出来,很明显,t可以表示成n=a0*2^0+a1*2^1+…+ak*2^(k-1),其中ak=0或者1,表示t的第ak位二进制数为0或者1. 5 (3)如果t>=2^k,设s=n-2^k+1,则t-s<=2^k-1,因而t-s可以表示成1,2,4,…,2^(k-1)中某几个数的和的形式,进而t可以表示成1,2,4,…,2^(k-1),s中某几个数的和(加数中一定含有s)的形式。 6 (证毕!)
14.非递归写dfs
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 void dfs(int now) 2 { 3 vis[now]=1; 4 //Balabala1 5 for (每一条now出发的边) 6 if (!vis[e.to]) dfs(e.to); 7 //Balabala2 8 } 9 调用dfs(Start) 10 11 改成 12 13 void dfs() 14 { 15 stack[top=1]=Start; 16 while(top) 17 { 18 int now=stack[top]; 19 if (!vis[now]) 20 { 21 vis[now]=1; 22 //Balabala1; 23 for (每一条now出发的边) 24 if (!vis[e.to]) stack[++top]=e.to; 25 } 26 else 27 { 28 //Balabala2; 29 top--; 30 } 31 } 32 }
15.STL里面的lower_bound,upper_bound
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 http://blog.csdn.net/nisxiya/article/details/44945441
16.unique 和lower_bound 离散化
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio> 2 #include<cstring> 3 #include<iostream> 4 #include<algorithm> 5 using namespace std; 6 7 int a[105],n,b[105]; 8 int c[105],d[105]; 9 10 int main(){ 11 while(scanf("%d",&n) != EOF){ 12 /* for(int i = 1;i <= n;i++) scanf("%d",&a[i]),b[i]=a[i]; 13 sort(a+1,a+n+1); 14 int sz = unique(a+1,a+n+1)-a-1; 15 printf("sz = %d\n",sz); 16 17 for(int i = 1;i <= n;i++){ 18 int pos = lower_bound(a+1,a+sz+1,b[i])-a; 19 printf("pos = %d\n",pos); 20 }*/ 21 22 for(int i = 0;i < n;i++) scanf("%d",&c[i]),d[i]=c[i]; 23 sort(c,c+n); 24 int tot = unique(c,c+n)-c; 25 printf("tot = %d\n",tot); 26 for(int i = 0;i < n;i++){ 27 int pos = lower_bound(c,c+n,d[i])-c; 28 printf("pos = %d\n",pos); 29 } 30 31 } 32 return 0; 33 }
17.二分的端点
1.如果正解在左边
左边的意思就是
比如一个递增的序列,需要找到大于x 的最小的数
那么要找的答案就是左边,因为满足左边的数都小于x
同理,如果要找大于 x 的最下的数,就是右边
左边 的话
mid = ub - (ub-lb)/2
l = mid, r = mid-1
右边的话
mid = lb +(ub-lb)/2
l = mid+1
r = mid
18.运算符的重载
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 struct node{ 2 int x; 3 int t; 4 friend bool operator < ( node n1,node n2) { 5 if(n1.x != n2.x) return n1.x < n2.x; 6 return n1.t > n2.t; 7 } 8 };
19.单调栈
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 a[0]=-1,a[n+1]=-1; 2 3 for(int i=1;i<=n;i++) 4 { 5 int j=i-1; 6 while(a[j]>=a[i])j=l[j]; 7 l[i]=j; 8 } 9 for(int i=n;i>=1;i--) 10 { 11 int j=i+1; 12 while(a[j]>=a[i])j=r[j]; 13 r[i]=j; 14 }
20.后缀表达式的求解
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 1. 维护数字栈 2 3 2. 从左向右扫描,遇到数字则入栈,遇到符号则出栈两个数字,计算后再入栈新数字,后缀表达式的特点就是方便计算,直接按操作符顺序和数字逆序计算即可。
21.host
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 # Copyright (c) 1993-2009 Microsoft Corp. 2 # 3 # This is a sample HOSTS file used by Microsoft TCP/IP for Windows. 4 # 5 # This file contains the mappings of IP addresses to host names. Each 6 # entry should be kept on an individual line. The IP address should 7 # be placed in the first column followed by the corresponding host name. 8 # The IP address and the host name should be separated by at least one 9 # space. 10 # 11 # Additionally, comments (such as these) may be inserted on individual 12 # lines or following the machine name denoted by a '#' symbol. 13 # 14 # For example: 15 # 16 # 102.54.94.97 rhino.acme.com # source server 17 # 38.25.63.10 x.acme.com # x client host 18 19 # localhost name resolution is handled within DNS itself. 20 # 127.0.0.1 localhost 21 # ::1 localhost
22.01背包的初始化
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 1、要求“恰好装满背包”时的最优解: 2 3 在初始化时除了f[0]为0其它f[1..V]均设为-∞,这样就可以保证最终得到的f[N]是一种恰好装满背包的最优解。如果不能恰好满足背包容量,即不能得到f[V]的最优值,则此时f[V]=-∞,这样就能表示没有找到恰好满足背包容量的最优值。 4 5 2、求小于等于背包容量的最优解,即不一定恰好装满背包: 6 7 如果并没有要求必须把背包装满,而是只希望价值尽量大,初始化时应该将f[0..V]全部设为0。
23.二进制枚举子集
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 int x = 20; 2 for(int i = 0;i < 1<<x;i++){ 3 for(int j = 0;j < x ;j++){ 4 if((1<<j) & i) printf("1 "); 5 else printf("0 "); 6 } 7 printf("\n"); 8 }
24.期望是可以相加的
25.构造系数矩阵
b g1 g2 要左乘 一个 什么 可以变成 b g2 g3 呢
1 0 0 b b
0 0 1 * g1 = g2
1 1 c g2 g3
因为 gn 只和 b gn-1 gn-2 有关,所以列向量要放这三个
然后尝试把b gn gn-1 用 b gn-1 gn-2 线性表出
系数就是矩阵
26.sublime
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 { 2 "cmd": ["g++", "${file}", "-o", "${file_path}/${file_base_name}.exe" ], 3 "file_regex": "^(..[^:]*):([0-9]+):?([0-9]+)?:? (.*)$", 4 "working_dir": "${file_path}", 5 "selector": "source.c, source.c++", 6 "shell": true, 7 "variants": 8 [ 9 { 10 "name": "Run", 11 "shell": true, 12 "cmd" : ["start", "cmd", "/k", "${file_path}/${file_base_name} &&echo. & pause && exit"] 13 } 14 ] 15 }
下载地址 http://www.sublimetext.com/2
27.
在一个字母上头打无穷符号:\overset{\infty}{V}