近期的做题总结
noi2014
luoguP2114 [NOI2014]起床困难综合症
luoguP2387 [NOI2014]魔法森林
luoguP2375 [NOI2014]动物园
luoguP2354 [NOI2014]随机数生成器
luoguP2305 [NOI2014]购票
noi2015
luoguP1955 [NOI2015]程序自动分析
luoguP2146 [NOI2015]软件包管理器
luoguP2150 [NOI2015]寿司晚宴
luoguP2168 [NOI2015]荷马史诗
luoguP2178 [NOI2015]品酒大会
luoguP2304 [NOI2015]小园丁与老司机
noi2016
luoguP1117 [NOI2016]优秀的拆分
luoguP1173 [NOI2016]网格
luoguP1587 [NOI2016]循环之美
luoguP1712 [NOI2016]区间
luoguP1721 [NOI2016]国王饮水记
luoguP1737 [NOI2016]旷野大计算
noi2017
luoguP3822 [NOI2017]整数
luoguP3823 [NOI2017]蚯蚓排队
luoguP3824 [NOI2017]泳池
luoguP3825 [NOI2017]游戏
luoguP3826 [NOI2017]蔬菜
luoguP3827 [NOI2017]分身术
20分
咕咕咕
noi2018
luoguP4768 [NOI2018]归程
luoguP4769 [NOI2018]冒泡排序
luoguP4770 [NOI2018]你的名字
luoguP4774 [NOI2018]屠龙勇士
luoguP4775 [NOI2018]情报中心
luoguP4776 [NOI2018]多边形
50分
咕咕咕
gym102028AXu Xiake in Henan Province
1 #include<bits/stdc++.h>
2 using namespace std;
3 int main()
4 {
5 int T;scanf("%d",&T);
6 while(T--)
7 {
8 int a,b,c,d;scanf("%d%d%d%d",&a,&b,&c,&d);
9 int t=(a==0)+(b==0)+(c==0)+(d==0);
10 if(t==4)puts("Typically Otaku");
11 if(t==3)puts("Eye-opener");
12 if(t==2)puts("Young Traveller");
13 if(t==1)puts("Excellent Traveller");
14 if(t==0)puts("Contemporary Xu Xiake");
15 }
16 return 0;
17 }
gym102028BUltraman vs. Aodzilla and Bodzilla
1 #include<bits/stdc++.h>
2 using namespace std;
3 #define ll long long
4 const int N=1e5+5;
5 int ha,hb,ta,tb;ll ans[2];char s[2][N];
6 int solab(ll &ans,char s[])
7 {
8 int i=0;ll sa=0;
9 while(sa<ha){s[++i]='A';sa+=i;}
10 ans=(ll)i*ta;ll sb=sa-ha;
11 while(sb<hb){s[++i]='B';sb+=i;}
12 ans+=(ll)i*tb;ll x=(sa-ha)-(sb-hb);
13 if(x>0)s[sa-ha]='B';return i;
14 }
15 int solba(ll &ans,char s[])
16 {
17 int i=0;ll sb=0;
18 while(sb<hb){s[++i]='B';sb+=i;}
19 ans=(ll)i*tb;ll sa=sb-hb;
20 while(sa<ha){s[++i]='A';sa+=i;}
21 ans+=(ll)i*ta;ll x=(sb-hb)-(sa-ha);
22 int j=1;ll t=sb-hb;
23 while(t>=j){s[j]='A';x-=j;t-=j;j++;}
24 while(x>0){s[j-1]='B';s[j]='A';x--;j++;}
25 return i;
26 }
27 int main()
28 {
29 int T;scanf("%d",&T);
30 while(T--)
31 {
32 scanf("%d%d%d%d",&ha,&hb,&ta,&tb);
33 int p,n=solab(ans[0],s[0]);solba(ans[1],s[1]);
34 if(ans[0]!=ans[1])p=ans[0]<ans[1];
35 else{int i=1;while(i<n&&s[0][i]==s[1][i])++i;p=s[0][i]<s[1][i];}
36 p^=1;s[p][n+1]=0;printf("%lld %s\n",ans[p],s[p]+1);
37 }
38 return 0;
39 }
gym102028CSupreme Command
1 #include<bits/stdc++.h>
2 using namespace std;
3 #define ll long long
4 const int N=300005;
5 int T,n,m,vis[N];ll LL,LR,RL,RR,C[N];
6 inline void cal(int i);
7 struct D
8 {
9 int p[N],id[N];ll l,r,d;
10 void init(){l=1,r=n,d=0;}
11 void pre(){cal(id[1]);cal(id[n]);}
12 void stp(int x,int i){p[i]=x;id[x]=i;}
13 void left(int k){while(l<r&&l+d-k<1)cal(id[++l]);if(l+d-k>=1)d-=k;else d=1-l;}
14 void right(int k){while(r>l&&r+d+k>n)cal(id[--r]);if(r+d+k<=n)d+=k;else d=n-r;}
15 int qry(int x){return p[x]<=l?l+d:(p[x]>=r?r+d:p[x]+d);}
16 }A, B;
17 inline void cal(int i)
18 {
19 if(vis[i])return;
20 else if(A.p[i]<=A.l&&B.p[i]<=B.l)vis[i]=1,LL++;
21 else if(A.p[i]<=A.l&&B.p[i]>=B.r)vis[i]=1,LR++;
22 else if(A.p[i]>=A.r&&B.p[i]<=B.l)vis[i]=1,RL++;
23 else if(A.p[i]>=A.r&&B.p[i]>=B.r)vis[i]=1,RR++;
24 }
25 ll qry()
26 {
27 if(A.r-A.l>0&&B.r-B.l>0)return C[LL]+C[RR]+C[LR]+C[RL];
28 if(A.r-A.l==0&&B.r-B.l>0)return C[LL+RL]+C[LR+RR];
29 if(B.r-B.l==0&&A.r-A.l>0)return C[LL+LR]+C[RR+RL];
30 return C[LL+LR+RR+RL];
31 }
32 int main()
33 {
34 scanf("%d",&T);
35 for(int i=2;i<N;i++)C[i]=1ll*i*(i-1)/2;
36 while(T--)
37 {
38 scanf("%d%d",&n,&m);LL=LR=RR=RL=0;
39 for(int i=1,x,y;i<=n;i++)scanf("%d%d",&x,&y),B.stp(x,i),A.stp(y,i);
40 A.init();B.init();A.pre();B.pre();int k;char s[3];
41 for(int i=1;i<=m;i++)
42 {
43 scanf("%s",s);
44 if(s[0]=='!')printf("%lld\n",qry());
45 else
46 {
47 scanf("%d",&k);
48 if(s[0]=='L') A.left(k);
49 else if(s[0]=='R') A.right(k);
50 else if(s[0]=='U') B.left(k);
51 else if(s[0]=='D') B.right(k);
52 else printf("%d %d\n",B.qry(k),A.qry(k));
53 }
54 }
55 for(int i=1;i<=n;i++)vis[i]=0;
56 }
57 return 0;
58 }
gym102028DKeiichi Tsuchiya the Drift King
1 #include<bits/stdc++.h>
2 using namespace std;
3 int main()
4 {
5 int T;scanf("%d",&T);
6 while(T--)
7 {
8 double a,b,r,d;scanf("%lf%lf%lf%lf",&a,&b,&r,&d);
9 double l=sqrt(b*b+(a+r)*(a+r)),ang=acos((a+r)/l);d=d/180*acos(-1.0);
10 if(d>ang||fabs(d-ang)<1e-6)printf("%.9lf\n",l-r);
11 else{double l2=(a+r)/cos(d),x=(b-l2*sin(d))*sin(d);printf("%.9lf\n",l2+x-r);}
12 }
13 return 0;
14 }
gym102028EResistors in Parallel
1 #include<bits/stdc++.h>
2 using namespace std;
3 string S[205],s1[205],s2[205];
4 int d[205],f[205][505];
5 inline void prt(string s)
6 {
7 int t=0;while(s[t]=='0')t++;
8 for(int i=t;i<=200;i++)printf("%c",s[i]);
9 }
10 inline string mul(string a,int b){for(int j=200,c=0;j>=0;j--){int t=(a[j]-'0')*b+c;a[j]='0'+t%10;c=t/10;}return a;}
11 int main()
12 {
13 string s="";for(int i=1;i<=200;i++)s+="0";s+="1";
14 int cc;S[cc=1]=s1[1]=s2[1]=s;
15 for(int i=2;i<=500;i++)
16 {
17 int ff=1;for(int j=2;j<i;j++)if(i%j==0){ff=0;break;}
18 if(!ff)continue;
19 int t1=i,t2=i+1,c=0;
20 for(int j=200;j>=0;j--)
21 {
22 int t=(S[cc][j]-'0')*t1+c;
23 d[j]=t%10;c=t/10;
24 }
25 if(c)break;
26 S[cc+1]="";
27 for(int j=0;j<=200;j++)S[cc+1]+=('0'+d[j]);
28 cc++;
29 for(int j=2;j<500;j++)f[cc][j]=f[cc-1][j];f[cc][i]++;
30 for(int j=2;j<500;j++)if(t2%j==0){while(t2%j==0)f[cc][j]--,t2/=j;}
31 s1[cc]=s2[cc]=s;
32 for(int j=2;j<500;j++)if(f[cc][j]>0)for(int k=1;k<=f[cc][j];k++)s1[cc]=mul(s1[cc],j);
33 for(int j=2;j<500;j++)if(f[cc][j]<0)for(int k=1;k<=-f[cc][j];k++)s2[cc]=mul(s2[cc],j);
34 }
35 int C;cin>>C;
36 while(C--)
37 {
38 cin>>s;int n=s.size();while(n<201)s="0"+s,n++;
39 int t=upper_bound(S+1,S+cc+1,s)-S-1;
40 prt(s1[t]);putchar('/');prt(s2[t]);putchar('\n');
41 }
42 return 0;
43 }
gym102028FHoneycomb
1 #include<bits/stdc++.h>
2 using namespace std;
3 const int N=1005;
4 int r,c,w,h,vis[N*6][N*6],d[2][6]={{2,-2,1,1,-1,-1},{0,0,3,-3,3,-3}};
5 char s[N*6][N*6];
6 struct qq{int x,y,d;}st;
7 inline bool chk(int x,int y){return x>0&&x<w&&y>0&&y<h;}
8 inline int bfs()
9 {
10 queue<qq>Q;qq u,v;u=st;vis[u.x][u.y]=1;Q.push(u);
11 while(!Q.empty())
12 {
13 v=Q.front();Q.pop();if(s[v.x][v.y]=='T')return v.d;
14 for(int i=0;i<6;i++)
15 {
16 int x=v.x+d[0][i],y=v.y+d[1][i],xx=x+d[0][i],yy=y+d[1][i];
17 if(s[x][y]==' '&&!vis[xx][yy]&&chk(xx,yy)){vis[xx][yy]=1;u.x=xx;u.y=yy;u.d=v.d+1;Q.push(u);}
18 }
19 }
20 return -1;
21 }
22 int main()
23 {
24 int T;scanf("%d",&T);
25 while(T--)
26 {
27 scanf("%d%d",&r,&c);getchar();w=4*r+3;h=6*c+3;
28 for(int i=0;i<w;i++)gets(s[i]);
29 for(int i=0;i<w;i++)
30 {
31 int l=strlen(s[i]);
32 for(int j=0;j<l;j++)if(s[i][j]=='S'){st.x=i,st.y=j,st.d=1;break;}
33 if(st.d)break;
34 }
35 int ans=bfs();
36 printf("%d\n",ans);
37 for(int i=0;i<w;i++)
38 {
39 int l=strlen(s[i]);
40 for(int j=0;j<l;j++)s[i][j]=vis[i][j]=0;
41 }
42 st.x=0;st.y=0;st.d=r=c=w=h=0;
43 }
44 return 0;
45 }
gym102028GShortest Paths on Random Forests
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int N=1000005,P=998244353; 4 int qn[N],qm[N],fac[N],ifac[N],r[N],f[N],g[N],h[N],c[N],t[N],A[N],B[N],C[N]; 5 inline int pw(int a,int b){int r=1;for(;b;b>>=1,a=1ll*a*a%P)if(b&1)r=1ll*r*a%P;return r;} 6 inline void ntt(int n,int*a,int f) 7 { 8 int l=1;while((1<<l)<n)l++; 9 for(int i=1;i<n;i++)r[i]=r[i>>1]>>1|(i&1)<<(l-1); 10 for(int i=0;i<n;i++)if(i<r[i])swap(a[i],a[r[i]]); 11 for(int i=2;i<=n;i<<=1) 12 { 13 int wn=pw(3,(P-1)/i);if(f==-1)wn=pw(wn,P-2); 14 for(int j=0;j<n;j+=i)for(int k=0,w=1;k<(i>>1);k++,w=1ll*w*wn%P){int u=a[j+k],v=1ll*a[j+k+(i>>1)]*w%P;a[j+k]=(u+v)%P;a[j+k+(i>>1)]=(u+P-v)%P;} 15 } 16 if(f==-1)for(int i=0,iv=pw(n,P-2);i<n;i++)a[i]=1ll*a[i]*iv%P; 17 } 18 void inv(int n,int*a,int*b) 19 { 20 if(n==1){b[0]=pw(a[0],P-2);b[1]=0;return;} 21 inv(n>>1,a,b);int m=n<<1; 22 for(int i=0;i<m;i++)A[i]=i<n?a[i]:0,b[i]=i<n?b[i]:0; 23 ntt(m,A,1);ntt(m,b,1); 24 for(int i=0;i<m;i++)b[i]=1ll*(P+2-1ll*A[i]*b[i]%P)*b[i]%P; 25 ntt(m,b,-1);for(int i=n;i<m;i++)b[i]=0; 26 } 27 void ln(int n,int*a,int*b) 28 { 29 inv(n,a,b);int m=n<<1; 30 for(int i=0;i<n-1;i++)B[i]=1ll*a[i+1]*(i+1)%P;B[n-1]=0; 31 for(int i=n;i<m;i++)b[i]=B[i]=0;ntt(m,b,1);ntt(m,B,1); 32 for(int i=0;i<m;i++)b[i]=1ll*b[i]*B[i]%P; 33 ntt(m,b,-1);for(int i=n-1;i;i--)b[i]=1ll*b[i-1]*pw(i,P-2)%P;b[0]=0; 34 } 35 void exp(int n,int*a,int*b) 36 { 37 if(n==1){b[0]=1;b[1]=0;return;} 38 exp(n>>1,a,b);int m=n<<1;ln(n,b,C); 39 for(int i=0;i<m;i++)C[i]=i<n?(a[i]+P-C[i])%P:0;C[0]++; 40 ntt(m,b,1);ntt(m,C,1);for(int i=0;i<m;i++)b[i]=1ll*b[i]*C[i]%P; 41 ntt(m,b,-1);for(int i=n;i<m;i++)b[i]=0; 42 } 43 void mul(int n,int*a,int*b,int*c) 44 { 45 for(int i=0;i<n;i++)A[i]=a[i],B[i]=b[i]; 46 int m=n<<1;for(int i=n;i<m;i++)A[i]=B[i]=0; 47 ntt(m,A,1);ntt(m,B,1); 48 for(int i=0;i<m;i++)c[i]=1ll*A[i]*B[i]%P; 49 ntt(m,c,-1);for(int i=n;i<m;i++)c[i]=0; 50 } 51 int main() 52 { 53 int T,n=0,nn=1;scanf("%d",&T); 54 for(int i=1;i<=T;i++)scanf("%d%d",&qn[i],&qm[i]),n=max(n,qn[i]); 55 while(nn<=n)nn<<=1;nn<<=1;ifac[1]=fac[0]=ifac[0]=1; 56 for(int i=2;i<nn;i++)ifac[i]=1ll*ifac[P%i]*(P-P/i)%P; 57 for(int i=1;i<nn;i++)fac[i]=1ll*fac[i-1]*i%P,ifac[i]=1ll*ifac[i-1]*ifac[i]%P; 58 c[1]=1;for(int i=2;i<=n;i++)c[i]=1ll*pw(i,i-2)*ifac[i]%P;exp(nn>>1,c,f); 59 c[1]=0;for(int i=2;i<=n;i++)c[i]=1ll*(i-1)*pw(i,i-1)%P*ifac[2]%P*ifac[i]%P; 60 mul(nn>>1,f,c,g);for(int i=1;i<=n;i++)c[i]=1ll*pw(i,i)*ifac[i]%P; 61 mul(nn>>1,c,c,t);c[0]=ifac[2];mul(nn>>1,t,c,h);mul(nn>>1,h,f,h); 62 for(int i=1;i<=n;i++)f[i]=1ll*f[i]*fac[i]%P,g[i]=1ll*g[i]*fac[i]%P,h[i]=1ll*h[i]*fac[i]%P; 63 for(int i=1;i<=T;i++) 64 { 65 int n=qn[i],m=qm[i],ans=1ll*f[n]*n%P*(n-1)%P*ifac[2]%P; 66 ans=(ans+P-g[n])%P;ans=1ll*ans*m%P*m%P;ans=(ans+h[n])%P; 67 printf("%d\n",1ll*ans*pw(f[n],P-2)%P); 68 } 69 return 0; 70 }
gym102028HCan You Solve the Harder Problem?
1 #include<bits/stdc++.h>
2 #include<unordered_map>
3 using namespace std;
4 #define ll long long
5 const int N=2e5+5;
6 int n,tot,lst,rt,a[N],lk[N<<1],len[N<<1],stk[N],ps[N<<1],f1[N][30];
7 ll f2[N][30];
8 unordered_map<int,int>ch[N<<1];
9 inline void extend(int c,int x)
10 {
11 int p=lst,np=lst=++tot;len[np]=len[p]+1;ps[np]=x;ch[p].clear();
12 for(;!ch[p].count(c);p=lk[p])ch[p][c]=np;
13 if(!p)lk[np]=rt;
14 else
15 {
16 int q=ch[p][c];
17 if(len[q]==len[p]+1)lk[np]=q;
18 else
19 {
20 int nq=++tot;ps[nq]=ps[q];len[nq]=len[p]+1;lk[nq]=lk[q];lk[q]=lk[np]=nq;ch[nq]=ch[q];
21 for(;ch[p][c]==q;p=lk[p])ch[p][c]=nq;
22 }
23 }
24 }
25 long long sol()
26 {
27 int tp=0;stk[0]=0;a[0]=1e9+7;ll ans=0;
28 for(int i=1;i<=n;i++)f1[i][0]=f2[i][0]=0;
29 for(int i=n;i>=1;stk[++tp]=i--)while(a[stk[tp]]<a[i]){f1[stk[tp]][0]=i;f2[stk[tp]][0]=1ll*(stk[tp]-i)*a[stk[tp]];tp--;}
30 for(int i=1;i<=18;i++)for(int j=1;j<=n;j++)f1[j][i]=f1[f1[j][i-1]][i-1],f2[j][i]=f2[j][i-1]+f2[f1[j][i-1]][i-1];
31 for(int i=2;i<=tot;i++)
32 {
33 int t1=ps[i]-len[lk[i]]+1,x=ps[i];
34 for(int j=18;j>=0;j--)if(f1[x][j]>=t1)x=f1[x][j];
35 int t2=ps[i]-len[i]+1;ans+=1ll*(t1-max(f1[x][0]+1,t2))*a[x];
36 x=f1[x][0];if(x<t2)continue;
37 for(int j=18;j>=0;j--)if(f1[x][j]>=t2)ans+=f2[x][j],x=f1[x][j];
38 ans+=1ll*(x-t2+1)*a[x];
39 }
40 return ans;
41 }
42 int main()
43 {
44 int T;scanf("%d",&T);
45 while(T--)
46 {
47 tot=lst=rt=1;ch[1].clear();scanf("%d",&n);
48 for(int i=1;i<=n;i++)scanf("%d",&a[i]),extend(a[i],i);
49 printf("%lld\n",sol());
50 }
51 return 0;
52 }
gym102028IDistance
1 #include<bits/stdc++.h>
2 using namespace std;
3 #define ll long long
4 const int N=100005;
5 int n,a[N];ll s1[N],s2[N];
6 int main()
7 {
8 int T;scanf("%d",&T);
9 while(T--)
10 {
11 scanf("%d",&n);a[1]=0;
12 for(int i=2;i<=n;i++)scanf("%d",&a[i]),a[i]+=a[i-1];
13 s1[0]=0;for(int i=1;i<=n;i++)s1[i]=s1[i-1]+a[i];
14 s2[n+1]=0;for(int i=n;i>=1;i--)s2[i]=s2[i+1]+a[i];
15 int l=0,r=n+1;ll s=0;
16 for(int i=1;i<=n;i++)
17 {
18 int p;if(i&1)p=l+1;else p=r-1;
19 s=s+1ll*l*a[p]-s1[l]+s2[r]-1ll*(n-r+1)*a[p];
20 if(i&1)l++;else r--;printf("%lld",s);if(i<n)putchar(' ');
21 }
22 puts("");
23 }
24 return 0;
25 }
gym102028JCarpets Removal
咕咕咕
gym102028KCounting Failures on a Trie
咕咕咕
gym102028LConnected Subgraphs
1 #include<bits/stdc++.h>
2 using namespace std;
3 #define ll long long
4 const int N=100005,P=1e9+7;
5 int n,m,d[N],c[N],ps[N],id[N],w[N];
6 vector<int>g[N],e[N];
7 inline int pw(int a,int b){int r=1;for(;b;b>>=1,a=1ll*a*a%P)if(b&1)r=1ll*r*a%P;return r;}
8 inline bool cmp(int x,int y){return d[x]<d[y];}
9 inline ll f1111(){ll ans=0,iv=pw(24,P-2);for(int i=1;i<=n;i++){ll t=iv;for(int j=0;j<4;j++)t=t*(d[i]-j)%P;ans=(ans+t)%P;}return ans;}
10 inline ll f112(){ll ans=0;for(int x=1;x<=n;x++)for(int i=0,y;i<e[x].size();i++)y=e[x][i],ans=(ans+1ll*(d[x]-1)*(d[x]-2)/2%P*(d[y]-1))%P;return ans;}
11 inline ll f22(){ll ans=0;for(int x=1;x<=n;x++)for(int i=0,y,p=0;i<e[x].size();i++)y=e[x][i],ans=(ans+1ll*p*(d[y]-1))%P,p+=d[y]-1;return ans;}
12 inline int c3()
13 {
14 int ans=0;for(int i=1;i<=n;i++)w[i]=0;
15 for(int x=1;x<=n;x++)
16 {
17 for(int i=0;i<g[x].size();i++)w[g[x][i]]=1;
18 for(int i=0;i<g[x].size();i++)for(int j=0,y=g[x][i],z;j<g[y].size();j++)if(w[z=g[y][j]])ans=(ans+1+e[x].size()+e[y].size()+e[z].size()-6)%P;
19 for(int i=0;i<g[x].size();i++)w[g[x][i]]=0;
20 }
21 return ans;
22 }
23 inline int c4()
24 {
25 int ans=0;for(int i=1;i<=n;i++)w[i]=0;
26 for(int x=1;x<=n;x++)
27 {
28 for(int i=0;i<e[x].size();i++)for(int j=0,y=e[x][i],z;j<g[y].size();j++)if(ps[x]<ps[z=g[y][j]]){ans=(ans+w[z])%P;w[z]++;}
29 for(int i=0;i<e[x].size();i++)for(int j=0,y=e[x][i];j<g[y].size();j++)w[g[y][j]]=0;
30 }
31 return ans;
32 }
33 int main()
34 {
35 int T;scanf("%d",&T);
36 while(T--)
37 {
38 scanf("%d%d",&n,&m);
39 for(int i=1;i<=n;i++)d[i]=0,id[i]=i,e[i].clear(),g[i].clear();
40 for(int i=1,u,v;i<=m;i++){scanf("%d%d",&u,&v);d[u]++;d[v]++;e[u].push_back(v);e[v].push_back(u);}
41 sort(id+1,id+n+1,cmp);
42 for(int i=1;i<=n;i++)ps[id[i]]=i;
43 for(int x=1;x<=n;x++)for(int i=0,y;i<e[x].size();i++)if(ps[x]<ps[y=e[x][i]])g[x].push_back(y);
44 printf("%d\n",(1ll*(f1111()+f112()+f22()-3ll*c3()-3ll*c4())%P+P)%P);
45 }
46 return 0;
47 }
gym102056AExotic … Ancient City
利用$w\leq30$可以模拟Kruskal的过程,每次加入所有边权为w的边,考虑新加入的边数=新减少的连通块数,转化为求解边权$\leq w$的连通块数,然后差分。(意义:相当于抹去了边权的影响,Kruskal求MST转化成并查集求连通块数,从而加入边的顺序可以重新定义。)
考虑将$\leq w$的边从左到右一列一列依次加入,维护当前2列的连通性(因为只关心加入边两端是否已经连通),并查集合并时列小的挂列大的下面,考虑如何从1~i-1列的并查集得到1~i列的并查集,显然1~i列的并查集可以认为是1~i-1和2~i两个一样的并查集重叠,考虑在1~i-1(等价于2~i)列的答案上加边:1~i-1的并查集信息相当于在i-1列内部有加边,考虑维护加入的边,显然每次要么加入,则2i个元素的并查集中连通块数减少1,否则这条边对之后的结果无影响,由于连通块数由2n变成1,复杂度$O(m\alpha(n))$。
1 #include<bits/stdc++.h>
2 using namespace std;
3 #define ll long long
4 #define pp pair<int,int>
5 #define X first
6 #define Y second
7 const int N=200005,W=31;
8 int n,m,k,fa[N];ll a[N],ans[N];
9 vector<pp>e[W];
10 int fnd(int x){return fa[x]==x?x:fa[x]=fnd(fa[x]);}
11 int main()
12 {
13 scanf("%d%d%d",&n,&m,&k);
14 for(int i=1,u,v,w;i<=k;i++){scanf("%d%d%d",&u,&v,&w);e[w].push_back(make_pair(u,v));}
15 for(int w=1;w<=30;w++)
16 {
17 for(int i=1;i<=n*2;i++)fa[i]=i;
18 memset(a,0,sizeof(a));vector<pp>g;
19 for(int i=1;i<=w;i++)for(int j=0;j<e[i].size();j++)
20 {
21 int u=fnd(e[i][j].X),v=fnd(e[i][j].Y+n);
22 if(u==v)continue;if(u>v)swap(u,v);
23 a[1]++;fa[u]=v;
24 if(u>n&&v>n)g.push_back(make_pair(u-n,v-n));
25 }
26 for(int i=2;i<=m;i++)
27 {
28 vector<pp>gg;
29 for(int j=0;j<g.size();j++)
30 {
31 int u=fnd(g[j].X),v=fnd(g[j].Y);
32 if(u==v){a[i]--;continue;}
33 if(u>v)swap(u,v);fa[u]=v;
34 if(u>n&&v>n)gg.push_back(make_pair(u-n,v-n));
35 }
36 g=gg;
37 }
38 for(int i=2;i<=m;i++)a[i]+=a[i-1];
39 for(int i=2;i<=m;i++)a[i]+=a[i-1];
40 for(int i=1;i<=m;i++)if(w<30)ans[i]-=a[i];else ans[i]+=w*a[i];
41 }
42 for(int i=1;i<=m;i++)printf("%lld\n",ans[i]);
43 return 0;
44 }
gym102056BMysterious … Host
1 #include<bits/stdc++.h>
2 using namespace std;
3 #define ll long long
4 const int N=5050;
5 ll n,P,dp[N][5];
6 int main()
7 {
8 scanf("%lld%lld",&n,&P);dp[1][1]=1;
9 for(int i=2;i<=n;i++)
10 {
11 for(int j=1;j<i;j++)for(int k=1;k<=4;k++)(dp[i][min(4,k+1)]+=dp[i-j][k]*dp[j][1])%=P;
12 dp[i][1]=(dp[i][2]+dp[i][3]+dp[i][4]*2)%P;
13 }
14 for(int i=1;i<=n;i++)printf("%lld\n",dp[i][1]);
15 return 0;
16 }
gym102056CHeretical … Möbius
1 #include<bits/stdc++.h>
2 using namespace std;
3 #define ll long long
4 const int M=5e4+10,inf=1e9+7,lcm=901800900;
5 ll ans=inf,a[210],u[M],p[6],g[6]={4,9,25,49,121,169};
6 vector<ll>pr,v[6];bool vis[M];map<ll,ll>mp;
7 inline ll mu(ll x)
8 {
9 if(x<M)return u[x];
10 if(mp.count(x))return mp[x];
11 for(int i=0;i<pr.size();i++)
12 {
13 if(pr[i]*pr[i]>x)break;
14 if(x%(pr[i]*pr[i])==0)return mp[x]=0;
15 if(x%pr[i]==0)x/=pr[i];
16 }
17 return mp[x]=1;
18 }
19 void exgcd(ll a,ll b,ll&x,ll&y){if(!b){x=1;y=0;}else{exgcd(b,a%b,y,x);y-=a/b*x;}}
20 inline ll crt(int n,ll*a,ll*m)
21 {
22 ll M=m[0],R=a[0],x,y;
23 for(int i=1;i<n;i++)
24 {
25 ll c=(a[i]-R%m[i]+m[i])%m[i];exgcd(M,m[i],x,y);
26 x=(x%m[i])*(c%m[i]);x=(x%m[i]+m[i])%m[i];
27 R=M*x+R;M*=m[i];R=(R%M+M)%M;
28 }
29 return R;
30 }
31 void calc()
32 {
33 for(ll i=crt(6,p,g);i+199<=1e9;i+=lcm)
34 {
35 if(i>=ans)break;int ff=1;
36 for(ll j=i;j<=i+199;j++)if(mu(j)!=a[j-i+1]){ff=0;break;}
37 if(ff){ans=min(ans,i);break;}
38 }
39 }
40 void dfs(int x)
41 {
42 if(x==6){calc();return;}
43 random_shuffle(v[x].begin(),v[x].end());
44 for(int j=0;j<v[x].size();j++){p[x]=v[x][j];dfs(x+1);}
45 }
46 int main()
47 {
48 for(int i=1;i<M;i++)u[i]=1;
49 for(int i=2;i<M;i++)
50 {
51 if(!vis[i]){pr.push_back(i);for(ll j=1ll*i*i;j<M;j+=1ll*i*i)u[j]=0;}
52 for(int j=0;j<pr.size();j++)
53 {
54 ll t=1ll*i*pr[j];
55 if(t<M){vis[t]=1;if(i%pr[j]==0)break;}else break;
56 }
57 }
58 int tot=0;
59 for(int i=0;i<10;i++)
60 {
61 char s[30];scanf("%s",s);
62 for(int j=1;j<=20;j++)a[i*20+j]=s[j-1]-'0',tot+=s[j-1]-'0';
63 }
64 if(tot<=70){puts("-1");return 0;}
65 for(int i=0;i<6;i++)for(int j=1;j<=g[i];j++)
66 {
67 int f=1;for(int k=j;k<=200&&f;k+=g[i])f=a[k]^1;
68 if(f)v[i].push_back((g[i]-j+1)%g[i]);
69 }
70 for(int i=0;i<6;i++)if(v[i].size()==0){puts("-1");return 0;}
71 dfs(0);if(ans!=inf){printf("%d\n",ans);return 0;}else puts("-1");
72 return 0;
73 }
gym102056DDeja vu of … Go Players
1 #include<bits/stdc++.h>
2 using namespace std;
3 int main()
4 {
5 int T;scanf("%d",&T);
6 while(T--)
7 {
8 int n,m;scanf("%d%d",&n,&m);
9 puts(n>m?"No":"Yes");
10 for(int i=0,x;i<n+m;i++)scanf("%d",&x);
11 }
12 return 0;
13 }
gym102056EImmortal … Universe
咕咕咕
gym102056FInterstellar … Fantasy
1 #include<bits/stdc++.h>
2 using namespace std;
3 #define ll long long
4 struct P{int x,y,z;};
5 inline P operator+(const P&a,const P&b){return (P){a.x+b.x,a.y+b.y,a.z+b.z};}
6 inline P operator-(const P&a,const P&b){return (P){a.x-b.x,a.y-b.y,a.z-b.z};}
7 inline int operator*(const P&a,const P&b){return a.x*b.x+a.y*b.y+a.z*b.z;}
8 inline double len(const P&a){return sqrt(a*a);}
9 inline ll sqr(int x){return 1ll*x*x;}
10 int main()
11 {
12 int T;scanf("%d",&T);
13 while(T--)
14 {
15 P o,a,b;int r;scanf("%d%d%d%d%d%d%d%d%d%d",&o.x,&o.y,&o.z,&r,&a.x,&a.y,&a.z,&b.x,&b.y,&b.z);
16 if((a-b)*(a-b)==0||(o-a)*(b-a)<=0||(o-b)*(a-b)<=0)printf("%.9lf\n",len(a-b));
17 else
18 {
19 double h=sqrt((o-a)*(o-a)-(double)sqr((o-a)*(b-a))/((a-b)*(a-b)));
20 if(r<=h)printf("%.9lf\n",len(a-b));
21 else
22 {
23 double la=sqrt((o-a)*(o-a)-r*r),lb=sqrt((o-b)*(o-b)-r*r),ang=atan2(sqrt((o-a)*(o-a)-h*h),h)+atan2(sqrt((o-b)*(o-b)-h*h),h)-atan2(la,r)-atan2(lb,r);
24 printf("%.9lf\n",ang*r+la+lb);
25 }
26 }
27 }
28 return 0;
29 }
gym102056GOmnipotent … Garland
1 #include<bits/stdc++.h>
2 using namespace std;
3 const int N=1000005;
4 int n,m,k,b[N];char t[N];
5 vector<int>g[N],ans[N];
6 struct cmp
7 {
8 bool operator()(int x,int y)
9 {
10 if((!g[x].size())!=(!g[y].size()))return !g[x].size();
11 if((!x)!=(!y))return !x;return g[x].size()<g[y].size();
12 }
13 };
14 inline void psh(int i,int l,int r){while(((++l)%=n)!=r)g[i].push_back(l);}
15 bool sol()
16 {
17 scanf("%d%d%d%s",&n,&m,&k,t);int tp=0;
18 for(int i=0;i<n;i++)if(t[i]=='B')b[++tp]=i;
19 if(n%k||n/k<m||tp<2*m)return 0;
20 if(m==1)
21 {
22 for(int i=0;i<n;i++)if(t[i]=='B'&&t[(i+1)%n]=='B'){ans[1].resize(n);for(int j=0;j<n;j++)ans[1][j]=j;return 1;}
23 return 0;
24 }
25 int len=0;for(int i=1;i<=m;i++)len+=b[i*2]-b[i*2-1]-1;
26 if(len>n-2*m-len){int t=b[2*m];for(int i=2*m;i>=2;i--)b[i]=b[i-1];b[1]=t;}
27 for(int i=1;i<=m;i++)
28 {
29 ans[i].resize(2);ans[i][0]=b[i*2-1];ans[i][1]=b[i*2];
30 psh(i,b[i*2-1],b[i*2]);psh(0,b[i*2],b[i*2%(2*m)+1]);
31 }
32 priority_queue<int,vector<int>,cmp>Q;
33 for(int i=0;i<=m;i++)Q.push(i);
34 for(int i=1;i<=m;i++)
35 {
36 while(ans[i].size()%k)
37 {
38 int x=Q.top();Q.pop();
39 if(x==i)
40 {
41 int y=Q.top();Q.pop();
42 ans[i].push_back(g[y].back());g[y].pop_back();
43 Q.push(y);
44 }
45 else ans[i].push_back(g[x].back()),g[x].pop_back();
46 Q.push(x);
47 }
48 }
49 for(int x=m;x>=0;x--)
50 {
51 int i=x%m+1;
52 for(int j=0;j<g[x].size();j++)ans[i].push_back(g[x][j]);
53 g[x].clear();
54 while(ans[i].size()%k)ans[i].push_back(g[0].back()),g[0].pop_back();
55 }
56 return 1;
57 }
58 int main()
59 {
60 int T;scanf("%d",&T);
61 while(T--)
62 {
63 if(!sol())puts("No");
64 else
65 {
66 puts("Yes");
67 for(int i=1;i<=m;i++)
68 {
69 sort(ans[i].begin(),ans[i].end());
70 printf("%d",(int)(ans[i].size()));
71 for(int j=0;j<ans[i].size();j++)printf(" %d",ans[i][j]);
72 puts("");
73 }
74 }
75 }
76 return 0;
77 }
gym102056HSaintly … Coins
咕咕咕
gym102056IMisunderstood … Missing
1 #include<bits/stdc++.h>
2 using namespace std;
3 #define ll long long
4 const int N=105,M=5505;
5 int n;ll a[N],b[N],c[N],dp[2][N][M];
6 int main()
7 {
8 int T;scanf("%d",&T);
9 while(T--)
10 {
11 scanf("%d",&n);
12 for(int i=1;i<=n;i++)scanf("%lld%lld%lld",&a[i],&b[i],&c[i]);
13 for(int i=0;i<2;i++)for(int j=0;j<=n;j++)for(int k=0;k<=5050;k++)dp[i][j][k]=-1;
14 dp[n&1^1][0][0]=0;ll res=0;
15 for(int i=n;i>=1;i--)for(int j=0;j<=n;j++)for(int k=0;k<=5050;k++)if(~dp[i&1^1][j][k])
16 {
17 dp[i&1][j+1][k+i]=max(dp[i&1][j+1][k+i],dp[i&1^1][j][k]+a[i]);
18 dp[i&1][j][k]=max(dp[i&1][j][k],dp[i&1^1][j][k]+1ll*(k-j*i)*b[i]);
19 dp[i&1][j][k]=max(dp[i&1][j][k],dp[i&1^1][j][k]+1ll*j*c[i]);
20 }
21 for(int i=1;i<=n;i++)for(int j=1;j<=5050;j++)res=max(res,dp[1][i][j]);
22 printf("%lld\n",res);
23 }
24 return 0;
25 }
gym102056JPhilosophical … Balance
1 #include<bits/stdc++.h>
2 using namespace std;
3 #define ll long long
4 const int N=200010;
5 int n,cc,cnt=1,lst=1,fa[N<<1],len[N<<1],ch[N<<1][26],tg[N<<1],hd[N<<1];char s[N];double dp[N<<1];
6 struct E{int v,nxt;}e[N<<1];
7 inline void add(int u,int v){e[++cc]=(E){v,hd[u]};hd[u]=cc;}
8 inline void extend(int c)
9 {
10 int p=lst,np=lst=++cnt;tg[np]=1;len[np]=len[p]+1;
11 for(;!ch[p][c];p=fa[p])ch[p][c]=np;
12 if(!p)fa[np]=1;
13 else
14 {
15 int q=ch[p][c];
16 if(len[q]==len[p]+1)fa[np]=q;
17 else
18 {
19 int nq=++cnt;len[nq]=len[p]+1;tg[nq]=0;fa[nq]=fa[q];fa[q]=fa[np]=nq;memcpy(ch[nq],ch[q],sizeof(ch[q]));
20 for(;ch[p][c]==q;p=fa[p])ch[p][c]=nq;
21 }
22 }
23 }
24 void dfs(int x)
25 {
26 if(tg[x]){dp[x]=len[x];return;}double s=0;
27 for(int i=hd[x];i;i=e[i].nxt)dfs(e[i].v),s+=1.0/(dp[e[i].v]-len[x]);
28 dp[x]=1.0/s+len[x];
29 }
30 void sol()
31 {
32 scanf("%s",s+1);n=strlen(s+1);cc=0;lst=cnt=1;
33 for(int i=0;i<=2*n+5;i++)hd[i]=0,memset(ch[i],0,sizeof(ch[i]));
34 for(int i=n;i>=1;i--)extend(s[i]-'a');
35 for(int i=2;i<=cnt;i++)add(fa[i],i);
36 dfs(1);printf("%.10lf\n",dp[1]);
37 }
38 int main(){int T;scanf("%d",&T);while(T--)sol();return 0;}
gym102056KDesperate … Fire Survive
咕咕咕
gym102056LEventual … Journey
1 #include<bits/stdc++.h>
2 using namespace std;
3 const int N=100005;
4 int n,m,a[N],c[2],d[N],f[N];
5 int main()
6 {
7 scanf("%d%d",&n,&m);
8 for(int i=0;i<n;i++)scanf("%d",&a[i]),c[a[i]]++;
9 for(int i=0,u,v;i<m;i++)
10 {
11 scanf("%d%d",&u,&v);u--;v--;
12 if(a[u]!=a[v])d[u]++,d[v]++;
13 }
14 for(int i=0;i<n;i++)if(d[i])f[a[i]]++;
15 for(int i=0;i<n;i++)
16 {
17 int ans=0;
18 if(!d[i])
19 {
20 ans+=c[a[i]]-1;
21 ans+=2*f[a[i]^1];
22 ans+=3*(c[a[i]^1]-f[a[i]^1]);
23 }
24 else
25 {
26 ans+=c[a[i]]-1;
27 ans+=d[i];
28 ans+=2*(c[a[i]^1]-d[i]);
29 }
30 printf("%d%c",ans,i<n-1?' ':'\n');
31 }
32 return 0;
33 }
gym102012ARikka with Minimum Spanning Trees
1 #include<bits/stdc++.h>
2 using namespace std;
3 #define ll unsigned long long
4 const int N=200500,M=200500,P=1e9+7;
5 int n,m,tot,f[N];ll k1,k2;
6 struct E{ll u,v,w;}e[N];
7 inline void add(ll u,ll v,ll w){e[tot++]=(E){u,v,w};}
8 inline bool cmp(E a,E b){return a.w<b.w;}
9 inline ll fnd(ll x){return f[x]==-1?x:f[x]=fnd(f[x]);}
10 inline ll gt(){ll k3=k1,k4=k2;k1=k4;k3^=k3<<23;k2=k3^k4^(k3>>17)^(k4>>26);return k2+k4;}
11 inline void gen()
12 {
13 scanf("%d%d%llu%llu",&n,&m,&k1,&k2);
14 for(int i=1;i<=m;i++){ll u=gt()%n+1,v=gt()%n+1,w=gt();add(u,v,w);add(v,u,w);}
15 }
16 ll sol()
17 {
18 memset(f,-1,sizeof(f));sort(e,e+tot,cmp);ll cnt=0,ans=0;
19 for(int i=0;i<tot;i++)
20 {
21 ll u=e[i].u,v=e[i].v,w=e[i].w,f1=fnd(u),f2=fnd(v);
22 if(f1!=f2){ans=(ans+w)%P;f[f1]=f2;cnt++;}
23 if(cnt==n-1)break;
24 }
25 if(cnt<n-1)return 0;else return ans%P;
26 }
27 int main()
28 {
29 int T;scanf("%d",&T);
30 while(T--){tot=0;gen();ll ans=sol();printf("%lld\n",ans);}
31 return 0;
32 }
gym102012BRikka with Line Graphs
咕咕咕
gym102012CRikka with Consistency
1 #include<bits/stdc++.h>
2 using namespace std;
3 #define db double
4 const int N=55;
5 int n,a[N],vis[N][N][N];db b[N][N],d[N][N][N];
6 struct D{double d;int x,y,z;};
7 bool operator<(D a,D b){return a.d>b.d;}
8 priority_queue<D>Q;
9 inline void upd(int x,int y,int z,db w){if(d[x][y][z]>w)d[x][y][z]=w;Q.push((D){w,x,y,z});}
10 int main()
11 {
12 int T;scanf("%d",&T);
13 for(int i=0;i<=50;i++)for(int j=0;j<=50;j++)if(i!=j)b[i][j]=sqrt((i-j)*(i-j)+1)/abs(i-j);
14 while(T--)
15 {
16 scanf("%d",&n);
17 for(int i=0;i<=n;i++)scanf("%d",&a[i]);
18 for(int i=0;i<=n;i++)for(int j=0;j<=n;j++)for(int k=0;k<=50;k++)d[i][j][k]=1e9,vis[i][j][k]=0;
19 d[0][n][0]=0;Q.push((D){0,0,n,0});
20 while(!Q.empty())
21 {
22 int x=Q.top().x,y=Q.top().y,z=Q.top().z;Q.pop();
23 if(vis[x][y][z])continue;vis[x][y][z]=1;
24 if(x&&a[x]==z)upd(x-1,y,z,d[x][y][z]+(a[x-1]==a[x]));
25 if(x<n&&a[x+1]==z)upd(x+1,y,z,d[x][y][z]+(a[x+1]==a[x]));
26 if(y&&a[y]==z)upd(x,y-1,z,d[x][y][z]+(a[y-1]==a[y]));
27 if(y<n&&a[y+1]==z)upd(x,y+1,z,d[x][y][z]+(a[y+1]==a[y]));
28 if(x<n&&y<n)
29 {
30 if(min(a[x+1],a[x])<=z-1&&max(a[x+1],a[x])>=z-1&&min(a[y+1],a[y])<=z-1&&max(a[y+1],a[y])>=z-1)upd(x,y,z-1,d[x][y][z]+b[a[x+1]][a[x]]+b[a[y+1]][a[y]]);
31 if(min(a[x+1],a[x])<=z+1&&max(a[x+1],a[x])>=z+1&&min(a[y+1],a[y])<=z+1&&max(a[y+1],a[y])>=z+1)upd(x,y,z+1,d[x][y][z]+b[a[x+1]][a[x]]+b[a[y+1]][a[y]]);
32 }
33 }
34 printf("%.10lf\n",d[n][0][0]);
35 }
36 return 0;
37 }
gym102012DRikka with Subsequences
1 #include<bits/stdc++.h>
2 using namespace std;
3 const int N=250,P=1e9+7;
4 int n,a[N],f[N][N],s[N][N];char c[N][N];
5 int main()
6 {
7 int T;scanf("%d",&T);
8 while(T--)
9 {
10 scanf("%d",&n);
11 for(int i=0;i<n;i++)scanf("%d",&a[i]),a[i]--;
12 for(int i=0;i<n;i++)scanf("%s",c[i]);
13 for(int i=0;i<n;i++)for(int j=0;j<n;j++)f[i][j]=0;
14 int ans=0;
15 for(int i=0;i<n;i++)
16 {
17 for(int j=0;j<n;j++)for(int k=0;k<n;k++)
18 {
19 s[j+1][k+1]=c[a[j]][a[i]]=='1'?f[j][k]:0;
20 s[j+1][k+1]=(s[j+1][k+1]+s[j+1][k])%P;
21 s[j+1][k+1]=(s[j+1][k+1]+s[j][k+1])%P;
22 s[j+1][k+1]=(s[j+1][k+1]+P-s[j][k])%P;
23 }
24 for(int j=0;j<n;j++)for(int k=0;k<n;k++)if(a[i]==a[j]&&a[i]==a[k]){ans=(ans+s[j][k]+1)%P;f[j][k]=(f[j][k]+s[j][k]+1)%P;}
25 }
26 printf("%d\n",ans);
27 }
28 return 0;
29 }
gym102012ERikka with Data Structures
咕咕咕
gym102012FRikka with Nice Counting Striking Back
咕咕咕
gym102012GRikka with Intersections of Paths
1 #include<bits/stdc++.h>
2 using namespace std;
3 const int N=400005,P=1e9+7;
4 int n,m,k,ans,fac[N],ifac[N],fa[N][22],dep[N],f1[N],f2[N];
5 vector<int>g[N];
6 inline int pw(int a,int b){int r=1;for(;b;b>>=1,a=1ll*a*a%P)if(b&1)r=1ll*r*a%P;return r;}
7 inline int C(int a,int b){return a<b||a<0||b<0?0:1ll*fac[a]*ifac[b]%P*ifac[a-b]%P;}
8 void dfs(int x,int p)
9 {
10 fa[x][0]=p;dep[x]=dep[p]+1;
11 for(int i=0;i<g[x].size();i++)if(g[x][i]!=p){int y=g[x][i];dfs(y,x);}
12 }
13 void dfss(int x,int p)
14 {
15 for(int i=0;i<g[x].size();i++)if(g[x][i]!=p){int y=g[x][i];dfss(y,x);f1[x]+=f1[y];f2[x]+=f2[y];}
16 if(x!=1)ans=(ans+P-C(f1[x],k))%P;ans=(ans+C(f2[x],k))%P;
17 }
18 inline int lca(int u,int v)
19 {
20 if(dep[u]<dep[v])swap(u,v);int sub=dep[u]-dep[v];
21 for(int i=20;i>=0;i--)if(sub>>i&1)u=fa[u][i];
22 if(u==v)return u;
23 for(int i=20;i>=0;i--)if(fa[u][i]!=fa[v][i])u=fa[u][i],v=fa[v][i];
24 return fa[u][0];
25 }
26 int main()
27 {
28 for(int i=fac[0]=1;i<N;i++)fac[i]=1ll*fac[i-1]*i%P;
29 ifac[N-1]=pw(fac[N-1],P-2);
30 for(int i=N-1;i;i--)ifac[i-1]=1ll*ifac[i]*i%P;
31 int T;scanf("%d",&T);
32 while(T--)
33 {
34 scanf("%d%d%d",&n,&m,&k);ans=0;
35 for(int i=1;i<=n;i++)g[i].clear();
36 for(int i=1;i<n;i++){int u,v;scanf("%d%d",&u,&v);g[u].push_back(v);g[v].push_back(u);}
37 dfs(1,0);for(int i=1;i<=n;i++)f1[i]=f2[i]=0;
38 for(int i=1;i<=20;i++)for(int j=1;j<=n;j++)fa[j][i]=fa[fa[j][i-1]][i-1];
39 for(int i=1;i<=m;i++)
40 {
41 int u,v;scanf("%d%d",&u,&v);int t=lca(u,v);
42 f1[u]++;f1[v]++;f1[t]-=2;f2[u]++;f2[v]++;f2[t]--;f2[fa[t][0]]--;
43 }
44 dfss(1,0);printf("%d\n",ans);
45 }
46 return 0;
47 }
gym102012HRikka with A Long Colour Palette
1 #include<bits/stdc++.h>
2 using namespace std;
3 #define X first
4 #define Y second
5 const int N=300005;
6 int n,m,ans[N];
7 pair<pair<int,int>,int>a[N];
8 int main()
9 {
10 int T;scanf("%d",&T);
11 while(T--)
12 {
13 scanf("%d%d",&n,&m);int len=0,nw=0;
14 for(int i=0;i<n;i++)scanf("%d%d",&a[i].X.X,&a[i].X.Y),a[i].Y=i;
15 if(n<m)
16 {
17 printf("%d\n",0);
18 for(int i=0;i<n;i++)printf("%d%c",1,i==n-1?'\n':' ');
19 continue;
20 }
21 sort(a,a+n);priority_queue<pair<int,int> >q;
22 for(int i=0;i<m;i++)q.push(make_pair(0,i));
23 for(int i=0;i<n;i++)
24 {
25 int l=a[i].X.X;pair<int,int>p=q.top();q.pop();
26 len+=max(min(l,-p.X)-nw,0);nw=l;
27 p.X=-max(-p.X,a[i].X.Y);ans[a[i].Y]=p.Y;q.push(p);
28 }
29 len+=max(-q.top().X-nw,0);printf("%d\n",len);
30 for(int i=0;i<n;i++)printf("%d%c",ans[i]+1,i==n-1?'\n':' ');
31 }
32 return 0;
33 }
gym102012IRikka with Sorting Networks
1 #include<bits/stdc++.h>
2 using namespace std;
3 const int N=55;
4 int n,m,p,ans,a[N],u[N],v[N];
5 void dfs(int x,int t)
6 {
7 if(x==0){ans+=t;return;}
8 if(a[u[x]]<a[v[x]]){dfs(x-1,t);swap(a[u[x]],a[v[x]]);dfs(x-1,t);swap(a[u[x]],a[v[x]]);}
9 }
10 int main()
11 {
12 int T;scanf("%d",&T);
13 while(T--)
14 {
15 scanf("%d%d%d",&n,&m,&p);ans=0;
16 for(int i=1;i<=m;i++)scanf("%d%d",&u[i],&v[i]),u[i]--,v[i]--;
17 for(int i=0;i<n;i++)a[i]=i;dfs(m,1);
18 for(int i=0;i<n-1;i++){swap(a[i],a[i+1]);dfs(m,-1);swap(a[i],a[i+1]);}
19 for(int t=0;t<n;t++)for(int i=0;i<n;i++)if(i!=t)
20 {
21 int c=0;
22 for(int j=0;j<n;j++)if(j==i)a[j]=t;else{if(c==t)c++;a[j]=c++;}
23 dfs(m,1);
24 }
25 printf("%d\n",ans);
26 }
27 return 0;
28 }
gym102012JRikka with An Unnamed Temple
咕咕咕
gym102012KRikka with Ants
咕咕咕
gym102012LRikka with Grid Graphs
1 #include<bits/stdc++.h>
2 using namespace std;
3 #define ll long long
4 int main()
5 {
6 int T,n,m;scanf("%d",&T);
7 while(T--)
8 {
9 cin>>n>>m;vector<string>mp(2*n-1);getline(cin,mp[0]);
10 for(int i=0;i<2*n-1;i++){getline(cin,mp[i]);while((int)mp[i].size()<2*m-1)mp[i]+=' ';}
11 unordered_map<ll,ll>dp[2];
12 int p=0,q=1;dp[p][0]=1;
13 for(int x=0;x<n;x++)for(int y=0;y<m;y++)
14 {
15 swap(p,q);dp[p].clear();
16 int cu=(x>0&&mp[2*x-1][2*y]=='|'),cl=(y>0&&mp[2*x][2*y-1]=='-');
17 for(unordered_map<ll,ll>::iterator it=dp[q].begin();it!=dp[q].end();it++)
18 {
19 ll st1=it->first;vector<int>g(m);
20 for(int i=0,k=0;i<m;i++)for(int j=0;j<m;j++,k++)g[i]|=(st1>>k&1)<<j;
21 for(int u=0;u<=cu;u++)for(int l=0;l<=cl;l++)
22 {
23 vector<int>h=g;h.push_back(0);
24 h[m]|=u<<y,h[y]|=(cu-u)<<m;
25 if(y)h[m]|=l<<(y-1),h[y-1]|=(cl-l)<<m;
26 for(int k=0;k<=m;k++)for(int i=0;i<=m;i++)if(h[i]>>k&1)h[i]|=h[k];
27 bool ff=1;for(int i=0;i<=m;i++)ff&=h[i]>>i&1^1;
28 if(!ff)continue;ll st2=0;
29 for(int i=0,k=0;i<m;i++)for(int j=0;j<m;j++,k++)st2|=(ll)(h[(i==y?m:i)]>>(j==y?m:j)&1)<<k;
30 dp[p][st2]+=it->second;
31 }
32 }
33 }
34 ll res=0;for(unordered_map<ll,ll>::iterator it=dp[p].begin();it!=dp[p].end();it++)res+=it->second;printf("%lld\n",res);
35 }
36 return 0;
37 }
gym102012MRikka with Illuminations
1 #include<bits/stdc++.h>
2 using namespace std;
3 #define ll long long
4 const int N=2010;
5 int T,n,m,L,R,ans,t[N],f[N],po[N],res[N];
6 struct P
7 {
8 ll x,y;int i;
9 bool operator<(const P &b)const{return x*b.y-y*b.x>0;}
10 P operator-(const P &b)const{return (P){x-b.x,y-b.y,i};}
11 }ps,p[N],s[N];
12 int main()
13 {
14 scanf("%d",&T);
15 while(T--)
16 {
17 int n,m;scanf("%d%d",&n,&m);ans=m+1;
18 memset(f,0,sizeof(f));memset(t,0,sizeof(t));
19 for(int i=1;i<=n;i++)scanf("%lld%lld",&s[i].x,&s[i].y),s[i].i=i;
20 for(int i=1;i<=m;i++)
21 {
22 ll x,y;scanf("%lld%lld",&x,&y);ps=(P){x,y,0};
23 for(int j=1;j<=n;j++)p[j]=s[j]-ps;
24 sort(p+1,p+n+1);L=p[n].i;R=p[1].i-1;
25 if(R<L)R+=n;if(R>f[L])f[L]=R,t[L]=i;
26 if(R<=n&&n+R>f[n+L])f[n+L]=n+R,t[n+L]=i;
27 }
28 for(int i=1;i<=2*n;i++)if(f[i-1]>f[i])f[i]=f[i-1],t[i]=t[i-1];
29 for(int i=1;i<=n;i++)
30 {
31 int ff=0,cc=0;
32 for(int j=i;j<n+i;j=f[j]+1)if(f[j]+1<=j){ff=1;break;}else po[++cc]=t[j];
33 if(!ff&&cc<ans){ans=cc;for(int j=1;j<=cc;j++)res[j]=po[j];}
34 }
35 if(ans==m+1)ans=-1;printf("%d\n",ans);
36 if(ans>0){for(int i=1;i<ans;i++)printf("%d ",res[i]);printf("%d\n",res[ans]);}
37 }
38 return 0;
39 }
gym101955ASockpuppets
1 #include<bits/stdc++.h>
2 using namespace std;
3 const int N=100005,P=1e9+7;
4 int n,m,tot=0,f[N][12][22],g[N][12][22];char c[N];
5 struct st
6 {
7 int ch[26],o;
8 void init(){o=0;memset(ch,0,sizeof(ch));}
9 }p[N];
10 inline void init(){tot=1;p[1].init();}
11 void ins(char*s,int o)
12 {
13 int l=strlen(s+1),x=1;
14 for(int i=1,c;i<=l;x=p[x].ch[c],i++)if(!p[x].ch[c=s[i]-'a'])p[p[x].ch[c]=++tot].init();
15 p[x].o=o;
16 }
17 void dfs(int x)
18 {
19 for(int i=0;i<=10;i++)for(int j=0;j<=20;j++)f[x][i][j]=g[x][i][j]=0;
20 f[x][0][0]=1;
21 for(int i=0;i<26;i++)
22 {
23 int v=p[x].ch[i];if(!v)continue;dfs(v);
24 for(int j=10;~j;j--)for(int k=20;~k;k--)if(f[x][j][k])for(int l=0;l<=10-j;l++)for(int t=0;t<=20-k;t++)g[x][j+l][k+t]=(g[x][j+l][k+t]+1ll*f[x][j][k]*f[v][l][t])%P;
25 for(int j=0;j<=10;j++)for(int k=0;k<=20;k++)f[x][j][k]=g[x][j][k],g[x][j][k]=0;
26 }
27 if(!p[x].o)return;
28 for(int i=0;i<=10;i++)for(int j=0;j<=20;j++)if(f[x][i][j])
29 {
30 if(p[x].o==1)
31 {
32 if(i+1<=10)g[x][i+1][j]=(g[x][i+1][j]+f[x][i][j])%P;
33 if(j-1>=0)g[x][i][j-1]=(g[x][i][j-1]+1ll*f[x][i][j]*j)%P;
34 if(i+2<=10)g[x][i+2][j]=(g[x][i+2][j]+1ll*f[x][i][j]*(P+1)/2)%P;
35 if(j-2>=0)g[x][i][j-2]=(g[x][i][j-2]+1ll*f[x][i][j]*j*(j-1)/2)%P;
36 if(i+1<=10&&j-1>=0)g[x][i+1][j-1]=(g[x][i+1][j-1]+1ll*f[x][i][j]*j)%P;
37 }
38 else
39 {
40 if(i-1>=0)g[x][i-1][j]=(g[x][i-1][j]+1ll*f[x][i][j]*i)%P;
41 if(j+1<=20)g[x][i][j+1]=(g[x][i][j+1]+f[x][i][j])%P;
42 }
43 }
44 for(int i=0;i<=10;i++)for(int j=0;j<=20;j++)f[x][i][j]=(f[x][i][j]+g[x][i][j])%P;
45 }
46 int main()
47 {
48 int T,Cs=0;scanf("%d",&T);
49 while(T--)
50 {
51 init();scanf("%d%d",&n,&m);
52 for(int i=1;i<=n;i++)scanf("%s",c+1),ins(c,1);
53 for(int i=1;i<=m;i++)scanf("%s",c+1),ins(c,2);
54 dfs(1);printf("Case #%d: %d\n",++Cs,f[1][0][0]);
55 }
56 return 0;
57 }
gym101955BSequences Generator
咕咕咕
gym101955CInsertion Sort
1 #include<bits/stdc++.h>
2 using namespace std;
3 #define ll long long
4 const int N=55;
5 ll fac[N];
6 int main()
7 {
8 int T,Cs=0;scanf("%d",&T);
9 while(T--)
10 {
11 ll n,k,q;scanf("%lld%lld%lld",&n,&k,&q);
12 for(int i=fac[0]=1;i<N;i++)fac[i]=fac[i-1]*i%q;
13 if(n<=k){printf("Case #%d: %lld\n",++Cs,fac[n]);continue;}
14 ll ans=((n-1)*(n-k)%q+1)%q*fac[k]%q;
15 printf("Case #%d: %lld\n",++Cs,ans);
16 }
17 return 0;
18 }
gym101955DDiameter of a Tree
1 #include<bits/stdc++.h>
2 using namespace std;
3 #define ll long long
4 const int N=1000005;
5 int n,m,S,tt,cc,cnt,res,rt,s1,s2,vis[N<<1],hd[N],sz[N],v[N];ll ans[N];double P[N],Q[N];
6 struct E{int v,nxt,a,b;}e[N];
7 inline void add(int u,int v,int a,int b){e[++tt]=(E){v,hd[u],a,b};hd[u]=tt;vis[tt]=0;e[++tt]=(E){u,hd[v],a,b};hd[v]=tt;vis[tt]=0;}
8 struct G{int u,v,a,b;}g[N];
9 struct st{ll k,b;}p[N],q[N],r[N*50];
10 inline bool operator<(const st&a,const st&b){return a.k==b.k?a.b>b.b:a.k<b.k;}
11 inline bool chk(st&a,st&b,st&c){return 1.0*(c.b-b.b)/(b.k-c.k)<=1.0*(b.b-a.b)/(a.k-b.k);}
12 struct qq{ll w;int i;}W[N];
13 inline bool cmp(qq a,qq b){return a.w<b.w;}
14 void dfs(int x,int p)
15 {
16 for(int i=hd[x];i;i=e[i].nxt)if(e[i].v!=p)dfs(e[i].v,x);
17 int t=0,lst=x;for(int i=hd[x];i;i=e[i].nxt)if(e[i].v!=p)v[++t]=i;
18 if(!t)return;g[++cc]=(G){e[v[1]].v,x,e[v[1]].a,e[v[1]].b};
19 for(int i=2;i<t;i++)g[++cc]=(G){lst,++n,0,0},g[++cc]=(G){e[v[i]].v,lst=n,e[v[i]].a,e[v[i]].b};
20 if(t>1)g[++cc]=(G){e[v[t]].v,lst,e[v[t]].a,e[v[t]].b};
21 }
22 void gtrt(int x,int p){sz[x]=1;for(int i=hd[x],y,t;i;i=e[i].nxt)if((y=e[i].v)!=p&&!vis[i]){gtrt(y,x);sz[x]+=sz[y];if((t=max(sz[y],S-sz[y]))<res)res=t,rt=i;}}
23 void gt(int x,int p,int t,st*s,int&tp,ll s1,ll s2){if(t)s[++tp]=(st){s1,s2};for(int i=hd[x],y;i;i=e[i].nxt)if((y=e[i].v)!=p&&!vis[i])gt(y,x,e[i].a+e[i].b,s,tp,s1+e[i].a,s2+e[i].b);}
24 inline void build(st*s,int&tp,double*S,int f)
25 {
26 int t=0;sort(s+1,s+tp+1);
27 for(int i=1;i<=tp;i++)if(i==1||s[i].k!=s[i-1].k){while(t>1&&chk(s[t-1],s[t],s[i]))t--;s[++t]=s[i];}
28 tp=t;if(f)for(int i=1;i<=tp;i++)if(i<tp)S[i]=1.0*(s[i].b-s[i+1].b)/(s[i+1].k-s[i].k);
29 }
30 void sol(int x)
31 {
32 if(sz[x]==1)return;S=sz[x];res=1<<30;gtrt(x,0);
33 int A=e[rt].v,B=e[rt^1].v;vis[rt]=vis[rt^1]=1;s1=s2=0;
34 if(sz[A]>sz[B])sz[A]=S-sz[B];else sz[B]=S-sz[A];
35 gt(A,B,1,p,s1,e[rt].a,e[rt].b);gt(B,A,1,q,s2,0,0);build(p,s1,P,1);build(q,s2,Q,1);
36 for(int i=1,j=1;i<=s1;i++)
37 {
38 while(true)
39 {
40 r[++cnt]=(st){p[i].k+q[j].k,p[i].b+q[j].b};
41 if(j==s2||(i<s1&&P[i]<Q[j]))break;j++;
42 }
43 }
44 sol(A);sol(B);
45 }
46 int main()
47 {
48 int T,Cs=0;scanf("%d",&T);
49 while(T--)
50 {
51 scanf("%d%d",&n,&m);tt=0;cc=0;cnt=0;
52 for(int i=1,u,v,w;i<n;i++)scanf("%d%d%d",&u,&v,&w),add(u,v,1,w);
53 dfs(1,0);tt=1;for(int i=1;i<=n;i++)hd[i]=0;
54 for(int i=1;i<=cc;i++)add(g[i].u,g[i].v,g[i].a,g[i].b);
55 sz[1]=n;sol(1);build(r,cnt,P,0);
56 for(int i=1;i<=n;i++)hd[i]=0;
57 for(int i=1;i<=m;i++)scanf("%lld",&W[i].w),W[i].i=i;
58 sort(W+1,W+m+1,cmp);
59 for(int i=1,j=1;i<=m;i++)
60 {
61 while(j<cnt&&r[j].k*W[i].w+r[j].b<r[j+1].k*W[i].w+r[j+1].b)j++;
62 ans[W[i].i]=r[j].k*W[i].w+r[j].b;
63 }
64 printf("Case #%d:\n",++Cs);
65 for(int i=1;i<=m;i++)printf("%lld\n",ans[i]);
66 }
67 return 0;
68 }
gym101955EThe Kouga Ninja Scrolls
1 #include<bits/stdc++.h>
2 using namespace std;
3 #define ll long long
4 const int N=100005;
5 const ll inf=1ll<<50;
6 int n,m,ac[N];ll ax[N],ay[N];
7 struct st
8 {
9 ll f,s,c;
10 inline ll operator+(const st&b){return c==b.c?max(f+b.s,s+b.f):f+b.f;}
11 inline st operator*(const st&b){return (c==b.c)?(st){max(f,b.f),max(s,b.s),c}:(f<b.f?(st){b.f,max(f,b.s),b.c}:(st){f,max(s,b.f),c});}
12 inline void stv(ll x,int w){f=x;s=-inf;c=w;}
13 };
14 struct D
15 {
16 st a[4];
17 inline D operator+(const D&b){D r;for(int i=0;i<4;i++)r.a[i]=a[i]*b.a[i];return r;}
18 inline void stv(int l){for(int i=0;i<4;i++)a[i].stv(ax[l]*(i&1?1:-1)+ay[l]*(i&2?1:-1),ac[l]);}
19 inline ll calc(){ll r=0;for(int i=0;i<4;i++)r=max(r,a[i]+a[3^i]);return r;}
20 };
21 struct tr{int l,r,m;D s;}T[N<<2];
22 inline void up(int x){T[x].s=T[x<<1].s+T[x<<1|1].s;}
23 void build(int x,int l,int r)
24 {
25 T[x].l=l;T[x].r=r;T[x].m=l+r>>1;if(l==r){T[x].s.stv(l);return;}
26 build(x<<1,l,T[x].m);build(x<<1|1,T[x].m+1,r);up(x);
27 }
28 void upd(int x,int p){if(T[x].l==T[x].r){T[x].s.stv(T[x].l);return;}p<=T[x].m?upd(x<<1,p):upd(x<<1|1,p);up(x);}
29 D qry(int x,int l,int r)
30 {
31 if(l<=T[x].l&&T[x].r<=r)return T[x].s;
32 if(l>T[x].m)return qry(x<<1|1,l,r);
33 if(r<=T[x].m)return qry(x<<1,l,r);
34 return qry(x<<1,l,r)+qry(x<<1|1,l,r);
35 }
36 int main()
37 {
38 int T,Cs=0;scanf("%d",&T);
39 while(T--)
40 {
41 printf("Case #%d:\n",++Cs);scanf("%d%d",&n,&m);
42 for(int i=1;i<=n;i++)scanf("%lld%lld%d",&ax[i],&ay[i],&ac[i]);
43 build(1,1,n);
44 while(m--)
45 {
46 int o,k,x,y;scanf("%d%d%d",&o,&k,&x);
47 if(o==1)scanf("%d",&y),ax[k]+=x,ay[k]+=y;
48 if(o==2)ac[k]=x;
49 if(o==3)printf("%lld\n",max(0ll,qry(1,k,x).calc()));
50 else upd(1,k);
51 }
52 }
53 return 0;
54 }
gym101955FCounting Sheep in Ami Dongsuo
1 #include<bits/stdc++.h> 2 using namespace std; 3 const int N=10005,M=1205,P=1e9+7; 4 int inv[M],ifac[M],n,m,w,to[N*3],nxt[N*3],hd[N],d[N],Q[N],pw[M][M],f[N][3][M],tot[M],tmp[M],ans[M]; 5 int main() 6 { 7 inv[1]=ifac[0]=1; 8 for(int i=2;i<M;i++)inv[i]=1ll*inv[P%i]*(P-P/i)%P; 9 for(int i=1;i<M;i++)ifac[i]=1ll*ifac[i-1]*inv[i]%P; 10 for(int i=1;i<M;i++)for(int j=pw[i][0]=1;j<M;j++)pw[i][j]=1ll*pw[i][j-1]*i%P; 11 int T,Cs=0;scanf("%d",&T); 12 while(T--) 13 { 14 scanf("%d%d%d",&n,&m,&w);w*=3; 15 for(int i=1;i<=n;i++) 16 { 17 int v;scanf("%d",&v);hd[i]=d[i]=0; 18 for(int j=0;j<3;j++)for(int k=1;k<=w;k++)f[i][j][k]=pw[k][v*(j+1)]; 19 } 20 for(int i=1,u,v;i<=m;i++){scanf("%d%d",&u,&v);to[i]=v;nxt[i]=hd[u];hd[u]=i;d[v]++;} 21 int L=0,R=0;for(int i=1;i<=n;i++)if(!d[i])Q[++R]=i; 22 while(L<R)for(int x=Q[++L],i=hd[x];i;i=nxt[i])if(!--d[to[i]])Q[++R]=to[i]; 23 memset(tot,0,sizeof(tot)); 24 for(int i=n;i;i--) 25 { 26 int x=Q[i]; 27 for(int t=hd[x];t;t=nxt[t]) 28 { 29 int y=to[t]; 30 for(int j=0;j<3;j++)for(int k=0;k<=w;k++)f[x][j][k]=(f[x][j][k]+f[y][j][k])%P; 31 } 32 for(int j=0;j<=w;j++) 33 { 34 int res=1ll*f[x][0][j]*f[x][0][j]%P*f[x][0][j]%P; 35 res=(res-3ll*f[x][0][j]*f[x][1][j]%P+P)%P; 36 res=(res+2ll*f[x][2][j])%P;tot[j]=(tot[j]+res)%P; 37 } 38 } 39 for(int i=1;i<=w;i++)tot[i]=1ll*tot[i]*inv[6]%P; 40 memset(tmp,0,sizeof(tmp));memset(ans,0,sizeof(ans));tmp[1]=1; 41 for(int i=1;i<=w;i++)for(int j=i+1;j;j--)tmp[j]=(1ll*tmp[j]*(P-i)+tmp[j-1])%P; 42 for(int i=1;i<=w;i++) 43 { 44 for(int j=1;j<=w;j++)tmp[j]=1ll*(tmp[j]-tmp[j-1]+P)*(P-inv[i])%P; 45 int c=1ll*tot[i]*ifac[i]%P*ifac[w-i]%P*(w-i&1?P-1:1)%P; 46 for(int j=1;j<=w;j++)ans[j]=(ans[j]+1ll*tmp[j]*c)%P; 47 for(int j=w;j;j--)tmp[j]=(1ll*tmp[j]*(P-i)+tmp[j-1])%P; 48 } 49 printf("Case #%d:",++Cs); 50 for(int i=1;i<=w;i++)printf(" %d",ans[i]);puts(""); 51 } 52 return 0; 53 }
gym101955GBest ACMer Solves the Hardest Problem
1 #include<bits/stdc++.h>
2 using namespace std;
3 const int N=13000005,M=6005;
4 int n,m,px[200005],py[200005],w[M][M];
5 vector<pair<int,int> >g[N];
6 inline void chk(int x,int y,int v){if(x>=1&&x<=6000&&y>=1&&y<=6000&&w[x][y])w[x][y]+=v;}
7 inline void qry(int x,int y,ll&v){if(x>=1&&x<=6000&&y>=1&&y<=6000&&w[x][y])v+=w[x][y];}
8 int main()
9 {
10 for(int i=0;i<=6000;i++)for(int j=0;j<=6000;j++)if(i*i+j*j<=10000000)g[i*i+j*j].push_back(make_pair(i,j));else break;
11 int T,Cs=0;scanf("%d",&T);
12 while(T--)
13 {
14 printf("Case #%d:\n",++Cs);
15 scanf("%d%d",&n,&m);ll ans=0;
16 for(int i=1;i<=n;i++)scanf("%d%d",&px[i],&py[i]),scanf("%d",&w[px[i]][py[i]]);
17 while(m--)
18 {
19 int o,x,y;scanf("%d%d%d",&o,&x,&y);
20 x=(x+ans)%6000+1;y=(y+ans)%6000+1;
21 if(o==1){scanf("%d",&w[x][y]);n++;px[n]=x;py[n]=y;}
22 else if(o==2)w[x][y]=0;
23 else if(o==3)
24 {
25 int d,v;scanf("%d%d",&d,&v);
26 for(int i=0;i<g[d].size();i++)
27 {
28 int dx=g[d][i].first,dy=g[d][i].second;
29 if(!dx&&!dy)chk(x,y,v);
30 else if(!dx)chk(x,y+dy,v),chk(x,y-dy,v);
31 else if(!dy)chk(x+dx,y,v),chk(x-dx,y,v);
32 else chk(x+dx,y+dy,v),chk(x+dx,y-dy,v),chk(x-dx,y+dy,v),chk(x-dx,y-dy,v);
33 }
34 }
35 else
36 {
37 int d;scanf("%d",&d);ans=0;
38 for(int i=0;i<g[d].size();i++)
39 {
40 int dx=g[d][i].first,dy=g[d][i].second;
41 if(!dx&&!dy)qry(x,y,ans);
42 else if(!dx)qry(x,y+dy,ans),qry(x,y-dy,ans);
43 else if(!dy)qry(x+dx,y,ans),qry(x-dx,y,ans);
44 else qry(x+dx,y+dy,ans),qry(x+dx,y-dy,ans),qry(x-dx,y+dy,ans),qry(x-dx,y-dy,ans);
45 }
46 printf("%lld\n",ans);
47 }
48 }
49 for(int i=1;i<=n;i++)w[px[i]][py[i]]=0;
50 }
51 return 0;
52 }
gym101955HRainbow Graph
咕咕咕
gym101955IDistance Between Sweethearts
1 #include<bits/stdc++.h>
2 using namespace std;
3 #define ll long long
4 const int N=2049;
5 int mx,sz,p=0,q=1,A[3],B[3],f[3][N][N];ll g[2][N];
6 inline void fwt(int n,int*a,int f){int w=f==1?0:1;for(int t=1;t<n;t<<=1)for(int i=0;i<n;i+=(t<<1))for(int j=0;j<t;j++){int u=a[i+j],v=a[i+j+t];a[i+j]=(u+v)>>w;a[i+j+t]=(u-v)>>w;}}
7 inline void fwt(int n,ll*a,int f){int w=f==1?0:1;for(int t=1;t<n;t<<=1)for(int i=0;i<n;i+=(t<<1))for(int j=0;j<t;j++){ll u=a[i+j],v=a[i+j+t];a[i+j]=(u+v)>>w;a[i+j+t]=(u-v)>>w;}}
8 int main()
9 {
10 int T,Cs=0;scanf("%d",&T);
11 while(T--)
12 {
13 mx=0;sz=1;memset(f,0,sizeof(f));memset(g[p],0,sizeof(g[0]));
14 for(int i=0;i<3;i++)scanf("%d",&A[i]);
15 for(int i=0;i<3;i++)scanf("%d",&B[i]);
16 for(int i=0;i<3;i++)
17 {
18 mx=max(mx,max(A[i],B[i]));for(;sz<=mx;sz<<=1);
19 for(int x=0;x<=A[i];x++)for(int y=0;y<=B[i];y++)f[i][abs(x-y)][x^y]++;
20 }
21 ll ans=0;
22 for(int i=0;i<=mx;i++)
23 {
24 for(int j=0;j<3;j++)
25 {
26 fwt(sz,f[j][i],1);
27 if(i)for(int k=0;k<sz;k++)f[j][i][k]+=f[j][i-1][k];
28 }
29 swap(p,q);
30 for(int j=0;j<sz;j++)g[p][j]=1ll*f[0][i][j]*f[1][i][j]*f[2][i][j];
31 fwt(sz,g[p],-1);ll res=0;
32 for(int j=sz-1;j>0;j--){int k=i^j;res+=g[p][k]-g[q][k];ans+=res;}
33 }
34 printf("Case #%d: %llu\n",++Cs,ans);
35 }
36 return 0;
37 }
gym101955JHow Much Memory Your Code Is Using?
1 #include<bits/stdc++.h>
2 using namespace std;
3 int main()
4 {
5 int T,Cs=0;scanf("%d",&T);
6 while(T--)
7 {
8 int n,ans=0,c;cin>>n;string s;getline(cin,s);
9 for(int i=0;i<n;i++)
10 {
11 string s;int t;getline(cin,s);string str=s;
12 if(s[0]=='b'||s[0]=='c')t=1;
13 else if(s[0]=='i'||s[0]=='f')t=4;
14 else if(s[0]=='d')t=8;else if(s[0]=='_')t=16;
15 else if(s[0]=='l'){if(s[5]=='l')t=8;else if(s[5]=='d')t=16;}
16 int p1=str.find("[");c=0;
17 if(p1!=-1){int p2=str.find("]");for(int i=p1+1;i<p2;i++)c=c*10+s[i]-'0';}
18 if(c)t*=c;ans+=t;
19 }
20 ans=ans/1024+(ans%1024>0);
21 cout<<"Case #"<<++Cs<<": "<<ans<<endl;
22 }
23 return 0;
24 }
gym101955KLet the Flames Begin
1 #include<bits/stdc++.h>
2 using namespace std;
3 #define ll long long
4 int main()
5 {
6 int T,Cs=0;ll n,m,k;scanf("%d",&T);
7 while(T--)
8 {
9 scanf("%lld%lld%lld",&n,&m,&k);
10 if(k==1)printf("Case #%d: %lld\n",++Cs,m);
11 else
12 {
13 ll c=n-m+1,res=(k-1)%c;
14 while(c<n){ll d=min(max((c-res)/(k-1),1ll),n-c);c+=d;res=(res+d*K)%c;}
15 printf("Case #%d: %lld\n",++Cs,res+1);
16 }
17 }
18 return 0;
19 }
gym101955LMachining Disc Rotors
1 #include<bits/stdc++.h>
2 using namespace std;
3 const int N=205;
4 const double eps=1e-9,pi=acos(-1.0);
5 double ans,X[N],Y[N],L[N],R[N],a[N];
6 inline int sgn(double x){return x>eps?1:(x<-eps?-1:0);}
7 int main()
8 {
9 int T,Cs=0,n,cc,Ra;scanf("%d",&T);
10 while(T--)
11 {
12 scanf("%d%d",&n,&Ra);cc=0;ans=0;
13 for(int i=1;i<=n;i++)
14 {
15 int x,y,r;scanf("%d%d%d",&x,&y,&r);int t=x*x+y*y;
16 if(t>=(r+Ra)*(r+Ra)||(Ra>r&&t<=(Ra-r)*(Ra-r)))continue;
17 double a1=atan2(y,x),a2=acos((Ra*Ra+t-r*r)/(2*Ra*sqrt(t)));
18 X[++cc]=Ra*cos(a1-a2);Y[cc]=Ra*sin(a1-a2);L[cc]=a1-a2;R[cc]=a1+a2;a[cc]=a1-a2;
19 X[++cc]=Ra*cos(a1+a2);Y[cc]=Ra*sin(a1+a2);a[cc]=a1+a2;
20 }
21 for(int i=1,j;i<=cc;i++)
22 {
23 for(j=1;j<=cc;j+=2)if((sgn(L[j]-a[i]-pi)<0&&sgn(R[j]-a[i]-pi)>0)||(sgn(L[j]-a[i]+pi)<0&&sgn(R[j]-a[i]+pi)>0))break;
24 if(j>cc)ans=Ra*2;
25 }
26 for(int i=1;i<=cc;i++)for(int j=i+1;j<=cc;j++)ans=max(ans,sqrt((X[i]-X[j])*(X[i]-X[j])+(Y[i]-Y[j])*(Y[i]-Y[j])));
27 printf("Case #%d: %.10f\n",++Cs,ans);
28 }
29 return 0;
30 }
gym101955MRenaissance Past in Nancy
1 #include<bits/stdc++.h>
2 using namespace std;
3 const int N=10005,M=1005,P=1e9+7;
4 int n,m,ans,f[N][M],g[N][M];
5 inline void ins(int*f,int x){for(int i=M-1;i>=x;i--)f[i]=(f[i]-f[i-x]+P)%P;}
6 inline void del(int*f,int x){for(int i=x;i<M;i++)f[i]=(f[i]+f[i-x])%P;}
7 int main()
8 {
9 int T,Cs=0;scanf("%d",&T);
10 while(T--)
11 {
12 printf("Case #%d:\n",++Cs);scanf("%d%d",&n,&m);ans=0;
13 memset(f[0],0,sizeof(f[0]));f[0][0]=1;
14 memset(g[0],0,sizeof(g[0]));g[0][0]=1;
15 for(int i=1;i<=n;i++)
16 {
17 int a,b;scanf("%d%d",&a,&b);a++;
18 memcpy(f[i],f[i-1],sizeof(f[i]));
19 memcpy(g[i],g[i-1],sizeof(g[i]));
20 del(f[i],b);ins(g[i],b);
21 if(a*b<M)ins(f[i],a*b),del(g[i],a*b);
22 }
23 for(int i=1;i<=n;i++)for(int j=1;j<M;j++)f[i][j]=(f[i][j]+f[i][j-1])%P;
24 while(m--)
25 {
26 int l,r,c;scanf("%d%d%d",&l,&r,&c);l=(l+ans)%n+1;r=(r+ans)%n+1;if(l>r)swap(l,r);l--;ans=0;
27 for(int i=0;i<=c;i++)ans=(ans+1ll*f[r][i]*g[l][c-i])%P;printf("%d\n",ans);
28 }
29 }
30 return 0;
31 }
loj#507. 「LibreOJ NOI Round #1」接竹竿
1 #include<bits/stdc++.h>
2 using namespace std;
3 #define ll long long
4 const int N=1000005;
5 ll n,k,a[N],s[N],f[N],g[N];
6 int main()
7 {
8 scanf("%lld%lld",&n,&k);
9 for(int i=1;i<=n;i++)scanf("%lld",&a[i]);
10 for(int i=1;i<=n;i++)scanf("%lld",&s[i]),s[i]+=s[i-1];
11 for(int i=n;i;i--)f[i]=max(f[i+1],g[a[i]]-s[i-1]),g[a[i]]=max(g[a[i]],s[i]+f[i+1]);
12 printf("%lld\n",f[1]);
13 return 0;
14 }
loj#508. 「LibreOJ NOI Round #1」失控的未来交通工具
1 #include<bits/stdc++.h>
2 using namespace std;
3 #define ll long long
4 const int N=1000005;
5 int n,m,q,f[N],d[N],g[N];
6 inline int rd()
7 {
8 int x=0;char c=getchar();while(!isdigit(c))c=getchar();
9 while(isdigit(c))x=x*10+c-48,c=getchar();return x;
10 }
11 inline int fnd(int x){if(f[x]==x)return x;fnd(f[x]);d[x]=(d[x]+d[f[x]])%m;return f[x]=f[f[x]];}
12 inline void exgcd(int a,int b,int&x,int&y){if(!b){x=1;y=0;}else {exgcd(b,a%b,y,x);y-=a/b*x;}}
13 inline int inv(int a,int b){int x,y;exgcd(a,b,x,y);return (x%b+b)%b;}
14 int main()
15 {
16 n=rd();m=rd();q=rd();
17 for(int i=1;i<=n;i++)f[i]=i,g[i]=m;
18 while(q--)
19 {
20 int o,u,v,x,y,z,w,t;o=rd();u=rd();v=rd();x=rd();
21 if(o==1)
22 {
23 if((y=fnd(u))^(z=fnd(v)))d[z]=((d[u]+d[v])%m+x)%m,g[y]=__gcd(__gcd(g[y],g[z]),x*2),f[z]=y;
24 else g[y]=__gcd(g[y],((d[u]+d[v])%m+x)%m);
25 }
26 else
27 {
28 y=rd();z=rd();if(fnd(u)!=fnd(v)){puts("0");continue;}
29 w=g[fnd(u)];t=__gcd(w,y);x=((d[u]+d[v])%m+x)%m;if(x%t){puts("0");continue;}
30 x/=t;y/=t;w/=t;printf("%d\n",(int)((z-1ll*x*(w-inv(y%w,w))%w+w-1)/w));
31 }
32 }
33 return 0;
34 }
loj#509. 「LibreOJ NOI Round #1」动态几何问题
1 #include<bits/stdc++.h>
2 using namespace std;
3 #define ll long long
4 const int N=130000000;
5 int cc,pr[10000005],g[N];char mu[N];short f[N];
6 bitset<N>vis;
7 inline ll sqr(ll n){return sqrt(n+0.5);}
8 inline ll qry(ll n)
9 {
10 if(n<N&&g[n])return g[n];
11 ll i=1,s=0;for(;i*i*i<=n;i++)s+=n/(i*i)*mu[i];
12 while(i*i<=n){ll j=sqr(n/(n/(i*i)));s+=n/(i*i)*(f[j]-f[i-1]);i=j+1;}return s;
13 }
14 inline void init(int n)
15 {
16 mu[1]=f[1]=g[1]=1;
17 for(int i=2;i<=n;i++)
18 {
19 if(!vis[i]){pr[cc++]=i;f[i]=-1;}
20 for(int j=0;j<cc;j++)
21 {
22 int t=pr[j];if(i*t>n)break;
23 vis[i*t]=1;if(i%t==0)break;
24 f[i*t]=-f[i];
25 }
26 mu[i]=f[i];g[i]=g[i-1]+!!f[i];f[i]+=f[i-1];
27 }
28 }
29 int main()
30 {
31 ll n,m,ans=0;scanf("%lld%lld",&n,&m);if(n>m)swap(n,m);init(sqr(n));
32 for(ll i=1,j,s,t;i<=n;i=j+1,s=t){ll x=sqr(n/i),y=sqr(m/i);j=min(n/(x*x),m/(y*y));t=qry(j);ans+=x*y*(t-s);}
33 printf("%lld\n",ans);return 0;
34 }