随笔分类 -  模版

虽然模版能减少思维复杂度,但是还是希望能早点脱离模版。
摘要:View Code /*==================================================*\| Polya计数| c种颜色的珠子, 组成长为s的项链, 项链没有方向和起始位置;\*==================================================*/// 注意超long longint Polya(int c,int s) { int i,j,k,p[64], count; p[0]=1; // power of c for(k=1; k<=s ;k++) p[k]=p[k-1]*c; //reflecti... 阅读全文
posted @ 2013-04-20 13:41 zhang1107 阅读(173) 评论(0) 推荐(0)
摘要:View Code const int maxn = 11111; //文本最大长度int fail[maxn]; //失败了应该跳的位置char str[maxn], text[maxn]; //输入文本和待匹配的字符串//初始位置从0开始void get_next(int n) { int i, j=-1; for(fail[0]=-1, i=1;i < n; i++) { while(j>=0 && str[i]!=str[j+1]) j=fail[j]; if(str[j+1]==str[i]) j++; fail[... 阅读全文
posted @ 2013-04-19 16:13 zhang1107 阅读(146) 评论(0) 推荐(0)
摘要:View Code /*================================*\| 筛素数 [1..n]\*================================*/bool isp[MM]; int prm[MM];int get_prime(int n) { int i,j,k=0; int s, e=(int)(sqrt(0.0+n)+1); for(i=0;i<=n;i++) isp[i]=true; prm[k++]=2; isp[0]=isp[1]=false; for(i=4;i<n;i+=2) isp[i]=false; ... 阅读全文
posted @ 2013-04-18 19:34 zhang1107 阅读(140) 评论(0) 推荐(0)
摘要://组合数 com(n,r)View Code /*==========================*\| 组合数 com(n,r)\*==========================*/const int maxn = 100; //元素个数int com[maxn][maxn]; //注意 long longvoid get_com() { int i,j,k; for(i=0;i<maxn;i++) { com[i][0]=1; for(j=1;j<=i;j++) com[i][j]=com[i-1][j-1]+com[i-1][j]; ... 阅读全文
posted @ 2013-04-18 11:31 zhang1107 阅读(134) 评论(0) 推荐(0)
摘要:http://poj.org/problem?id=1699状态压缩,预处理出任意两个串的最长前缀和后缀的公共部分,然后DP。View Code //POJ1699const int MM = 110;#define maxint 0x3f3f3f3fint N;int d[1<<11][11];char str[MM];int len[MM][MM];int fail[MM];char ch[21][MM];int cal(int s1,int s2) { int i,j,k,n,m,ans=0; n=strlen(ch[s1]+1); m=strlen(ch[s2]+1)... 阅读全文
posted @ 2013-04-17 13:42 zhang1107 阅读(281) 评论(0) 推荐(0)
摘要:http://poj.org/problem?id=3368一维RMQView Code //询问递增序列 [l,r] 间,出现次数最多的数的个数,分段后用RMQ询问最小值int id, d[MM][17], N, Q;int len[MM],pos[MM],b[MM];int log_2(int x) { int res=0; while(x>>=1) res++; return res;}void get_rmq() { int i,j,k,limit; for(i=1;i<=id;i++) d[i][0]=len[i]; k=log_2(id); ... 阅读全文
posted @ 2013-04-17 13:21 zhang1107 阅读(123) 评论(0) 推荐(0)
摘要://phi[i]记录的是 0~i 之间与i互质的数的个数http://poj.org/problem?id=2478View Code const int MM = 1000005;typedef __int64 int64;int64 N;int64 phi[MM];void get_phi() { int64 i,j,k; for(i=1;i<MM;i++) phi[i]=i; for(i=2;i<MM;i+=2) phi[i]>>=1; for(i=3;i<MM;i++) { if(phi[i]==i) { for(... 阅读全文
posted @ 2013-04-17 13:09 zhang1107 阅读(176) 评论(0) 推荐(0)
摘要:View Code //头文件:#include <queue>struct Tpoint{ int val,dead; //Tpoint(int v,int d):val(v),dead(d) {} bool friend operator<(Tpoint x,Tpoint y) { if(x.val!=y.val) return x.val>y.val; else return x.dead>y.dead; }};//多个key,重载运算符,que.top()保存的是val最小的priority_queue<Tpoint>que; 阅读全文
posted @ 2013-04-17 12:34 zhang1107 阅读(170) 评论(0) 推荐(0)
摘要:http://poj.org/problem?id=2299View Code //POJ2299int f_min(int x,int y) {if(x<y)return x; else return y;}int f_max(int x,int y) {if(x<y)return y; else return x;}const int MM = 511111;typedef __int64 int64;#define maxint 0x3f3f3f3fint64 N;int64 num[MM];int64 L[MM], R[MM],ans;void get_data() { i 阅读全文
posted @ 2013-04-16 19:43 zhang1107 阅读(117) 评论(0) 推荐(0)
摘要:View Code int gcd(int x,int y) { int tmp; while(y) { tmp=y; y=x%y; x=tmp; } return x;}int lcm(int a,int b) { return a/gcd(a,b)*b;} 阅读全文
posted @ 2013-04-15 12:58 zhang1107 阅读(168) 评论(0) 推荐(0)
摘要:View Code const int maxn = 22; //矩阵大小struct Matrix { int r, c; //矩阵大小 int a[maxn][maxn]; //初始化 Matrix(int r,int c):r(r), c(c) {} void reset() {memset(a,0,sizeof(a));} //重写(),方便存取 int& operator() (int i,int j) {return a[i][j];} //O(N^3) 矩阵乘法 Matrix operator*(const Matrix&B)... 阅读全文
posted @ 2013-04-11 18:26 zhang1107 阅读(238) 评论(0) 推荐(0)
摘要:类似与快速幂的做法View Code /*===================================================================*\ | 两个数相乘超long long乘法的取模 mod的大小要十分注意\*===================================================================*/int multi_mod(int a,int b,int c) { //(a*b)%c int ret=0; while(b) { if(b&1) { ret+=a; if(re... 阅读全文
posted @ 2013-03-31 11:09 zhang1107 阅读(1760) 评论(0) 推荐(0)
摘要:View Code /*==================================================*\| 最长有序子序列(递增/递减/非递增/非递减)\*==================================================*/const int N = 1001;int a[N], f[N], d[N]; // d[i]用于记录a[0...i]的最大长度int bsearch(const int *f, int size, const int &a) { int l=0, r=size-1; while( l <= r ) 阅读全文
posted @ 2013-03-24 18:10 zhang1107 阅读(253) 评论(0) 推荐(0)
摘要:http://www.codeforces.com/contest/283/problem/AView Code #include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int MM = 222222;#define debug puts("wrong")typedef __int64 int64;#define lson rt<<1#define rson rt<& 阅读全文
posted @ 2013-03-19 17:21 zhang1107 阅读(117) 评论(0) 推荐(0)
摘要:View Code set<int ,less<int> > 集合中元素从小到大排序set<int ,greater<int> > 集合中元素从大到小排序 begin() 返回指向第一个元素的迭代器clear() 清除所有元素count() 返回某个值元素的个数empty() 如果集合为空,返回true(真)end() 返回指向最后一个元素之后的迭代器,不是最后一个元素erase() 删除集合中的元素insert() 在集合中插入元素size() 集合中元素的数目struct compare{ bool operator ()(string s1 阅读全文
posted @ 2013-03-12 13:42 zhang1107 阅读(150) 评论(0) 推荐(0)
摘要:View Code #include <iostream>#include <cstdio>#include <queue>#include <cstring>#include <cmath>#include <algorithm>using namespace std;const int MM = 111111; //处理字符串的最大长度#define debug puts("wrong")int Wa[MM], Wb[MM], Wv[MM], Ws[MM];int ran[MM], height[M 阅读全文
posted @ 2013-03-08 11:41 zhang1107 阅读(166) 评论(0) 推荐(0)
摘要:http://poj.org/problem?id=1469View Code //POJ1469#include <iostream>#include <cstdio>#include <cstring>using namespace std;const int MM=100010;int P,N;bool vis[MM];int pre[MM];int head[MM], NE;struct Edge{int v,next;}edge[MM];void add_edge(int u,int v) { edge[NE].v=v; edge[NE].next 阅读全文
posted @ 2013-03-08 11:37 zhang1107 阅读(177) 评论(0) 推荐(0)
摘要:http://poj.org/problem?id=3974View Code //O(N)计算一个给定字符串的最长回文子串const int MM = 1111111; char str[MM],ch[MM<<2];int rad[MM<<2], N;void get_init() { int i,j,k;ch[0]='$', ch[1]='#'; for(i=0;str[i];i++) ch[(i<<1)+2]=str[i],ch[(i<<1)+3]='#'; N=(i<<1)+2, 阅读全文
posted @ 2012-12-30 00:04 zhang1107 阅读(161) 评论(0) 推荐(0)
摘要:POJ2115 模线性方程View Code #include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;typedef __int64 int64;int64 A,B,C,K;int64 p[35];void get_P() { p[0]=1; for(int i=1;i<33;i++) p[i]=p[i-1]*2;}int64 Extend_euc(int64 a,int64 b,int64&x,int 阅读全文
posted @ 2012-12-28 19:55 zhang1107 阅读(184) 评论(0) 推荐(0)
摘要:Happy 2006容斥原理+分解素数因子View Code //POJ2773const int MM = 1100000;typedef __int64 int64;const int64 maxint = 0x3f3f3f3f;int64 N,M;bool isp[MM];int64 tp[MM],mm;int64 p[MM],cnt;void get_prime() { int64 i,j,k; memset(isp,true,sizeof(isp)); isp[0]=isp[1]=false; for(i=2;i<1005;i++) { ... 阅读全文
posted @ 2012-12-22 16:16 zhang1107 阅读(177) 评论(0) 推荐(0)