跳到正文

剪贴板

剪贴板 96t9kgev

原文
剪贴板信息

剪贴板信息

收录
更新

模板代码(算法专辑):

初始程序:

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
int main() {
	ios::sync_with_stdio(0);
	return 0;
}

万能头源码(用于交 HDU 等 OJ)

C++
// C++ includes used for precompiling -*- C++ -*-

// Copyright (C) 2003-2013 Free Software Foundation, Inc.
//
// This file is part of the GNU ISO C++ Library.  This library is free
// software; you can redistribute it and/or modify it under the
// terms of the GNU General Public License as published by the
// Free Software Foundation; either version 3, or (at your option)
// any later version.

// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// Under Section 7 of GPL version 3, you are granted additional
// permissions described in the GCC Runtime Library Exception, version
// 3.1, as published by the Free Software Foundation.

// You should have received a copy of the GNU General Public License and
// a copy of the GCC Runtime Library Exception along with this program;
// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
// <Licenses - GNU Project - Free Software Foundation>.

/** @file stdc++.h
 *  This is an implementation file for a precompiled header.
 */

// 17.4.1.2 Headers

// C
#ifndef _GLIBCXX_NO_ASSERT
#include <cassert>
#endif
#include <cctype>
#include <cerrno>
#include <cfloat>
#include <ciso646>
#include <climits>
#include <clocale>
#include <cmath>
#include <csetjmp>
#include <csignal>
#include <cstdarg>
#include <cstddef>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>

#if __cplusplus >= 201103L
#include <ccomplex>
#include <cfenv>
#include <cinttypes>
#include <cstdalign>
#include <cstdbool>
#include <cstdint>
#include <ctgmath>
#include <cwchar>
#include <cwctype>
#endif

// C++
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>

#if __cplusplus >= 201103L
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#endif

快速排序:

C++
void quicksort(int a[],int l,int r){
    int i=l,j=r,flag=a[(l+r)/2];
    do{
        while(a[i]<flag) i++;
        while(a[j]>flag) j--;
        if(i<=j) swap(a[i++],a[j--]);
    }while(i<=j);
    if(l<j) quicksort(a,l,j);
    if(i<r) quicksort(a,i,r);
}

基数排序:

C++
int buc[310],b[int(1e5)+10],a[int(1e5)+10],n;
void radix_sort(int a[],int l,int r){
	int radix=1;
	for(int i=1;i<=4;i++){
		memset(buc,0,sizeof buc);
		for(int j=l;j<=r;j++) buc[a[j]/radix%256]++;
		for(int j=0;j<256;j++) buc[j]+=buc[j-1];
		for(int j=r;j>=l;j--) b[l+(buc[a[j]/radix%256]--)-1]=a[j];
		for(int j=l;j<=r;j++) a[j]=b[j];
		radix*=256;
	}
}

归并排序:

C++
int b[int(1e5)+10];
void msort(int a[],int l,int r){
	if(l==r) return;
	int mid=l+r>>1,i=l,j=mid+1,k=l;
	msort(a,i,mid),msort(a,mid+1,r);
	while(i<=mid && j<=r)
		if(a[i]<=a[j]) b[k++]=a[i++];
		else b[k++]=a[j++];
	while(i<=mid) b[k++]=a[i++];
	while(j<=r) b[k++]=a[j++];
	for(int p=l;p<=r;p++)
		a[p]=b[p];
}

高精:

