# 717 Bubble Cup 9 - Finals [Online Mirror]

### 签到题：

##### C. Potions Homework

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;
const int _=1e2;
const int maxn=1e5+_;

LL a[maxn];
int main()
{
int n;LL ans=0;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]);
sort(a+1,a+n+1);
for(int i=1;i<=n;i++)
ans+=a[i]*a[n-i+1]%10007,ans%=10007;
printf("%d\n",ans);

return 0;
}

##### D. Dexterina’s Lab

f [ i ] [ j ] f[i][j] 表示枚举到第i堆，sg值为j的概率，矩乘优化即可。

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;
const int _=1e2;
const int maxn=1e5+_;
int n,m;

struct Matrix
{
double mp[210][210];
Matrix(){memset(mp,0,sizeof(mp));}
friend Matrix operator *(Matrix a,Matrix b)
{
Matrix c;
memset(c.mp,0,sizeof(c.mp));
for(int i=0;i<=127;i++)
for(int j=0;j<=127;j++)
for(int k=0;k<=127;k++)
c.mp[i][j]+=a.mp[i][k]*b.mp[k][j];
return c;
}
}ans,A;

double p[210];
int main()
{
scanf("%d%d",&n,&m);
for(int i=0;i<=m;i++)scanf("%lf",&p[i]);

for(int i=0;i<=127;i++)
{
for(int j=0;j<=127;j++)//由i转移到j
A.mp[i][j]+=p[i^j];
}
ans.mp[0][0]=1;
while(n!=0)
{
if(n%2==1)ans=ans*A;
A=A*A;n/=2;
}
printf("%.8lf\n",1.0-ans.mp[0][0]);

return 0;
}

##### E. Paint it really, really dark gray

1开始搜一次，经过就变色，每个子树保证它全黑了（除了根）再回溯。假如子树的根是白的就再去一次，如果最后1是白的就和随便一个孩子来回一下

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
const int _=1e2;
const int maxn=1e5+_;

struct node
{
int x,y,next;
}a[2*maxn];int len,last[maxn];
void ins(int x,int y)
{
len++;
a[len].x=x;a[len].y=y;
a[len].next=last[x];last[x]=len;
}

int c[maxn];
void dfs(int x,int fa)
{
c[x]^=1,printf("%d ",x);
for(int k=last[x];k;k=a[k].next)
{
int y=a[k].y;
if(y!=fa)
{
dfs(y,x);
c[x]^=1,printf("%d ",x);
if(c[y])
{
c[y]^=1,printf("%d ",y);
c[x]^=1,printf("%d ",x);
}
}
}
if(x==1&&!c[x])
{
int k=last[x];
int y=a[k].y;
c[y]^=1,printf("%d ",y);
c[x]^=1,printf("%d ",x);
c[y]^=1,printf("%d ",y);
}
}

int main()
{
int n,x,y;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&x);
if(x==-1)c[i]=1;
else c[i]=0;
}
for(int i=1;i<n;i++)
{
scanf("%d%d",&x,&y);
ins(x,y),ins(y,x);
}
dfs(1,0);

return 0;
}

##### G. Underfail

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
const int _=1e2;
const int maxn=1e5+_;

struct node
{
int x,y,c,d,next;
}a[20*maxn];int len,last[maxn];
void ins(int x,int y,int c,int d)
{
//	printf("%d %d %d %d\n",x,y,c,d);
len++;
a[len].x=x;a[len].y=y;a[len].c=c;a[len].d=d;
a[len].next=last[x];last[x]=len;

len++;
a[len].x=y;a[len].y=x;a[len].c=0;a[len].d=-d;
a[len].next=last[y];last[y]=len;
}

