1.快排
struct node
{
int id;
long long int val;
} a[1000001], e;
int vis[1000001];
void sort(int l, int r)
{
int left = l, right = r, i;
int t = a[(l + r) >> 1].val;
while (left <= right)
{
while (a[left].val < t && left <= right)
{
left++;
}
while (a[right].val > t && left <= right)
{
right--;
}
if (left <= right)
{
e.id = a[left].id;
e.val = a[left].val;
a[left].id = a[right].id;
a[left].val = a[right].val;
a[right].id = e.id;
a[right].val = e.val;
left++, right--;
}
}
if (l < right)
{
sort(l, right);
}
if (left < r)
{
sort(left, r);
}
return;
}
struct node
{
int m;
int w[10001];
} a[101];
void sort(int x, int l, int r)
{
int left = l, right = r, u, v;
int t = a[x].w[(l + r) >> 1];
while (left <= right)
{
while (a[x].w[left] < t && left <= right)
{
left++;
}
while (a[x].w[right] > t && left <= right)
{
right--;
}
if (left <= right)
{
u = a[x].w[left];
a[x].w[left] = a[x].w[right];
a[x].w[right] = u;
left++, right--;
}
}
if (l < right)
{
sort(x, l, right);
}
if (left < r)
{
sort(x, left, r);
}
return;
}
- 快速幂
long long int qs(long long int a, long long int k)
{
long long int ans = 1;
while (k)
{
if (k & 1)
{
ans = ans * a;
}
a = a * a;
if(ans>n)
{
break;
}
k >>= 1;
}
return ans;
}
取模:
int md=1000000007;
long long int qsm(long long int a,int b)
{
long long int ans=1;
while(b)
{
if(b&1)
{
ans=(ans%md)*(a%md);
}
a=(a%md)*(a%md);
b>>=1;
}
return ans%md;
}
3.最大公因数
int gcd(int a, int b)
{
int r = a % b;
while (r)
{
a = b;
b = r;
r = a % b;
}
return b;
}
4.欧拉筛法
int prime[1000001],isprime[1000001],cnt=0;
void init()
{
int i,j;
for(i=2;i<1000001;i++)
{
if(!isprime[i])
{
prime[cnt++]=i;
isprime[i]=1;
}
for(j=0;j<cnt;j++)
{
if(i*prime[j]>1000000)
{
break;
}
isprime[i*prime[j]]=1;
if(i%prime[j]==0)
{
break;
}
}
}
}
5.队列:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
using namespace std;
struct lis
{
int cou,num;
}e,o;
queue<struct lis>q;
struct node
{
int u,v,w;
int next;
}a[200001];
int cnt=1,h[100001]={0},vis[100001]={0};
void add(int u,int v)
{
a[cnt].u=u,a[cnt].v=v,a[cnt].w=1;
a[cnt].next=h[u];
h[u]=cnt++;
}
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("in.txt", "r", stdin);
#endif
int n,m,i,j,u,v;
scanf("%d %d",&n,&m);
for(i=0;i<m;i++)
{
scanf("%d %d",&u,&v);
add(u,v);
add(v,u);
}
e.cou=1,e.num=0;
q.push(e);
vis[e.cou]=1;
while(!q.empty())
{
e=q.front();
q.pop();
if(e.cou==n)
{
break;
}
for(i=h[e.cou];i!=0;i=a[i].next)
{
o.cou=a[i].v;
if(vis[o.cou]==0)
{
vis[o.cou]=1;
o.num=e.num+1;
q.push(o);
}
}
}
printf("%d\n",e.num-1);
return 0;
}
6.哈希:
#include <stdio.h>
#include <string.h>
typedef unsigned long long int ull;
char s[10001];
ull a[10001];
int prime = 233317;
ull base = 131;
ull mod = 212370440130137957ll;
ull HASH()
{
ull ans = 0;
int i, j, len = strlen(s);
for (i = 0; i < len; i++)
{
ans = (ans * base + ((ull)s[i])) % mod + prime;
}
return ans;
}
void sort(int l, int r)
{
int left = l, right = r, i;
ull t = a[(l + r) >> 1];
while (left <= right)
{
while (a[left] < t && left <= right)
{
left++;
}
while ((ull)a[right] > (ull)t && left <= right)
{
right--;
}
if (left <= right)
{
ull tmp = a[left];
a[left] = a[right];
a[right] = tmp;
left++, right--;
}
}
if (l < right)
{
sort(l, right);
}
if (left < r)
{
sort(left, r);
}
return;
}
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("in.txt", "r", stdin);
#endif
char st[11];
int i, j, ans = 0;
int n;
scanf("%d", &n);
for (i = 0; i < n; i++)
{
scanf("%s", s);
fgets(st, 11, stdin);
a[i] = HASH();
}
sort(0, n - 1);
for (i = 0; i < n; i++)
{
if (a[i] != a[i + 1])
{
ans++;
}
}
printf("%d\n", ans);
return 0;
}
7.树状数组:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int a[100001]={0}, n, p, N = 100000 + 5;
long long int sum[2][100001]={0};
int lowit(int x)
{
return x & -x;
}
void add(int k, int x, int val)
{
int i = x;
while (i < N)
{
sum[k][i] += val;
i += lowit(i);
}
}
long long int quarry(int k, int x)
{
int i = x;
long long int ans = 0;
while (i >= 1)
{
ans += sum[k][i];
i -= lowit(i);
}
return ans;
}
int main()
{
#ifdef ONLINE_JUDGE
#else
freopen("in.txt", "r", stdin);
#endif
int i, j, u, v;
long long int ans = 0, cnt1 = 0, tot1 = 0, cnt2 = 0, tot2 = 0, cnt3 = 0, tot3 = 0, cnt4 = 0, tot4 = 0;
scanf("%d %d", &n, &p);
for (i = 1; i < n + 1; i++)
{
scanf("%d", &a[i]);
add(0, a[i], 1);
add(1, a[i], a[i]);
}
while (p--)
{
ans = 0;
scanf("%d %d", &u, &v);
if (u > v)
swap(u, v);
int mid = (u + v) / 2;
cnt1 = quarry(0, u);
tot1 = quarry(1, u);
ans += cnt1 * u - tot1;
cnt2 = quarry(0, mid);
tot2 = quarry(1, mid);
ans += tot2 - tot1 - (u) * (cnt2 - cnt1);
cnt3 = quarry(0, v);
tot3 = quarry(1, v);
ans += v * (cnt3 - cnt2) - (tot3 - tot2);
cnt4 = quarry(0, 100001);
tot4 = quarry(1, 100001);
ans += tot4 - tot3 - (cnt4 - cnt3) * v;
printf("%lld\n", ans);
}
return 0;
}
8.求逆元:
long long int qp(long long int a,long long int b,long long int mod)
{
long long ans=1;
while(b)
{
if(b&1)
{
ans=ans*a;
ans%=mod;
}
a=a*a;
a%=mod;
b>>=1;
}
return ans;
}
v=qp(v,mod-2,mod);