C++
#include<bits/stdc++.h>
using namespace std;
struct node{
	int a[310];
	int len;
	node(int x=0){
		memset(a,0,sizeof a);
		for(len=1;x;len++)
			a[len]=x%10,x/=10;
		len--;
	}
	int &operator [] (int i){
		return a[i];
	}
	void fl(int l){
		len=l;
		for(int i=1;i<=len;i++)
			a[i+1]+=a[i]/10,a[i]%=10;
		for( ;!a[len]; )
			len--;
	}
};
void input(node &a){
	string s;
	cin>>s;
	a.len=s.size();
	for(int i=s.size()-1;i>=0;i--)
		a[s.size()-i]=s[i]-'0';
}
void print(node x){
	for(int i=max(1,x.len);i>=1;i--)
		cout<<x[i];
	cout<<"\n";
}
bool operator == (node x,node y){
	if(x.len!=y.len) return false;
    for(int i=x.len;i>=1;i--)
        if(x[i]!=y[i]) return false;
  	return true;
}
bool operator >= (node x,node y){
	if(x.len!=y.len) return x.len>y.len;
	for(int i=x.len;i>=1;i--)
		if(x[i]!=y[i]) return x[i]>y[i];
	return true;
}
node operator + (node x,node y){
	node c;
	c.len=max(x.len,y.len);
	for(int i=1;i<=c.len;i++)
		c[i]=x[i]+y[i];
	c.fl(c.len+2);
	return c;
}
node operator - (node x,node y){
	node c;
	int len=max(x.len,y.len);
	for(int i=1;i<=len;i++)
		c[i]=x[i]-y[i];
	for(int i=1;i<=len;i++)
		while(c[i]<0) c[i]+=10,c[i+1]--;
	c.len=len;
	for( ;!c[c.len]; )
		c.len--;
	return c;
}
node operator * (node x,int y){
	node c;
	for(int i=1;i<=x.len;i++)
		c[i]=x[i]*y;
	c.fl(x.len+12);
	return c;
}
node operator / (node x,node y){
	node u,c;
	for(int i=x.len;i>=1;i--){
		u=u*10+node(x[i]);
		while(u>=y){
			u=u-y;
			c[i]++;
		}
	}
	c.fl(x.len+1);
	return c;
}
node operator % (node x,node y){
	node u,c;
	for(int i=x.len;i>=1;i--){
		u=u*10+node(x[i]);
		while(u>=y){
			u=u-y;
			c[i]++;
		}
	}
	return u;
}
node operator * (node x,node y){
	node c;
	int len=x.len+y.len;
	for(int i=1;i<=x.len;i++)
		for(int j=1;j<=y.len;j++)
			c[i+j-1]+=x[i]*y[j];
	c.fl(len);
	return c;
}
int main() {
	return 0;
}

快读 & 快写:

C++
inline long long read(){
	long long x=0,f=1;char ch=getchar();
	while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
	while (ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
	return x*f;
}
inline void write(long long x){
	if(x<0){
		putchar('-');
		write(-x);
		return;
	}
	if(x<10){
		putchar(x%10+'0');
		return;
	}
	write(x/10);
	putchar((x%10)^48);
}

快速幂:

C++
#include<bits/stdc++.h>
using namespace std;
long long a,b,p;
long long quickpow(long long x,long long y,long long m){
	if(y==0) return 1ll%m;
	long long tot=quickpow(x,y/2,m);
	if(y & 1) return tot*tot%m*x%m;
	else return tot*tot%m;
}
int main() {
	cin>>a>>b>>p;
	printf("%d^%d mod %d=%d",a,b,p,quickpow(a,b,p));
	return 0;
}

整数二分答案(模板一):

C++
while(l<=r){
	mid=l+r>>1;
	if(p(mid))
		ans=mid,l=mid+1;
	else
		r=mid-1;
}
cout<<ans;

整数二分答案(模板二):

C++
while(l<r){
  mid=l+r+1>>1;
  if(p(mid))
    l=mid;
  else
    r=mid+1;
}
cout<<l;

整数二分答案(模板三):

C++
while(l+1<r){
  mid=l+r>>1;
  if(p(mid))
    l=mid;
  else
    r=mid;
}
cout<<l;

实数二分答案:

C++
while(r-l>eps){
  mid=(l+r)/2;
  if(p(mid))
    l=mid;
  else
    r=mid;
}
cout<<l;