int st,ed;
int pre[maxn],c[maxn],ans,d[maxn];
int list[maxn];bool v[maxn];
bool spfa()
{
memset(d,-63,sizeof(d));d[st]=0;c[st]=(1<<30);
memset(v,false,sizeof(v));v[st]=true;
{
for(int k=last[x];k;k=a[k].next)
{
int y=a[k].y;
if(a[k].c>0&&d[y]<d[x]+a[k].d)
{
d[y]=d[x]+a[k].d;
c[y]=min(a[k].c,c[x]);
pre[y]=k;
if(v[y]==false)
{
v[y]=true;
list[tail]=y;
tail++;if(tail==maxn)tail=1;
}
}
}
v[x]=false;
}
if(d[ed]==d[ed+1])return false;
else
{
int y=ed;ans+=c[ed]*d[ed];
while(y!=st)
{
int k=pre[y];
a[k].c-=c[ed];
a[k^1].c+=c[ed];
y=a[k].x;
}
return true;
}
}

char ss[maxn],sa[maxn];
bool check(int len,int j)
{
for(int i=1;i<=len;i++,j++)
if(ss[j]!=sa[i])return false;
return true;
}
int main()
{
int n,m,x;
scanf("%d%s%d",&n,ss+1,&m); len=1;
st=0,ed=n+1;
for(int i=1;i<=m;i++)
{
scanf("%s%d",sa+1,&x);int len=strlen(sa+1);
for(int j=1;j<=n-len+1;j++)
if(check(len,j))ins(j,j+len,1,x);
}
scanf("%d",&x);
for(int i=1;i<=n+1;i++)ins(i-1,i,x,0);

ans=0;
while(spfa());
printf("%d\n",ans);
return 0;
}


### 中档题

##### H. Pokermon League challenge

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
const int _=1e2;
const int maxn=5e4+_;
const int maxm=1e5+_;

int as[maxn],ans[maxn],c[maxm],g[maxn][22];
vector<int>h[maxn];
int main()
{
int n,m,x,y,L=0;
scanf("%d%d",&n,&m);
for(int i=1;i<=m;i++)
{
scanf("%d%d",&x,&y);
if(x<y)swap(x,y);
h[x].push_back(y);
}
for(int i=1;i<=n;i++)
{
scanf("%d",&g[i][0]);
for(int j=1;j<=g[i][0];j++)
scanf("%d",&g[i][j]),L=max(L,g[i][j]);
}

for(int i=1;i<=n;i++)
{
int aa=0,bb=0;
for(int j=0;j<h[i].size();j++)
if(as[h[i][j]]==0)aa++;
else bb++;
as[i]=aa>=bb;
}
srand(252628);
bool bk=false;
while(bk==false)
{
for(int i=1;i<=L;i++)c[i]=rand()%2;
bk=true;
for(int i=1;i<=n;i++)
{
bool flag=false;
for(int j=1;j<=g[i][0];j++)
if(c[g[i][j]]==as[i]){ans[i]=g[i][j];flag=true;break;}
if(!flag)
{bk=false;break;}
}
}
for(int i=1;i<n;i++)printf("%d ",ans[i]); printf("%d\n",ans[n]);
for(int i=1;i<L;i++)printf("%d ",c[i]+1); printf("%d\n",c[L]+1);

return 0;
}


#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<map>
using namespace std;
typedef long long LL;

map<LL,int>mp; map<LL,int>::iterator it;
int main()
{
int n,c0,c1;
scanf("%d%d%d",&n,&c0,&c1);
if(c0==0||c1==0){printf("%lld\n",(LL)(n-1)*(c0+c1));return 0;}

mp[0]=1;int u=n-1;
it=mp.begin();
while(1)
{
if(u>it->second)
mp[it->first+c0]+=it->second,mp[it->first+c1]+=it->second;
else
{
mp[it->first+c0]+=u,mp[it->first+c1]+=u;
it->second-=u;
break;
}
u-=it->second;
it++;
}
LL ans=0;
for(;it!=mp.end();it++)
ans+=(it->first)*(it->second);
printf("%lld\n",ans);

return 0;
}


### 较难题：

##### F. Heroes of Making Magic III

