标签:else stream namespace 关系 a* pre iostream swap dig
快速幂
//By Menteur_Hxy
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define F(i,a,b) for(register int i=(a);i<=(b);i++)
using namespace std;
typedef long long LL;
LL read() {
LL x=0,f=1; char c=getchar();
while(!isdigit(c)) {if(c=='-')f=-f;c=getchar();}
while(isdigit(c)) x=(x<<1)+(x<<3)+c-48,c=getchar();
return x*f;
}
LL n,m,k,ans;
LL qpow(LL a,LL b,LL MOD) {
LL res=1;
while(b) {
if(b&1) res=res*a%MOD;
a=a*a%MOD; b>>=1;
}
return res;
}
int main() {
n=read(),m=read(),k=read(),ans=read();
(ans+=m*qpow(10,k,n)%n)%=n;
printf("%lld\n",ans);
return 0;
}
建立映射关系,求逆序对个数
//By Menteur_Hxy
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define F(i,a,b) for(register int i=(a);i<=(b);i++)
#define R(i,a,b) for(register int i=(b);i>=(a);i--)
using namespace std;
typedef long long LL;
LL read() {
LL x=0,f=1; char c=getchar();
while(!isdigit(c)) {if(c=='-')f=-f;c=getchar();}
while(isdigit(c)) x=(x<<1)+(x<<3)+c-48,c=getchar();
return x*f;
}
const LL N=100010,MOD=99999997;
int n;
int to[N],C[N];
LL ans;
struct Dat{LL id,da;}A[N],B[N];
struct BIT{
LL da[N];
BIT() {memset(da,0,sizeof(da));}
void upd(int x,int k) {for(;x<=N;x+=(x&-x)) da[x]+=k;}
LL qry(int x) {LL res=0;for(;x>0;x-=(x&-x)) res+=da[x];return res;}
}T;
bool cmp(Dat a,Dat b) {return a.da<b.da;}
int main() {
n=read();
F(i,1,n) A[i].id=i,A[i].da=read();
F(i,1,n) B[i].id=i,B[i].da=read();
sort(A+1,A+1+n,cmp);
sort(B+1,B+1+n,cmp);
F(i,1,n) C[B[i].id]=A[i].id;
R(i,1,n) {
ans+=T.qry(C[i]-1);
T.upd(C[i],1);
}
printf("%lld",ans%MOD);//忘%wa一次QAQ
return 0;
}
建最大生成树,对询问找lca,暴力
//By Menteur_Hxy
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define F(i,a,b) for(register int i=(a);i<=(b);i++)
#define R(i,a,b) for(register int i=(b);i>=(a);i--)
#define E(i,u) for(register int i=head[u],v;i;i=T[i].nxt)
using namespace std;
typedef long long LL;
LL read() {
LL x=0,f=1; char c=getchar();
while(!isdigit(c)) {if(c=='-')f=-f;c=getchar();}
while(isdigit(c)) x=(x<<1)+(x<<3)+c-48,c=getchar();
return x*f;
}
const int N=10010,M=50010,INF=0x3f3f3f3f;
int n,m,q,cnt;
int fa[N],f[N][33],dis[N],head[N],dep[N],vis[N];
struct Edg{int fr,to,cst,nxt;}E[M],T[N<<1];
bool cmp(Edg a,Edg b) {return a.cst>b.cst;}
int getf(int x) {return fa[x]==x?x:fa[x]=getf(fa[x]);}
void dfs(int u,int pre) {
vis[u]=1;
E(i,u) if((v=T[i].to)!=pre) {//T,E不分QAQ
dep[v]=dep[u]+1;
f[v][0]=u;
dis[v]=T[i].cst;
dfs(v,u);
}
}
int getm(int x,int lca) {
int res=INF;
while(x!=lca) {
res=min(res,dis[x]);
x=f[x][0];
}
return res;
}
int lca(int x,int y) {
int l=x,r=y;
if(dep[x]<dep[y]) swap(x,y);
int nd=dep[x]-dep[y];
for(int i=0;nd;nd>>=1,i++) if(nd&1) x=f[x][i];
if(x!=y) {
R(i,0,32) if(f[x][i]!=f[y][i])
x=f[x][i],y=f[y][i];
x=f[x][0];
}
return min(getm(l,x),getm(r,x));
}
int main() {
n=read(),m=read();
F(i,1,m) {
int a=read(),b=read(),c=read();
E[i]=(Edg){a,b,c};
}
sort(E+1,E+1+m,cmp);
F(i,1,n) fa[i]=i;
F(i,1,m) {
int fu=getf(E[i].fr),fv=getf(E[i].to);
if(fu==fv) continue; fa[fu]=fv;
int a=E[i].fr,b=E[i].to,c=E[i].cst;
T[++cnt]=(Edg){a,b,c,head[a]}; head[a]=cnt;
T[++cnt]=(Edg){b,a,c,head[b]}; head[b]=cnt;
if(cnt==((n-1)<<1)) break;
}
F(i,1,n) if(!vis[i]) dfs(i,0);//可能有多棵树
for(register int j=1;(1<<j)<=n;j++)
F(i,1,n) if(f[i][j-1]) f[i][j]=f[f[i][j-1]][j-1];
q=read();
while(q--) {
int x=read(),y=read();
if(getf(x)!=getf(y)) puts("-1");
else printf("%d\n",lca(x,y));
}
return 0;
}
标签:else stream namespace 关系 a* pre iostream swap dig
原文地址:https://www.cnblogs.com/Menteur-Hxy/p/9502496.html