一:【快读,快输 】
快读乃考试必备佳品,能大大优化程序运行时间。
#include<cstdio>
#define LD double
#define Re register int
inline void in(Re &x){
int f=0;x=0;char c=getchar();
while(c<'0'||c>'9')f|=c=='-',c=getchar();
while(c>='0'&&c<='9')x=(x<<1)+(x<<3)+(c^48),c=getchar();
x=f?-x:x;
}
inline void print(Re x){
if(x<0)putchar('-'),x=-x;
if(x>9)print(x/10);putchar(x%10+'0');
}
inline LD LDin(){
LD X=0,Y=1.0;int f=0;char c=getchar();
while(c<'0'||c>'9')f|=c=='-',c=getchar();
while(c>='0'&&c<='9')X=X*10+(c^48),c=getchar();
if(c=='.'){c=getchar();while(c>='0'&&c<='9')X+=(Y/=10)*(c^48),c=getchar();}
return f?-X:X;
}
int main(){}
二:【快速幂,龟速乘】
【模板】快速幂 \(||\) 取余运算 \([P1226]\)
这个有两种写法:二分,按进制分。按进制分要快些,二分数据范围要小些。
在快速幂的代码中需要做乘法,很有可能会乘爆,所以就需要用龟速乘来缩小数据范围,如果数据范围允许的话,就不用龟速乘,这样可以快些。
#include<cstdio>
#define LL long long
#define Re register int
int a,b,P;
inline int cf_flow(Re a,Re k){//分治 龟速乘
if(!k)return 0;
Re x=1,y=cf_flow(a,k>>1);
x=(LL)y+y%P;
if(k&1)x=(LL)x+a%P;
return x%P;
}
inline int cf(Re a,Re b){//按进制分 龟速乘
Re x=0;
while(b){
if(b&1)x=(x+a)%P;
a=a*2%P,b>>=1;
}
return x%P;
}
inline int mi_slow(Re a,Re k){//分治 快速幂
if(!k)return 1%P;
Re x=1,y=mi_slow(a,k>>1);
x=(LL)y*y%P;
if(k&1)x=(LL)x*a%P;
return x%P;
}
inline int mi(Re x,Re k){//按进制分 快速幂
Re s=1;
while(k){
if(k&1)s=(LL)s*x%P;
x=(LL)x*x%P,k>>=1;
}
return s%P;
}
int main(){}
三:【ST表求区间最值 (RMQ)】
【模板】求 \(m\) 区间内的最小值 \([P1440]\)
#include<algorithm>
#include<cstring>
#include<cstdio>
using namespace std;
const int N=1e5+3,inf=2147483647;
int i,j,n,k,m,a[N],lg[N],f1[N][18],f2[N][18];
int main(){
scanf("%d%d",&n,&k);
for(i=0;i<=17;i++)
for(j=0;j<=n+1;j++)
f1[i][j]=-inf,f2[i][j]=inf;
lg[1]=0;for(i=2;i<=n;i++)lg[i]=lg[i>>1]+1;m=lg[n];
for(i=1;i<=n;f2[i][0]=f1[i][0],i++)scanf("%d",&f1[i][0]);
for(i=1;i<=m;i++)
for(j=1;j+(1<<i)-1<=n;j++)
f1[j][i]=max(f1[j][i-1],f1[j+(1<<i-1)][i-1]),f2[j][i]=min(f2[j][i-1],f2[j+(1<<i-1)][i-1]);
m=lg[k];
for(i=1;i+k-1<=n;i++)printf("%d %d\n",max(f1[i][m],f1[i+k-(1<<m)][m]),min(f2[i][m],f2[i+k-(1<<m)][m]));
}
四:【归并排序】
#pragma GCC optimize(3,"Ofast","inline")
#pragma GCC optimize(2)
#include<cstdio>
#define Re register int
const int N=5e5+3;
int fu,n,s[N],b[N];long long gs;char ch;
inline void in(int &x){
fu=x=0;ch=getchar();
while(ch<'0'||ch>'9')fu|=ch=='-',ch=getchar();
while(ch>='0'&&ch<='9')x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
x=fu?-x:x;
}
void merge(Re L,Re mid,Re R){
Re i=L,j=mid+1,w=L;
while(i<=mid&&j<=R){
if(s[i]<=s[j])b[w++]=s[i++];
else b[w++]=s[j++],gs+=mid-i+1;
}
for(;i<=mid;i++)b[w++]=s[i];
for(;j<=R;j++)b[w++]=s[j];
for(i=L;i<=R;i++)s[i]=b[i];
}
inline void Sort(Re L,Re R){
if(L<R){
Re mid=L+R>>1;
Sort(L,mid),Sort(mid+1,R);
merge(L,mid,R);
}
}
int main(){
in(n);
for(Re i=1;i<=n;i++)in(s[i]);
Sort(1,n);
for(Re i=1;i<=n;i++)printf("%d ",s[i]);
// printf("%lld",gs);//输出逆序对个数
}
五:【快排】
排序直接用 \(\text{sort}\),求逆序对用归并,快排似乎没啥用哎╮(╯▽╰)╭。
#include<bits/stdc++.h>
using namespace std;
int data[100100],n;
void mysort(int left, int right){
int mid=data[(left+right)/2];
int i=left,j=right;
while(i<=j){
while(data[i]<mid)i++;
while(data[j]>mid)j--;
if(i<=j)swap(data[i++],data[j--]);
}
if(j>left)mysort(left,j);
if(i<right)mysort(i,right);
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;i++)scanf("%d",&data[i]);
mysort(1,n);
for(int i=1;i<=n;i++)printf("%d ",data[i]);
}
六:【n 进制转 m 进制】
这个感觉很少用到哎。。
#include<cstdio>
const int N=1e5+3;
int n,m,x,tmp,ansl;char a[N],ans[N];
inline int n_to_ten(){
int x=0;
for(int i=0;a[i];++i){
x*=n;
if (a[i]>='A'&&a[i]<='F')x+=(a[i]-'A'+10);
else x+=(a[i]-'0');
}
return x;
}
inline void ten_to_m(){
do{
tmp=x%m;
if(tmp<10)ans[++ansl]='0'+tmp;
else ans[++ansl]='A'+tmp-10;
x/=m;
}while(x);
}
int main(){
scanf("%d%s%d",&n,a,&m);
x=n_to_ten();
ten_to_m();
for(int i=ansl;i;--i)printf("%c",ans[i]);puts("");
}
七:【高精度加法(普通写法)】
【模板】\(\text{A+B Problem}\)(高精)\(\text{[P1303]}\)
#include<bits/stdc++.h>
using namespace std;
int a[1000],b[1000],c[1000];
char st[1000];
int main(){
scanf("%s",st);
int lena,lenb,lenc;
lena =strlen(st);
for(int i=1;i<=lena;i++)a[i]=st[lena-i]-'0';
scanf("%s",st);
lenb=strlen(st);
for(int i=1;i<=lenb;i++)b[i]=st[lenb-i]-'0';
if(lena>lenb)lenc=lena;
else lenc=lenb;
for(int i=1;i<=lenc;i++){
c[i]=a[i]+b[i]+c[i];
c[i+1]=c[i+1]+c[i]/10;
c[i]=c[i]%10;
}
if(c[lenc+1])lenc++;
for(int i=lenc;i>0;i--)printf("%d",c[i]);
}
八:【高精度乘法(普通写法)】
【模板】\(\text{A+B Problem}\)(高精)\(\text{[P1303]}\)
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;
int main(){
char a1[10005],b1[10005];
int a[100005],b[100005],c[100005],lena,lenb,lenc,i,j,x;
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
memset(c,0,sizeof(c));
gets(a1);gets(b1);
lena=strlen(a1);lenb=strlen(b1);
for(i=0;i<=lena-1;i++) a[lena-i]=a1[i]-48;
for(i=0;i<=lenb-1;i++) b[lenb-i]=b1[i]-48;
for(i=1;i<=lena;i++){
x=0;
for(j=1;j<=lenb;j++){
c[i+j-1]=a[i]*b[j]+x+c[i+j-1];
x=c[i+j-1]/10;
c[i+j-1] %= 10;
}
c[i+lenb]=x;
}
lenc=lena+lenb;
while(c[lenc]==0&&lenc>1)lenc--;
for(i=lenc;i>=1;i--)cout<<c[i];
cout<<endl;
}
九:【高精全模板(结构体封装)】
【模板】\(\text{A+B Problem}\)(高精)\(\text{[P1601]}\)
【模板】\(\text{A*B Problem}\)(高精)\(\text{[P1303]}\)
【模板】\(\text{A/B Problem}\)(高精)\(\text{[P1480]}\)
【模板】\(\text{A+B A-B A*B A/B A%B Problem [P1932]}\)
代码量略大,疯狂压了波行,还是有两百多行。
如果嫌代码太长,可以吧一些不必要的删掉,只是要注意像除法,取模这样的运算需要用到减法,加法,所以像只留除,模,删掉加,减这样的行为会让你编译不过的。
#include<cstring>
#include<cstdio>
#define LL long long
#define Re register int
using namespace std;
const int base=1e8;
const int N=1e4+10;
int aux[N<<3];
struct Int{
int s[N],l;
inline void CL(){l=0;memset(s,0,sizeof(s));}
inline void pr(){
printf("%d",s[l]);
for(Re i=l-1;i;i--)printf("%08d",s[i]);
}
inline void re_l(){
Re i,x=0,k=1,L=0,fl,o;char c=getchar();
for(;c<'0'||c>'9';c=getchar());
for(;c>='0'&&c<='9';c=getchar()){if(!(L-1)&&!aux[L])L--;aux[++L]=c-'0';}
CL();l=L/8+((o=L%8)>0);
for(i=1;i<=o;i++)x=x*10+aux[i];
if(o)s[l]=x;fl=!o?l+1:l;
for(i=o+1,x=0;i<=L;i++,k++){
x=x*10+aux[i];
if(!(k^8))s[--fl]=x,x=k=0;
}
if(!l)l=1;
}
inline LL toint(){
LL x=0;
for(Re i=l;i;i--)x=x*base+s[i];
return x;
}
inline Int operator=(Re b){
CL();
do s[++l]=b%base,b/=base;while(b>0);
return *this;
}
inline Int operator=(LL b){
CL();
do s[++l]=b%base,b/=base;while(b>0);
return *this;
}
inline Int operator+(const Re &b){
Int c=*this;LL x=b;
for(Re i=1;i<=l&&x;i++)x=x+c.s[i],c.s[i]=x%base,x/=base;
if(x)c.s[++c.l]=x;
return c;
}
inline Int operator+(const LL &b){
Int c=*this;LL x=b;
for(Re i=1;i<=l&&x;i++)x=x+c.s[i],c.s[i]=x%base,x/=base;
if(x)c.s[++c.l]=x;
return c;
}
inline Int operator+(Int &b){
if(b.l<3)return *this+b.toint();
Int c;LL x=0;Re k=l<b.l?b.l:l;c.CL(),c.l=k;
for(Re i=1;i<=k;i++)x=x+s[i]+b.s[i],c.s[i]=x%base,x/=base;
if(x)c.s[++c.l]=x;
return c;
}
inline Int operator-(const Int &b){
Int c,d=*this;LL x=0;c.CL();
for(Re i=1;i<=l;i++){
if((x=d.s[i])<b.s[i])d.s[i+1]--,x+=base;
c.s[i]=x-b.s[i];
}
c.l=l;
for(;!c.s[c.l]&&c.l>1;c.l--);
return c;
}
inline Int operator-(const Re &b){Int c;return *this-(c=b);}
inline Int operator-(const LL&b){Int c;return *this-(c=b);}
inline Int operator*(const Re &b){
Int c;LL x=0;c.CL();
for(Re i=1;i<=l;i++)x=x+1LL*s[i]*b,c.s[i]=x%base,x/=base;
for(c.l=l;x;x/=base)c.s[++c.l]=x%base;
return c;
}
inline Int operator*(Int&b){
if(b.l<2)return *this*b.toint();
Int c;LL x;Re i,j,k;c.CL();
for(i=1;i<=l;i++){
x=0;
for(j=1;j<=b.l;j++)x=x+1LL*s[i]*b.s[j]+c.s[k=i+j-1],c.s[k]=x%base,x/=base;
if(x)c.s[i+b.l]=x;
}
for(c.l=l+b.l;!c.s[c.l]&&c.l>1;c.l--);
return c;
}
inline Int operator*(const LL &b){
Int c;
if(b>2e9){c=b;return *this*c;}
LL x=0;c.CL();
for(Re i=1;i<=l;i++)x=x+b*s[i],c.s[i]=x%base,x/=base;
for(c.l=l;x;x/=base)c.s[++c.l]=x%base;
return c;
}
inline Int operator/(const Re &b){
Int c;LL x=0;c.CL();
for(Re i=l;i;i--)c.s[i]=(x*base+s[i])/b,x=(x*base+s[i])%b;
for(c.l=l;!c.s[c.l]&&c.l>1;c.l--);
return c;
}
inline Int operator/(const LL&b){
Int c;LL x=0;c.CL();
for(Re i=l;i;i--)c.s[i]=(x*base+s[i])/b,x=(x*base+s[i])%b;
for(c.l=l;!c.s[c.l]&&c.l>1;c.l--);
return c;
}
inline Int operator/(Int&b){
if(b.l<2)return *this/b.toint();
Int c,d;Re i,j,le,r,mid,k;c.CL();d.CL();
for(i=l;i;i--){
for(j=++d.l;j>1;j--)d.s[j]=d.s[j-1];
d.s[1]=s[i];
if(d<b)continue;
le=k=0;r=base-1;
while(le<=r){
mid=(le+r)>>1;
if(b*mid<=d)le=mid+1,k=mid;
else r=mid-1;
}
c.s[i]=k,d=d-b*k;
}
for(c.l=l;!c.s[c.l]&&c.l>1;c.l--);
return c;
}
inline Int operator%(const Re &b){
Int c;LL x=0;c.CL();
for(Re i=l;i;i--)x=(x*base+s[i])%b;
return c=x;
}
inline Int operator%(const LL&b){
Int c;LL x=0;c.CL();
for(Re i=l;i;i--)x=(x*base+s[i])%b;
return c=x;
}
inline Int operator%(Int&b){
if(b.l<2)return *this%b.toint();
Int c;Re i,j,le,r,mid,k;c.CL();
for(i=l;i;i--){
for(j=++c.l;j>1;j--)c.s[j]=c.s[j-1];
c.s[1]=s[i];
if(c<b)continue;
le=k=0,r=base-1;
while(le<=r){
mid=(le+r)>>1;
if(b*mid<=c)le=mid+1,k=mid;
else r=mid-1;
}
c=c-b*k;
}
for(;!c.s[c.l]&&c.l>1;c.l--);
return c;
}
inline Int operator+=(Int&b){return *this=*this+b;}
inline Int operator+=(LL&b){return *this=*this+b;}
inline Int operator+=(Re &b){return *this=*this+b;}
inline Int operator-=(Int&b){return *this=*this-b;}
inline Int operator-=(LL&b){return *this=*this-b;}
inline Int operator-=(Re &b){return *this=*this-b;}
inline Int operator*=(Int&b){return *this=*this*b;}
inline Int operator*=(LL&b){return *this=*this*b;}
inline Int operator*=(Re &b){return *this=*this*b;}
inline Int operator/=(Int&b){return *this=*this/b;}
inline Int operator/=(LL&b){return *this=*this/b;}
inline Int operator/=(Re &b){return *this=*this/b;}
inline Int operator%=(Int&b){return *this=*this%b;}
inline Int operator%=(LL&b){return *this=*this%b;}
inline Int operator%=(Re &b){return *this=*this%b;}
inline bool operator<(const Int&b)const{
if(l^b.l)return l<b.l;
for(Re i=l;i;i--)if(s[i]^b.s[i])return s[i]<b.s[i];
return 0;
}
inline bool operator<=(const Int&b)const{
if(l^b.l)return l<b.l;
for(Re i=l;i;i--)if(s[i]^b.s[i])return s[i]<b.s[i];
return 1;
}
inline bool operator>(const Int&b)const{
if(l^b.l)return l>b.l;
for(Re i=l;i;i--)
if(s[i]^b.s[i])return s[i]>b.s[i];
return 0;
}
inline bool operator>=(const Int&b)const{
if(l^b.l)return l>b.l;
for(Re i=l;i;i--)if(s[i]^b.s[i])return s[i]>b.s[i];
return 1;
}
inline bool operator==(const Int&b)const{
if(l^b.l)return 0;
for(Re i=l;i;i--)if(s[i]^b.s[i])return 0;
return 1;
}
inline bool operator!=(const Int&b)const{
if(l^b.l)return 1;
for(Re i=l;i;i--)if(s[i]^b.s[i])return 1;
return 0;
}
inline bool operator<(LL b)const{Int c;return *this<(c=b);}
inline bool operator<=(LL b)const{Int c;return *this<=(c=b);}
inline bool operator>(LL b)const{Int c;return *this>(c=b);}
inline bool operator>=(LL b)const{Int c;return *this>=(c=b);}
inline bool operator==(LL b)const{Int c;return *this==(c=b);}
inline bool operator!=(LL b)const{Int c;return *this!=(c=b);}
inline bool operator<(Re b)const{Int c;return *this<(c=b);}
inline bool operator<=(Re b)const{Int c;return *this<=(c=b);}
inline bool operator>(Re b)const{Int c;return *this>(c=b);}
inline bool operator>=(Re b)const{Int c;return *this>=(c=b);}
inline bool operator==(Re b)const{Int c;return *this==(c=b);}
inline bool operator!=(Re b)const{Int c;return *this!=(c=b);}
};
Int a,b;
int main(){
a.re_l();
b.re_l();
a.pr();printf("+");b.pr();printf("=");(a+b).pr();printf("\n");
a.pr();printf("-");b.pr();printf("=");
if(a<b){putchar('-');(b-a).pr();}
else(a-b).pr();printf("\n");
a.pr();printf("*");b.pr();printf("=");(a*b).pr();printf("\n");
a.pr();printf("/");b.pr();printf("=");(a/b).pr();printf("\n");
a.pr();printf("%%");b.pr();printf("=");(a%b).pr();printf("\n");
if(a<b)printf("a<b\n");
if(a<=b)printf("a<=b\n");
if(a>b)printf("a>b\n");
if(a>=b)printf("a>=b\n");
if(a==b)printf("a==b\n");
if(a!=b)printf("a!=b\n");
}
十:【模拟退火】
(1).【坐标系随机移动】
【模板】 \(\text{A Star not a Tree? [UVA10228]}\)
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#define LD double
#define LL long long
#define Re register int
using namespace std;
const int N=1e6+3;
int n,HYJ,X[N],Y[N];
inline void in(Re &x){
int f=0;x=0;char c=getchar();
while(c<'0'||c>'9')f|=c=='-',c=getchar();
while(c>='0'&&c<='9')x=(x<<1)+(x<<3)+(c^48),c=getchar();
x=f?-x:x;
}
inline LD calc(LD x,LD y){
LD tmp=0;
for(Re i=1;i<=n;++i)tmp+=sqrt((x-X[i])*(x-X[i])+(y-Y[i])*(y-Y[i]));
return tmp;
}
inline void Srand(Re seed){
srand(seed);Re a=rand()%10+1;
while(a--)srand(rand());
}
LD ans,ansx,ansy;
const LD deltaT=0.99,maxT=4995,minT=1e-12;
inline void fire(){
LD x=ansx,y=ansy,nowT=maxT;
while(nowT>minT){
LD nx=x+((rand()<<1)-RAND_MAX)*nowT;
LD ny=y+((rand()<<1)-RAND_MAX)*nowT;
LD nans=calc(nx,ny),deltaE=nans-ans;
if(deltaE<0)ans=nans,ansx=x=nx,ansy=y=ny;
else if(exp(-deltaE/nowT)>(LD)rand()/RAND_MAX)x=nx,y=ny;
nowT*=deltaT;
}
}
int main(){
// freopen("123.txt","r",stdin);
in(HYJ),Srand(20030825);
while(HYJ--){
in(n);
for(Re i=1;i<=n;++i)in(X[i]),in(Y[i]);
ans=calc(X[1],Y[1]),ansx=X[1],ansy=Y[1];
fire(),fire(),fire(),fire(),fire();
printf("%.lf\n",ans);
if(HYJ)puts("");
}
}
(2).【序列随机swap】
【模板】 均分数据 \(\text{[HAOI2006] [P2503]}\)
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#define LD double
#define LL long long
#define Re register int
using namespace std;
const int N=23;
int n,m,S,A[N];LD x0;
inline void in(Re &x){
int f=0;x=0;char c=getchar();
while(c<'0'||c>'9')f|=c=='-',c=getchar();
while(c>='0'&&c<='9')x=(x<<1)+(x<<3)+(c^48),c=getchar();
x=f?-x:x;
}
inline void Srand(Re seed){
srand(seed);Re a=rand()%10+1;
while(a--)srand(rand());
}
inline void Random(Re *A,Re n){
for(Re i=1;i<=n;++i)swap(A[i],A[rand()%n+1]);
}
int x[N];
inline LD calc(Re *A){
for(Re i=1;i<=m;++i)x[i]=0;
for(Re i=1;i<=n;++i){
Re w=1;
for(Re j=2;j<=m;++j)if(x[j]<x[w])w=j;
x[w]+=A[i];
}
LD tmp=0;
for(Re i=1;i<=m;++i)tmp+=(x[i]-x0)*(x[i]-x0);
return tmp/m;
}
int nowA[N],ansA[N];LD ans;
const LD deltaT=0.995,maxT=4995,minT=1e-15;
inline void fire(){
for(Re i=1;i<=n;++i)nowA[i]=ansA[i];LD nowT=maxT;
while(nowT>minT){
Re x=rand()%n+1,y=rand()%n+1;
swap(nowA[x],nowA[y]);
LD nans=calc(nowA),deltaE=nans-ans;
if(deltaE<0){for(Re i=1;i<=n;++i)ansA[i]=nowA[i];ans=nans;}
else if(exp(-deltaE/nowT)>(LD)rand()/RAND_MAX);
else swap(nowA[x],nowA[y]);
nowT*=deltaT;
}
}
int main(){
// freopen("123.txt","r",stdin);
in(n),in(m);
for(Re i=1;i<=n;++i)in(A[i]),S+=A[i];x0=(LD)S/m;
for(Re i=1;i<=n;++i)ansA[i]=A[i];
Srand(965579261),Random(ansA,n),Srand(20030825);
ans=calc(ansA),fire(),fire(),fire(),fire(),fire();
printf("%.2lf\n",sqrt(ans));
}
十一:【Linux 对拍】
#!/bin/bash #背下来
g++ data.cpp -o data
g++ std.cpp -o std
g++ myprogram.cpp -o myprogram
while true; do
./data>Circuit.in #数据生成器输出数据重定向到Circuit.in
./myprogram #待测程序
./std #正确程序
if diff Circuit.out std.out; then #比较两个文件
printf AC #正确输出AC
else
printf WA #错误输出WA
#cat Circuit.out std.out #显示两个文件
exit 0 #退出
fi #结束if
done #结束while