a 1 − 1 ≥ 0 a_1-1\geq 0 （第一步） a 2 − a 1 ≥ 0 a_2-a1\geq 0 （能够在1、2之间来回走并到达2位置）

（这两个比较特殊）

a 3 − 1 − ( a 2 − a 1 ) ≥ 0 a_3-1-(a_2-a1)\geq 0 （先从2走到3，然后能够消去2剩下的）

a 4 − 1 − [ a 3 − 1 − ( a 2 − a 1 ) ] ≥ 0 a_4-1-[a_3-1-(a_2-a1)]\geq 0 （先从3走到4，然后能够消去3剩下的）

… … ……

∑ j = 1 i ( − 1 ) i − j a j ≥ i % 2 \sum^i_{j=1}(-1)^{i-j}a_j\geq i\%2 ( 0 ≤ i &lt; n ) (0\leq i&lt; n) ∑ j = 1 i ( − 1 ) i − j a j = i % 2 \sum^i_{j=1}(-1)^{i-j}a_j=i\%2 ( i = n ) (i=n) (最后要消完)

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#define lc (now<<1)
#define rc (now<<1)|1
#define mid (ql+qr)/2
using namespace std;
const int _=1e2;
const int maxn=2e5+_;
const int fbin=(1<<18);

struct TR
{
int n;
struct trnode
{
int c,la;
}tr[2*fbin];int num[maxn];
void update(int now){tr[now].c=min(tr[lc].c,tr[rc].c);}
void pushdown(int now)
{
tr[lc].c+=tr[now].la,tr[lc].la+=tr[now].la;
tr[rc].c+=tr[now].la,tr[rc].la+=tr[now].la;
tr[now].la=0;
}
void bt(int now,int ql,int qr)
{
if(ql==qr)tr[now].c=num[ql];
else bt(lc,ql,mid),bt(rc,mid+1,qr),update(now);
}
void change(int now,int ql,int qr,int l,int r,int k)
{
if(l>r||r>qr)return ;
if(ql==l&&qr==r)
{
tr[now].c+=k;tr[now].la+=k;
return ;
}
pushdown(now);
if(r<=mid)  change(lc,ql,mid,l,r,k);
else if(mid+1<=l)change(rc,mid+1,qr,l,r,k);
else change(lc,ql,mid,l,mid,k),change(rc,mid+1,qr,mid+1,r,k);
update(now);
}
int getmin(int now,int ql,int qr,int l,int r)
{
if(l<ql)return 0;
if(l>r||r>qr)return (1<<30);
if(ql==l&&qr==r)return tr[now].c;
pushdown(now);
if(r<=mid)  return getmin(lc,ql,mid,l,r);
else if(mid+1<=l)return getmin(rc,mid+1,qr,l,r);
else return min(getmin(lc,ql,mid,l,mid),getmin(rc,mid+1,qr,mid+1,r));
update(now);
}
}T[2];

