快速幂
#include<iostream>
#include<cstdio>
#define int long long
using namespace std;
int ksm(int b,int p,int k){
int ans=1;
while(p){
if(p&1)
ans=ans%k*b%k;
b=b%k*b%k;
p>>=1;
}
return ans%k;
}
signed main(){
int b,p,k;
cin>>b>>p>>k;
cout<<b<<"^"<<p<<" mod "<<k<<"="<<ksm(b,p,k)%k;
return 0;
}
字符串哈希
#include<bits/stdc++.h>
#define int unsigned long long
using namespace std;
int mod=0x7fffffff;
int p=131;
char s[100000];
int h(char *s){
int l=strlen(s);
int sum=0;
for(int i=0;i<l;i++)
sum=p*sum+(int)s[i];
return sum%mod;
}
int ans=0,m[100000];
signed main(){
int n;
cin>>n;
for(int i=1;i<=n;i++){
scanf("%s",s);
m[i]=h(s);
}
sort(m+1,m+n+1);
for(int i=1;i<=n;i++){
if(m[i]!=m[i-1])
ans++;
}
cout<<ans;
return 0;
}
并查集(路径压缩)
#include <iostream>
#include <cstdio>
using namespace std;
int z, x, y;
int n, m;
int f[10000];
int find(int s) {
if (f[s] == s)
return s;
return f[s] = find(f[s]);
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++) {
f[i] = i;
}
for (int i = 1; i <= m; i++) {
cin >> z >> x >> y;
if (z == 1)
f[find(x)] = find(y);
else {
if (find(x) == find(y))
cout << "Y" << endl;
else
cout << "N" << endl;
}
}
return 0;
}
gcd
int gcd(ll a, ll b){
if (!b)return a;
else return gcd(b, a % b);
}
lcm
int lcm(int a,int b){
return a/gcd(a,b)*b;
}
dijkstra堆优化
#include<bits/stdc++.h>
#define INF 0x7fffffff
#define MAXN 1000086
#define MAXM 2000086
#define int long long
using namespace std;
int n,m,s,head[MAXN],cnt,dis[MAXN];
bool vis[MAXN];
struct edge{
int w,to,nxt;
}e[MAXM];
struct node{
int dis,id;
bool operator <(const node& x)const{
return x.dis<dis;
}
};
priority_queue<node>q;
void add(int u,int v,int w){
e[cnt].w=w;
e[cnt].to=v;
e[cnt].nxt=head[u];
head[u]=cnt++;
}
void init(){
for(int i=0;i<=n;i++){
head[i]=-1;
e[i].nxt=-1;
}
cnt=0;
}
void dijkstra(){
dis[s]=0;
q.push((node){0,s});
while(!q.empty()){
node cur=q.top();
q.pop();
int x=cur.id;
int d=cur.dis;
if(vis[x])continue;
vis[x]=1;
for(int i=head[x];~i;i=e[i].nxt){
int y=e[i].to;
if(vis[y])continue;
if(dis[y]>dis[x]+e[i].w){
dis[y]=dis[x]+e[i].w;
q.push((node){dis[y],y});
}
}
}
}
signed main(){
cin>>n>>m>>s;
init();
for(int i=0;i<=n;i++){
dis[i]=INF;
}
int u,v,w;
for(int i=1;i<=m;i++){
cin>>u>>v>>w;
add(u,v,w);
}
dijkstra();
for(int i=1;i<=n;i++)printf("%lld ",dis[i]);
return 0;
}
标签:普及,return,int,long,find,include,CSP,模板,define
From: https://www.cnblogs.com/DAIANZE/p/16709092.html