# Codeforces Round #549 (Div. 1) 题解

### A The Beatles

 1 #include<bits/stdc++.h>
2 #define rep(i,x,y) for (int i=(x);i<=(y);i++)
3 #define ll long long
4
5 using namespace std;
6
7 const int N=1e5+10;
8 int n,m,a,b; ll len,ans1,ans2;
9
10 ll gcd(ll a,ll b){return !b?a:gcd(b,a%b);}
11 inline ll get(ll x){
12     x<=0?x+=len:0;
13     x>len?x-=len:0;
14     return x;
15 }
16 inline void upd(ll x){
17     x=len/gcd(len,x);
18     ans1=min(ans1,x),ans2=max(ans2,x);
19 }
20 inline void chk(ll x,ll y){
21     if (x>y) swap(x,y);
22     upd(y-x),upd(x+len-y);
23 }
24
25 int main(){
26     scanf("%d%d%d%d",&n,&m,&a,&b); len=(ll)n*m;
27     ans1=len+1,ans2=0;
28     ll q=1;
29     rep (i,1,n){
30         ll p=(ll)(i-1)*m+1;
31         ll x=get(p-a),y=get(q-b); chk(x,y);
32         x=get(p-a),y=get(q+b); chk(x,y);
33         x=get(p+a),y=get(q-b); chk(x,y);
34         x=get(p+a),y=get(q+b); chk(x,y);
35     }
36     printf("%lld %lld\n",ans1,ans2);
37     return 0;
38 }
View Code

### B Lynyrd Skynyrd

 1 #include<bits/stdc++.h>
2 #define rep(i,x,y) for (int i=(x);i<=(y);i++)
3 #define Vi vector<int>
4
5 using namespace std;
6
7 const int N=2e5+10;
8 int n,m,Q,a[N],b[N],nxt[N],st[N][20],R[N],lg[N]; Vi p[N];
9
10 int get_min(int x,int y){
11     int t=lg[y-x+1];
12     return min(st[x][t],st[y-(1<<t)+1][t]);
13 }
14
15 int main(){
16     scanf("%d%d%d",&n,&m,&Q);
17     rep (i,1,n) scanf("%d",&a[i]);
18     rep (i,1,n) nxt[a[i]]=a[i%n+1];
19     rep (i,1,m) scanf("%d",&b[i]),p[b[i]].push_back(i);
20     rep (i,1,m){
21         int x=nxt[b[i]];
22         int t=upper_bound(p[x].begin(),p[x].end(),i)-p[x].begin();
23         if (t==(int)p[x].size()) st[i][0]=m+1; else st[i][0]=p[x][t];
24     }
25     st[m+1][0]=m+1;
26     rep (j,1,18) rep (i,1,m+1) st[i][j]=st[st[i][j-1]][j-1];
27     rep (i,1,m){
28         int x=i;
29         for (int j=18;~j;j--) if ((n-1)>>j&1) x=st[x][j];
30         R[i]=x;
31     }
32     memset(st,0,sizeof(st));
33     rep (i,1,m) st[i][0]=R[i],lg[i]=i==1?0:lg[i>>1]+1;
34     rep (j,1,18)
35         for (int i=1;i+(1<<(j-1))<=m;i++)
36             st[i][j]=min(st[i][j-1],st[i+(1<<(j-1))][j-1]);
37     while (Q--){
38         int l,r; scanf("%d%d",&l,&r);
39         if (get_min(l,r)<=r) putchar('1'); else putchar('0');
40     }
41     return 0;
42 }
View Code

### C U2

 1 #include<bits/stdc++.h>
2 #define rep(i,x,y) for (int i=(x);i<=(y);i++)
3 #define ll long long
4
5 using namespace std;
6
7 const int N=1e5+10;
8 int n,top;
9
10 #define vec poi
11 struct poi{
12     ll x,y;
13     poi(ll x=0,ll y=0):x(x),y(y){}
14     friend vec operator - (vec A,vec B){return vec(A.x-B.x,A.y-B.y);}
15     friend ll operator ^ (vec A,vec B){return A.x*B.y-A.y*B.x;}
16     friend bool operator < (poi x,poi y){return x.x!=y.x?x.x<y.x:x.y<y.y;}
17 }a[N],stk[N];
18
19 int main(){
20     scanf("%d",&n);
21     rep (i,1,n){
22         scanf("%lld%lld",&a[i].x,&a[i].y);
23         a[i].y-=a[i].x*a[i].x;
24
25     }
26     sort(a+1,a+1+n);
27     rep (i,1,n){
28         while (top>=2&&((a[i]-stk[top-1])^(stk[top]-stk[top-1]))<=0) top--;
29         while (top&&a[i].x==stk[top].x) top--;
30         stk[++top]=a[i];
31     }
32     printf("%d\n",top-1);
33     return 0;
34 }
View Code

### D Foreigner

 1 #include<bits/stdc++.h>
2 #define rep(i,x,y) for (int i=(x);i<=(y);i++)
3 #define ll long long
4
5 using namespace std;
6
7 const int N=1e5+10;
8 int n,f[N][13]; ll ans; char s[N];
9
10 int dfs(int x,int y){
11     if (x>n) return x-1;
12     if (~f[x][y]) return f[x][y];
13     if (s[x]>=y) return x-1;
14     return f[x][y]=dfs(x+1,(y*(y-1)/2+10+s[x]%10)%11);
15 }
16
17 int main(){
18     memset(f,-1,sizeof(f));
19     scanf("%s",s+1),n=strlen(s+1);
20     rep (i,1,n) s[i]-='0';
21     rep (i,1,n) if (s[i]) ans+=dfs(i+1,s[i])-i+1;
22     printf("%lld\n",ans);
23     return 0;
24 }
View Code

### E Pink Floyd

 1 #include<bits/stdc++.h>
2 #define rep(i,x,y) for (int i=(x);i<=(y);i++)
3 #define Vi vector<int>
4
5 using namespace std;
6
7 const int N=1e5+10;
9 int dfn[N],low[N],stk[N],bel[N],top,clk,scc,rd[N]; Vi V[N];
11 struct edge{int to,nxt;}e[N<<1];
12
15 }
16
17 void tarjan(int u){
18     dfn[u]=low[u]=++clk; stk[++top]=u;
20         if (v=e[i].to,!dfn[v]){
21             tarjan(v);
22             low[u]=min(low[u],low[v]);
23         } else if (!bel[v]) low[u]=min(low[u],dfn[v]);
24     if (dfn[u]!=low[u]) return;
25     ++scc;
26     while (stk[top]!=u) V[scc].push_back(stk[top]),bel[stk[top--]]=scc;
27     V[scc].push_back(stk[top]),bel[stk[top--]]=scc;
28 }
29
31     printf("? %d %d\n",x,y),fflush(stdout);
32     scanf("%d",&x); return x;
33 }
34
35 int main(){
36     scanf("%d%d",&n,&m);
38     rep (i,1,n) if (!dfn[i]) tarjan(i);
39     rep (i,1,m)
40         if (bel[x[i]]!=bel[y[i]]&&!S[bel[x[i]]].count(bel[y[i]])){
42             rd[bel[y[i]]]++,S[bel[x[i]]].insert(bel[y[i]]);
43         }
44     int tail=0;
45     rep (i,1,scc) if (!rd[i]) q[++tail]=i;
46     while (tail>1){
47         int x=V[q[1]].back(),y=V[q[2]].back();
49         x=q[2]; V[x].pop_back();
50         if (V[x].empty()){
51             swap(q[tail],q[2]),tail--;
58 }