被虐了 自己好弱
1001
就是个比较裸的矩阵快速幂了
#include<cstdio>
#include<cstdlib>
#include<algorithm>
using namespace std;
typedef long long ll;
inline char nc()
{
static char buf[100000],*p1=buf,*p2=buf;
if (p1==p2) { p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; }
return *p1++;
}
inline void read(ll &x)
{
char c=nc(),b=1;
for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;
}
ll x,m,P,c;
struct matrix{
ll a[2][2];
matrix(ll x1=0,ll x2=0,ll x3=0,ll x4=0){
a[0][0]=x1; a[0][1]=x2; a[1][0]=x3; a[1][1]=x4;
}
ll *operator [](int x){
return a[x];
}
friend matrix operator *(matrix A,matrix B){
matrix ret;
for (ll i=0;i<2;i++)
for (ll j=0;j<2;j++)
for (ll k=0;k<2;k++)
(ret[i][j]+=A[i][k]*B[k][j])%=P;
return ret;
}
};
inline matrix Pow(matrix a,ll b){
matrix ret(1,0,0,1);
for (;b;b>>=1,a=a*a)
if (b&1)
ret=ret*a;
return ret;
}
int main()
{
ll Q,ans;
freopen("t.in","r",stdin);
freopen("t.out","w",stdout);
read(Q);
for (int i=1;i<=Q;i++)
{
read(x); read(m); read(P); read(c);
matrix tmp(10,x,0,1);
tmp=Pow(tmp,m-1);
ans=(tmp[0][0]*x+tmp[0][1])%P;
c=c%P;
printf("Case #%d:\n",i);
ans==c?printf("Yes\n"):printf("No\n");
}
return 0;
}
1002
一个n*n*2^n的DP
赛时竟然没打 哭晕
1003
SB dfs序+线段树
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#define V G[p].v
#define cl(x) memset(x,0,sizeof(x))
#pragma comment(linker, "/STACK:102400000000,102400000000")
using namespace std;
typedef long long ll;
inline char nc()
{
static char buf[100000],*p1=buf,*p2=buf;
if (p1==p2) { p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; }
return *p1++;
}
inline void read(int &x)
{
char c=nc(),b=1;
for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;
}
const int N=100005;
struct SEGTREE{
int M,TH;
ll T[N*4],H[N*4];
inline void Build (int n)
{
for (M=1,TH=0;M<n+2;M<<=1,TH++);
memset(T,-0x3f,sizeof(T)); cl(H);
for (int i=1;i<=n;i++) T[M+i]=0;
for (int i=M-1;i;i--) T[i]=max(T[i<<1],T[i<<1|1]);
}
inline void Pushdown(int rt){
int p;
for (int i=TH;i;i--)
if (H[p=rt>>i]){
T[p<<1]+=H[p]; T[p<<1|1]+=H[p];
H[p<<1]+=H[p]; H[p<<1|1]+=H[p];
H[p]=0;
}
}
inline void Add(int s,int t,ll c){
for (Pushdown(s+=M-1),Pushdown(t+=M+1);s^t^1;){
if (~s&1) T[s^1]+=c,H[s^1]+=c;
if ( t&1) T[t^1]+=c,H[t^1]+=c;
s>>=1; T[s]=max(T[s<<1],T[s<<1|1]);
t>>=1; T[t]=max(T[t<<1],T[t<<1|1]);
}
while (s>>=1)
T[s]=max(T[s<<1],T[s<<1|1]);
}
inline ll Query(int s,int t){
ll ret=-1LL<<60;
for (Pushdown(s+=M-1),Pushdown(t+=M+1);s^t^1;s>>=1,t>>=1){
if (~s&1) ret=max(ret,T[s^1]);
if ( t&1) ret=max(ret,T[t^1]);
}
return ret;
}
}Seg;
struct edge{
int u,v,next;
};
edge G[N<<1];
int head[N],inum;
inline void add(int u,int v,int p){
G[p].u=u; G[p].v=v; G[p].next=head[u]; head[u]=p;
}
int size[N],last[N];
int clk,tid[N];
void dfs(int u,int fa){
tid[u]=++clk; size[u]=1;
for (int p=head[u];p;p=G[p].next)
if (V!=fa)
dfs(V,u),size[u]+=size[V];
last[u]=tid[u]+size[u]-1;
}
int n,m; ll ans;
int val[N];
struct data{
int p,u,fa;
}Stk[N];
int top;
inline void idfs(int u,int fa){
top++; Stk[top].u=u; Stk[top].fa=fa; Stk[top].p=head[u];
tid[u]=++clk; size[u]=1;
while (top)
{
if (Stk[top].p==0)
{
size[Stk[top-1].u]+=size[Stk[top].u];
last[Stk[top].u]=tid[Stk[top].u]+size[Stk[top].u]-1;
top--;
}
else
{
fa=Stk[top].u; u=G[Stk[top].p].v; Stk[top].p=G[Stk[top].p].next;
if (u!=Stk[top].fa)
{
++top; Stk[top].u=u; Stk[top].fa=fa; Stk[top].p=head[u];
tid[u]=++clk; size[u]=1;
}
}
}
}
int main()
{
int iu,iv,x,y,Q,order;
freopen("t.in","r",stdin);
freopen("t.out","w",stdout);
read(Q);
for (int t=1;t<=Q;t++)
{
printf("Case #%d:\n",t);
read(n); read(m);
for (int i=1;i<n;i++)
read(iu),read(iv),iu++,iv++,add(iu,iv,++inum),add(iv,iu,++inum);
clk=0;
idfs(1,0);
Seg.Build(n);
for (int i=1;i<=n;i++)
read(val[i]),Seg.Add(tid[i],last[i],val[i]);
for (int i=1;i<=m;i++)
{
read(order);
if (order==0){
read(x); x++; read(y);
Seg.Add(tid[x],last[x],-val[x]);
val[x]=y;
Seg.Add(tid[x],last[x],val[x]);
}else{
read(x); x++;
ans=Seg.Query(tid[x],last[x]);
printf("%I64d\n",ans);
}
}
inum=0; cl(G); cl(head); cl(val);
}
return 0;
}
1004
不会做 Orz
1005
赛时没调出来 调到结束后一小时 智商突破下限
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
using namespace std;
typedef long long ll;
inline char nc()
{
static char buf[100000],*p1=buf,*p2=buf;
if (p1==p2) { p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; }
return *p1++;
}
inline void read(ll &x)
{
char c=nc(),b=1;
for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;
}
ll Q;
ll L,R;
struct abcd{
ll B,D;
abcd(ll B=0,ll D=0):B(B),D(D) { }
abcd reverse() { return abcd(D,B); }
friend abcd operator + (abcd A,abcd B) { return abcd(A.B+B.B,A.D+B.D); }
}cnt[1000];
ll len[1000];
int tot;
inline void Pre()
{
ll Len=1; cnt[1].B=1; len[1]=1;
for (int i=2;i;i++)
{
Len=Len<<1|1;
len[i]=Len;
cnt[i].B=cnt[i-1].B+cnt[i-1].D+1;
cnt[i].D=cnt[i-1].D+cnt[i-1].B;
if (Len>(ll)(1e18)+1) { tot=i; break; }
}
}
inline ll rev(ll n,ll a){
return n-a+1;
}
int maxd;
abcd Calc(int idx,ll l,ll r,int f,int d=0){
maxd=max(maxd,d);
if (l==1 && r==len[idx]){
abcd ret=cnt[idx];
if (f) ret=ret.reverse();
return ret;
}
if (l>r) return abcd(0,0);
abcd ret;
if (r<=len[idx-1])
ret=Calc(idx-1,l,r,f,d+1);
else if (l>len[idx-1]+1)
{
l-=len[idx-1]+1; r-=len[idx-1]+1;
ret=Calc(idx-1,rev(len[idx-1],r),rev(len[idx-1],l),f^1,d+1);
}
else
{
ret=ret+Calc(idx-1,l,len[idx-1],f,d+1);
if (f) ret=ret+abcd(0,1); else ret=ret+abcd(1,0);
r-=len[idx-1]+1;
ret=ret+Calc(idx-1,rev(len[idx-1],r),rev(len[idx-1],1),f^1,d+1);
}
return ret;
}
inline ll Sum(ll n){
abcd ret=Calc(tot,1,n,0);
return ret.B;
}
int main()
{
ll Q,L,R;
freopen("t.in","r",stdin);
freopen("t.out","w",stdout);
read(Q); Pre();
while (Q--)
{
read(L); read(R);
// printf("%lld %lld\n",Sum(R),Sum(L-1));
printf("%lld\n",Sum(R)-Sum(L-1));
}
return 0;
}
1006
堆+拓扑序 诶
#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
#include<queue>
#define V G[p].v
#define cl(x) memset(x,0,sizeof(x))
using namespace std;
typedef long long ll;
inline char nc()
{
static char buf[100000],*p1=buf,*p2=buf;
if (p1==p2) { p2=(p1=buf)+fread(buf,1,100000,stdin); if (p1==p2) return EOF; }
return *p1++;
}
inline void read(int &x)
{
char c=nc(),b=1;
for (;!(c>='0' && c<='9');c=nc()) if (c=='-') b=-1;
for (x=0;c>='0' && c<='9';x=x*10+c-'0',c=nc()); x*=b;
}
const int N=100005;
struct edge{
int u,v;
int next;
};
edge G[N<<1];
int head[N],inum;
inline void add(int u,int v,int p){
G[p].u=u; G[p].v=v; G[p].next=head[u]; head[u]=p;
}
int n,m; ll ans;
int deg[N<<1];
priority_queue<int> Que;
int main()
{
int Q,x,y;
freopen("t.in","r",stdin);
freopen("t.out","w",stdout);
read(Q);
while (Q--)
{
read(n); read(m);
for (int i=1;i<=m;i++) read(x),read(y),add(x,y,++inum),deg[y]++;
for (int i=1;i<=n;i++) if (!deg[i]) Que.push(i);
ll tmp,minimum=Que.top(); ans=0;
for (int i=1;i<=n;i++)
{
ans+=(minimum=min(minimum,tmp=Que.top())); Que.pop();
for (int p=head[tmp];p;p=G[p].next) if (!(--deg[V])) Que.push(V);
}
printf("%I64d\n",ans);
cl(head); cl(G); inum=0;
}
return 0;
}