inline int id(int x){return (x+1)/2;}
int main()
{
int n,x,la=0;
scanf("%d",&n);
for(int i=1;i<=n;i++)
{
scanf("%d",&x),x-=la,la=x;
T[i%2].num[id(i)]=x;
}
T[0].n=n/2,T[0].bt(1,1,T[0].n);
T[1].n=(n+1)/2,T[1].bt(1,1,T[1].n);
int Q,op,l,r,w,k;
scanf("%d",&Q);
while(Q--)
{
scanf("%d",&op);
if(op==1)
{
scanf("%d%d%d",&l,&r,&k); l++,r++;
w=l%2;
if((r-l+1)%2==1)
{
T[w].change(1,1,T[w].n,id(l),T[w].n,k);
T[w^1].change(1,1,T[w^1].n,id(l+1),T[w^1].n,-k);
l++;
}
w=l%2;
if(l<r)
T[w].change(1,1,T[w].n,id(l),id(r-1),k);
}
else
{
bool bk=true;
scanf("%d%d",&l,&r); l++,r++;
w=l%2;
if(l==r)bk=(T[w].getmin(1,1,T[w].n,id(l),id(l))==1);
else
{
if((r-l+1)%2==1)
{
int B=T[w^1].getmin(1,1,T[w^1].n,id(l-1),id(l-1));
bk&=(T[w].getmin(1,1,T[w].n,id(l),id(r))+B>=1);
bk&=(T[w^1].getmin(1,1,T[w^1].n,id(l+1),id(r-1))-B>=0);
bk&=(T[w].getmin(1,1,T[w].n,id(r),id(r))+B==1);
}
else
{
int B=T[w^1].getmin(1,1,T[w^1].n,id(l-1),id(l-1));
bk&=(T[w].getmin(1,1,T[w].n,id(l),id(r-1))+B>=1);
bk&=(T[w^1].getmin(1,1,T[w^1].n,id(l+1),id(r))-B>=0);
bk&=(T[w^1].getmin(1,1,T[w^1].n,id(r),id(r))-B==0);
}
}
puts(bk?"1":"0");
}
}

return 0;
}

##### A. Festival Organization

KaTeX parse error: Got function '\underline' with no arguments as superscript at position 27: …\sum^n_{i=1}fib^̲\underline k_i

KaTeX parse error: Got function '\underline' with no arguments as superscript at position 27: …\sum^n_{i=1}fib^̲\underline k_i

= 1 k ∑ i = 1 n ∑ j = 0 k [ k j ] ( − 1 ) k − j f i b i j ={1\over k}\sum^n_{i=1}\sum^k_{j=0}{k\brack j}(-1)^{k-j}fib_i^j （换入第一类斯特林数）

= 1 k ∑ j = 0 k [ k j ] ( − 1 ) k − j ∑ i = 1 n f i b i j ={1\over k}\sum^k_{j=0}{k\brack j}(-1)^{k-j}\sum^n_{i=1}fib_i^j

G ( n , j ) = ∑ i = 1 n f i b i j G(n,j)=\sum^n_{i=1}fib_i^j 只要快速算出这个东西就行了，前面只有 O ( k ) O(k) 的花费

（下面用 G ( n , k ) G(n,k) 推）

G ( n , k ) = ∑ i = 1 n f i b i k G(n,k)=\sum^n_{i=1}fib_i^k

= ∑ i = 1 n [ ( 1 + 5 2 ) i − ( 1 − 5 2 ) i 5 ] k =\sum^n_{i=1}[{({1+\sqrt 5\over2})^i-({1-\sqrt 5\over2})^i\over \sqrt 5}]^k

= ( 1 5 ) k ∑ i = 1 n [ ( 1 + 5 2 ) i − ( 1 − 5 2 ) i ] k =({1\over \sqrt 5})^k\sum^n_{i=1}[{({1+\sqrt 5\over2})^i-({1-\sqrt 5\over2})^i}]^k

= ( 1 5 ) k ∑ i = 1 n ∑ j = 0 k ( k j ) ∗ ( 1 + 5 2 ) i ( k − j ) ∗ [ − ( 1 − 5 2 ) i ] j =({1\over \sqrt 5})^k\sum^n_{i=1}\sum^k_{j=0}{k\choose j}*{({1+\sqrt 5\over2})^{i(k-j)}*[-({1-\sqrt 5\over2})^i}]^j （二项式展开）

= ( 1 5 ) k ∑ i = 1 n ∑ j = 0 k ( k j ) ∗ ( − 1 ) j ∗ ( 1 + 5 2 ) i ( k − j ) ∗ ( 1 − 5 2 ) i j =({1\over \sqrt 5})^k\sum^n_{i=1}\sum^k_{j=0}{k\choose j}*(-1)^j*{({1+\sqrt 5\over2})^{i(k-j)}*({1-\sqrt 5\over2})^{ij}}

