1 long long qpow(long long a,long long b,int mod) 2 { 3 long long res=1; 4 while (b) 5 { 6 if (b&1) res=res*a%mod; 7 a=a*a%mod; 8 b>>=1; 9 } 10 return res%mod; 11 }
1 const int mod1=19260817; 2 const int mod2=23333333; 3 const int mod3=33333331; 4 bool show[mod3]; 5 int hash(string a) 6 { 7 long long val=1; 8 int ha1=0,ha2=0; 9 int len=a.length(); 10 for(register int i=len-1;i>-1;i--) 11 { 12 ha1+=a[i]*val; 13 ha1%=mod1; 14 ha2+=a[i]*val; 15 ha2%=mod2; 16 val*=26; 17 if(val > 10000000 ) val=331; 18 } 19 return (long long)ha1*ha2%mod3; 20 }
1 int k,n,m,cnt,sum,ai,bi,ci,head[5005],dis[5005],vis[5005]; 2 struct Edge 3 { 4 int v,w,next; 5 }e[400005]; 6 void add(int u,int v,int w) 7 { 8 e[++k].v=v; 9 e[k].w=w; 10 e[k].next=head[u]; 11 head[u]=k; 12 } 13 typedef pair <int,int> pii; 14 priority_queue <pii,vector<pii>,greater<pii> > q; 15 int Prim() 16 { 17 memset(dis,127,sizeof(dis)); 18 memset(head,-1,sizeof(head)); 19 dis[1]=0; 20 q.push(make_pair(0,1)); 21 while(!q.empty()&&cnt<n) 22 { 23 int d=q.top().first,u=q.top().second; 24 q.pop(); 25 if(vis[u]) continue; 26 cnt++; 27 sum+=d; 28 vis[u]=1; 29 for(R i=head[u];i!=-1;i=e[i].next) 30 if(e[i].w<dis[e[i].v]) 31 dis[e[i].v]=e[i].w,q.push(make_pair(dis[e[i].v],e[i].v)); 32 } 33 if (cnt==n) return sum; 34 return -1;
1 struct Edge 2 { 3 int x,y,z; 4 }e[200001]; 5 int n,m,u,v,fu,fv,cnt,ans; 6 int fa[200001]; 7 bool cmp(Edge a,Edge b){return a.z < b.z || (a.z==b.z && a.x<b.x);} 8 int find(int x){return x == fa[x]?x:fa[x]=find(fa[x]);} 9 void kruskal() 10 { 11 for(int i=1;i<=m;i++) fa[i] = i; 12 for(int i=1;i<=n;i++) 13 { 14 u = e[i].x; 15 v = e[i].y; 16 fu = find(u); 17 fv = find(v); 18 if(fu != fv) 19 { 20 fa[fu] = fv; 21 ans += e[i].z; 22 cnt++; 23 } 24 if(cnt == m-1) break; 25 } 26 if (ans) return ans; 27 return -1' 28 }
1 #ifndef DS 2 #define DS 3 template <unsigned const long long maxn=20> 4 class i_Ds 5 { 6 typedef long long X; 7 X s[maxn],height[maxn]; 8 typedef X inte; 9 public: 10 void init(){for (inte i=0;i<maxn;i++) s[i]=i,height[i]=0;} 11 i_Ds(){init();} 12 X find(X x) {if (x!=s[x]) s[x]=find(s[x]);return s[x];} 13 void _union(X x,X y) 14 { 15 x=find(x);y=find(y); 16 if (height[x]==height[y]) height[x]++,s[y]=x; 17 else if(height[x]<height[y]) s[x]=y; 18 else s[y]=x; 19 } 20 inte js(inte b,inte n) {inte ans=0;for (inte i=b;i<n;i++)if (s[i]==i) ++ans;return ans;} 21 inte js(const inte n=maxn){inte ans=0;for (inte i=0;i<n;i++)if (s[i]==i) ++ans;return ans;} 22 }; 23 #endif
1 int prime[maxn]; 2 int visit[maxn]; 3 void Prime() 4 { 5 memset(visit,0,sizeof visit); 6 memset(prime,0,sizeof prime); 7 for (int i=2;i<=maxn;i++) 8 { 9 if (!visit[i]) prime[++prime[0]]=i; 10 for (int j=1;j<=prime[0]&&i*prime[j]<=maxn;j++) 11 { 12 visit[i*prime[j]]=1; 13 if ((!i%prime[j])) break; 14 } 15 } 16 }
1 void qsort(int a[],int l,int r) 2 { 3 int i=l,j=r,mid=a[(l+r)/2]; 4 do 5 { 6 while (a[i]<mid) i++; 7 while (a[j]>mid) j--; 8 if (i<=j) 9 { 10 int tmp=a[i];a[i]=a[j];a[j]=tmp; 11 i++;j--; 12 } 13 }while(i<=j); 14 if (l<j) qsort(a,l,j); 15 if (i<r) qsort(a,i,r); 16 }
1 //用洛谷P3865做示范 2 #include<cstdio> 3 #define max(a,b) (a>b?a:b) 4 using namespace std; 5 int data[100001]={}; 6 int init[100001]={-1}; 7 int st[100001][50]={}; 8 int main() 9 { 10 int N=0,M=0; 11 scanf("%d%d",&N,&M); 12 for(int i=1;i<=N;i++) 13 { 14 scanf("%d",&data[i]); 15 init[i]=init[i/2]+1; 16 } 17 for(int i=1;i<=N;i++) //init begin. 18 st[i][0]=data[i]; 19 for(int i=1;i<=init[N];i++) 20 for(int j=1;j+(1<<i)-1<=N;j++) 21 st[j][i]=max(st[j][i-1],st[j+(1<<(i-1))][i-1]);//init end. 22 int Left=0,Right=0; 23 while(M--) 24 { 25 scanf("%d%d",&Left,&Right); 26 int Length=init[Right-Left+1]; 27 printf("%d ",max(st[Left][Length],st[Right-(1<<(Length))+1][Length])); 28 } 29 return 0; 30 }
1 if (所有石子个数异或得到的数字==0) cout<<"先手必败“; 2 else cout<<"先手必胜“;
1 void exgcd(int a,int b,int& x,int& y) 2 { 3 if (!b){x=1,y=0;return ;} 4 exgcd(b,a%b); 5 int t=x; 6 x=y,y=t-a/b*y; 7 }
1 //前置exgcd 2 int inv(int k,int p) 3 { 4 int x,y; 5 exgcd(k,p,x,y); 6 return (x%p+p)%p; 7 }
1 const int N = 505; //设置数组的大小 2 bool line[N][N]; //记录连接x和y的边,如果i和j之间有边则为1,否则为0 3 int result[N]; //记录当前与y节点相连的x的节点:i未加入匹配时为link[i]==0 4 bool used[N]; //记录y中节点是否使用 5 int k, m, n; 6 bool found(int x) 7 { 8 for (int i = 1; i <= n; i++) 9 { 10 if (line[x][i] && !used[i]) 11 { 12 used[i] = true; 13 if (result[i] == 0 || found(result[i])) 14 { 15 result[i] = x; 16 return true; 17 } 18 } 19 } 20 return false; 21 } 22 int main() 23 { 24 int x, y; 25 printf("请输入相连边的数量k: "); 26 while (scanf("%d", &k) && k) 27 { 28 printf("请输入二分图中x和y中点的数目: "); 29 scanf("%d %d", &m, &n); 30 memset(line, 0, sizeof(line)); 31 memset(result, 0, sizeof(result)); 32 for (int i = 0; i < k; i++) 33 { 34 printf("请输入相连边的两个点: "); 35 scanf("%d %d", &x, &y); 36 line[x][y] = 1; 37 } 38 int sum = 0; 39 for (int i = 1; i <= m; i++) 40 { 41 memset(used, 0, sizeof(used)); 42 if (found(i)) sum++; 43 } 44 printf("%d ", sum); 45 } 46 return 0; 47 }
1 //P3379示范 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <algorithm> 6 using namespace std; 7 struct Edge 8 { 9 int Length,Next; 10 }ed[1000020]; 11 int start[500010],tot; 12 void Add_Edge(int x,int y) 13 { 14 ed[++tot].Length=y; 15 ed[tot].Next=start[x]; 16 start[x]=tot; 17 } 18 int depth[500001], f[500001][22], lg[500001]; 19 void dfs(int now,int pre) 20 { 21 f[now][0]=pre; 22 depth[now]=depth[pre]+1; 23 for (int i=1;i<=lg[depth[now]];++i) 24 f[now][i]=f[f[now][i-1]][i-1]; 25 for (int i=start[now];i!=0;i=ed[i].Next) 26 if(ed[i].Length!=pre) dfs(ed[i].Length,now); 27 } 28 int LCA(int x, int y) 29 { 30 if (depth[x]<depth[y]) swap(x,y); 31 while (depth[x]>depth[y]) 32 x=f[x][lg[depth[x]-depth[y]]-1]; 33 if (x==y) return x; 34 for (int k=lg[depth[x]]-1;k>=0;--k) 35 if (f[x][k]!=f[y][k]) 36 x=f[x][k],y=f[y][k]; 37 return f[x][0]; 38 } 39 int main() 40 { 41 int n,m,s; 42 scanf("%d%d%d", &n, &m, &s); 43 for (int i=1;i<=n-1;++i) 44 { 45 int x,y; 46 scanf("%d%d",&x,&y); 47 Add_Edge(x,y); 48 Add_Edge(y,x); 49 } 50 for(int i=1;i<=n;++i) 51 lg[i]=lg[i-1]+(1<<lg[i-1]==i); 52 dfs(s,0); 53 for (int i=1;i<=m;++i) 54 { 55 int x,y; 56 scanf("%d%d",&x,&y); 57 printf("%d ",LCA(x,y)); 58 } 59 return 0; 60 }
1 #include<iostream> 2 #include<cstring> 3 #innclude<cmath> 4 struct Trie 5 { 6 int ch[maxn][maxsize]; 7 int val[maxn]; 8 int sz; 9 Trie() 10 { 11 sz=1; 12 val[0]=0; 13 memset(ch[0],0,sizeof ch[0]); 14 } 15 void clear() 16 { 17 sz=1; 18 val[0]=0; 19 memset(ch[0],0,sizeof ch[0]); 20 } 21 int idx(char c){return c-'a';} 22 void insert(const char *s,int v=1) 23 { 24 int u=0,n=strlen(s); 25 for (int i=0;i<n;i++) 26 { 27 int c=idx(s[i]); 28 if (ch[u][c]==0) 29 { 30 memset(ch[sz],0,sizeof ch[sz]); 31 val[sz]=0; 32 ch[u][c]=sz++; 33 } 34 u=ch[u][c]; 35 } 36 val[u]=v; 37 } 38 int find(const char *s) 39 { 40 int u=0,n=strlen(s); 41 for (int i=0;i<n;i++) 42 { 43 int c=idx(s[i]); 44 if (ch[u][c]==0) return -1; 45 u=ch[u][c]; 46 } 47 return val[u]; 48 } 49 void del(const char *s) 50 { 51 int u=0,n=strlen(s); 52 for (int i=0;i<n;i++) 53 { 54 int c=idx(s[i]); 55 if (ch[u][c]==0) return ; 56 u=ch[u][c]; 57 } 58 val[u]=0; 59 } 60 };
1 struct point 2 { 3 int hao; 4 ll dis; 5 bool friend operator <(point a,point b){return a.dis>b.dis;} 6 }; 7 priority_queue<point>q; 8 void dij() 9 { 10 point st; 11 st.hao=s; 12 st.dis=0; 13 q.push(st); 14 int has=0; 15 while ((has!=n)&&(!q.empty())) 16 { 17 point now=q.top(); 18 q.pop(); 19 if (vis[now.hao]) continue; 20 has++; 21 vis[now.hao]=1; 22 dis[now.hao]=now.dis; 23 for (int i=head[now.hao];i;i=bian[i].nxt) 24 { 25 int y=bian[i].to; 26 if (!vis[y]) 27 { 28 point last; 29 last.hao=y; 30 last.dis=now.dis+bian[i].val; 31 q.push(last); 32 } 33 } 34 } 35 }
1 //初始化 2 for (i=1;i<=n;i++) 3 for (j=1;j<=n;j++) 4 e[i][j]=(i==j)?0:inf; 5 //核心 6 for(k=1;k<=n;k++) 7 for(i=1;i<=n;i++) 8 for(j=1;j<=n;j++) 9 e[i][j]=max(e[i][j],e[i][k]+e[k][j]);
1 int Kmp(char* s, char* p) 2 { 3 int i=0,j=0; 4 int sLen=strlen(s); 5 int pLen=strlen(p); 6 while (i<sLen&&j<pLen) 7 { 8 if (j==-1||s[i]==p[j]) i++,j++; 9 else j=next[j]; 10 } 11 if (j==pLen) return i-j; 12 else return -1; 13 }
1 char s[11000002]; 2 char s_new[21000002] 3 int p[21000002]; 4 int Init() 5 { 6 int len=strlen(s); 7 s_new[0]='$'; 8 s_new[1]='#'; 9 int j=2; 10 for(int i=0;i<len;i++) 11 { 12 s_new[j++]=s[i]; 13 s_new[j++]='#'; 14 } 15 s_new[j]='