NOIP考前恶补模板,争取背下所有模板!!
本模板柱按照《算法竞赛进阶指南》的顺序,全部提供模板题链接以及标程,方便查(chao)阅(xi)。
#include<iostream>
#include<cstdio>
#include<cstring>
#define LL long long
#define FILEIN(s) freopen(s".in","r",stdin)
#define FILEOUT(s) freopen(s".out","w",stdout)
#define FILE(s) FILEIN(s);FILEOUT(s)
#define mem(s,v) memset(s,v,sizeof(s))
using namespace std;
template<class Type>
inline Type read(void){
Type 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*10+ch-'0';ch=getchar();}
return f*x;
}
LL a,b,p;
inline LL power(LL a,LL b,LL p){
LL ans=1%p;
for(;b;b>>=1){
if(b&1)ans=ans*a%p;
a=a*a%p;
}
return ans;
}
int main(){
a=read<LL>();b=read<LL>();p=read<LL>();
printf("%lld^%lld mod %lld=%lld\n",a,b,p,power(a,b,p));
return 0;
}
#include<iostream>
#include<cstdio>
#include<cstring>
#define LL long long
#define FILEIN(s) freopen(s".in","r",stdin)
#define FILEOUT(s) freopen(s".out","w",stdout)
#define FILE(s) FILEIN(s);FILEOUT(s)
#define mem(s,v) memset(s,v,sizeof(s))
using namespace std;
template<class Type>
inline Type read(void){
Type 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*10+ch-'0';ch=getchar();}
return f*x;
}
const int maxn=20;
#define eps 1e-6
int n;
double l,r,a[maxn];
inline double power(double a,int b){
double ans=1.0;
for(;b;b>>=1){
if(b&1)ans=ans*a;
a=a*a;
}
return ans;
}
inline double calc(double x){
double ans=0.0;
for(register int i=n;i>=0;--i){
ans+=power(x,i)*a[i];
}
return ans;
}
inline void solve(void){
while(r-l>=eps){
double K=(r-l)/3.0;
double lmid=l+K,rmid=r-K;
if(calc(lmid)<calc(rmid))l=lmid;
else r=rmid;
}
}
int main(){
n=read<int>();scanf("%lf%lf",&l,&r);
for(register int i=n;i>=0;--i){
scanf("%lf",a+i);
}
solve();
printf("%.5lf",l);
return 0;
}
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#define LL long long
#define FILEIN(s) freopen(s".in","r",stdin)
#define FILEOUT(s) freopen(s".out","w",stdout)
#define FILE(s) FILEIN(s);FILEOUT(s)
#define mem(s,v) memset(s,v,sizeof(s))
using namespace std;
template<class Type>
inline Type read(void){
Type 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*10+ch-'0';ch=getchar();}
return f*x;
}
const int maxn=1e5+5;
int n,m,a[maxn];
int f[maxn][25];
inline void init(void){
for(register int i=1;i<=n;++i)f[i][0]=a[i];
for(register int j=1;j<=20;++j){
for(register int i=1;i<=n-(1<<j)+1;++i){
f[i][j]=max(f[i][j-1],f[i+(1<<(j-1))][j-1]);
}
}
}
int main(){
n=read<int>();m=read<int>();
for(register int i=1;i<=n;++i){
a[i]=read<int>();
}
init();
while(m--){
int l=read<int>(),r=read<int>();
if(l>r)swap(l,r);
int k=log2(r-l+1);
printf("%d\n",max(f[l][k],f[r-(1<<k)+1][k]));
}
return 0;
}
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define LL long long
#define FILEIN(s) freopen(s".in","r",stdin)
#define FILEOUT(s) freopen(s".out","w",stdout)
#define FILE(s) FILEIN(s);FILEOUT(s)
#define mem(s,v) memset(s,v,sizeof(s))
using namespace std;
template<class Type>
inline Type read(void){
Type 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*10+ch-'0';ch=getchar();}
return f*x;
}
const int base=131,maxn=10005;
int n;
unsigned long long a[maxn];
char str[1005];
inline unsigned long long Hash(void){
int len=strlen(str+1);unsigned long long ret=0;
for(register int i=1;i<=len;++i){
ret=ret*base+str[i]-'a'+1;
}
return ret;
}
int main(){
n=read<int>();
for(register int i=1;i<=n;++i){
scanf("%s",str+1);
a[i]=Hash();
}
sort(a+1,a+n+1);
printf("%d\n",unique(a+1,a+n+1)-a-1);
return 0;
}
原文:https://www.cnblogs.com/little-aztl/p/9807617.html