= ( 1 5 ) k ∑ j = 0 k ( k j ) ∗ ( − 1 ) j ∗ ∑ i = 1 n ( 1 + 5 2 ) i ( k − j ) ∗ ( 1 − 5 2 ) i j =({1\over \sqrt 5})^k\sum^k_{j=0}{k\choose j}*(-1)^j*\sum^n_{i=1}{({1+\sqrt 5\over2})^{i(k-j)}*({1-\sqrt 5\over2})^{ij}}

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
using namespace std;
typedef long long LL;
const int _=1e2;
const int maxK=210;
const int mod=1e9+7;
inline int ad(int x,int y){return x>=mod-y?x-mod+y:x+y;}
inline int re(int x,int y){return x<y?x-y+mod:x-y;}
inline int mu(int x,int y){return (LL)x*y%mod;}
inline int qp(int x,int y){int r=1;while(y){if(y&1)r=mu(r,x);x=mu(x,x),y>>=1;}return r;}
inline int iv(int x){return qp(x,mod-2);}
inline int dv(int x,int y){return mu(x,iv(y));}
struct complex
{
int a,b;
complex(){};complex(int A,int B){a=A,b=B;}
friend complex operator -(complex x,complex y){return complex(re(x.a,y.a),re(x.b,y.b));}
friend complex operator ^(complex x,LL p){complex r=complex(1,0);while(p){if(p&1)r=r*x;x=x*x;p>>=1;}return r;}
friend bool operator ==(complex x,complex y){return x.a==y.a&&x.b==y.b;}
};

int S1[maxK][maxK],fac[maxK],fac_inv[maxK];
int C(int n,int m){return mu(fac[n],mu(fac_inv[m],fac_inv[n-m]));}
void yu()
{
fac[0]=1;for(int i=1;i<maxK;i++)fac[i]=mu(fac[i-1],i);
fac_inv[maxK-1]=iv(fac[maxK-1]);
for(int i=maxK-2;i>=0;i--)fac_inv[i]=mu(fac_inv[i+1],i+1);
S1[0][0]=1;
for(int i=1;i<maxK;i++)
for(int j=1;j<=i;j++)
}
int fib[maxK];
int getsum(LL n,int k)
{
/*	int ret=1;fib[1]=1;
printf("%d\n",ret);
return ret;*/

complex ret=complex(0,0),q,u,d,t;
for(int j=0;j<=k;j++)
{
q=(complex(fac_inv[2],fac_inv[2])^(k-j))*(complex(fac_inv[2],re(0,fac_inv[2]))^j);

if(q==complex(1,0))q=complex(n%mod,0);
else
{
u=(q^n)-complex(1,0),d=q-complex(1,0);

t=d,t.b=re(0,t.b);
u=u*t,d=d*t;

q=q*u*complex(iv(d.a),0);
}

if(j&1)ret=ret-q*complex(C(k,j),0);
else ret=ret+q*complex(C(k,j),0);
}
ret=ret*(complex(0,dv(1,5))^k);
if(ret.b)puts("error");
return ret.a;
}
int calc(LL n,int k)
{
int ret=0;
for(int j=1;j<=k;j++)
{
if((k-j)&1)ret=re(ret,mu(S1[k][j],getsum(n,j)));
}
return mu(ret,fac_inv[k]);
}
int main()
{
int K;LL l,r;
scanf("%d%lld%lld",&K,&l,&r);yu();
printf("%d\n",re(calc(r+2,K),calc(l+1,K)));

return 0;
}

##### I. Cowboy Beblop at his computer 几何题待填
• 点赞
• 评论
• 分享
x

海报分享

扫一扫，分享海报

• 收藏
• 手机看

分享到微信朋友圈

x

扫一扫，手机阅读

• 打赏

打赏

mf_chris

你的鼓励将是我创作的最大动力

C币 余额
2C币 4C币 6C币 10C币 20C币 50C币
• 一键三连

点赞Mark关注该博主, 随时了解TA的最新博文
09-26 1379

09-26 102