Codeforces Round #520 (Div. 2)

Codeforces Round #520 (Div. 2)

https://codeforces.com/contest/1062

A

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define lson l,mid,rt<<1
 4 #define rson mid+1,r,rt<<1|1
 5 #define sqr(x) ((x)*(x))
 6 #define pb push_back
 7 #define eb emplace_back
 8 #define maxn 100005
 9 #define eps 1e-8
10 #define pi acos(-1.0)
11 #define rep(k,i,j) for(int k=i;k<j;k++)
12 typedef long long ll;
13 typedef pair<int,int> pii;
14 typedef pair<long long,int>pli;
15 typedef pair<int,char> pic;
16 typedef pair<pair<int,string>,pii> ppp;
17 typedef unsigned long long ull;
18 const long long mod=1e9+7;
19 /*#ifndef ONLINE_JUDGE
20         freopen("1.txt","r",stdin);
21 #endif */
22 
23 int a[105];
24 
25 int main(){
26     #ifndef ONLINE_JUDGE
27      //   freopen("1.txt","r",stdin);
28     #endif
29     int n;
30     cin>>n;
31     if(n==1) {
32         cout<<0<<endl;
33         return 0;
34     } 
35     for(int i=1;i<=n;i++) cin>>a[i];
36     a[0]=0,a[n+1]=1001;
37     int ans=0,Max=0;
38     for(int i=1;i<=n+1;i++){
39         if(a[i]-a[i-1]==1){
40             Max++;
41         }
42         else{
43             ans=max(ans,Max-1);
44             Max=0;
45         }
46         
47     }
48     ans=max(ans,Max-1);
49     cout<<ans<<endl;
50 }
View Code

 

B

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define lson l,mid,rt<<1
 4 #define rson mid+1,r,rt<<1|1
 5 #define sqr(x) ((x)*(x))
 6 #define pb push_back
 7 #define eb emplace_back
 8 #define maxn 100005
 9 #define eps 1e-8
10 #define pi acos(-1.0)
11 #define rep(k,i,j) for(int k=i;k<j;k++)
12 typedef long long ll;
13 typedef pair<int,int> pii;
14 typedef pair<long long,int>pli;
15 typedef pair<int,char> pic;
16 typedef pair<pair<int,string>,pii> ppp;
17 typedef unsigned long long ull;
18 const long long mod=1e9+7;
19 /*#ifndef ONLINE_JUDGE
20         freopen("1.txt","r",stdin);
21 #endif */
22 
23 int a[105];
24 
25 int main(){
26     #ifndef ONLINE_JUDGE
27      //   freopen("1.txt","r",stdin);
28     #endif
29     ll n;
30     cin>>n;
31     ll tmp=n;
32     ll num=1;
33     for(int i=2;i<=n;i++){
34         if(tmp%i==0){
35             num*=i;
36             while(tmp%i==0){
37                 tmp/=i;
38             }
39         }
40     }
41     ll ans=0;
42     tmp=num;
43     while(num%n){
44         num*=num;
45         ans++;
46     } 
47     cout<<tmp<<" "<<(ans+(num>n))<<endl;
48 }
View Code

 

C

 数论+推公式

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define lson l,mid,rt<<1
 4 #define rson mid+1,r,rt<<1|1
 5 #define sqr(x) ((x)*(x))
 6 #define pb push_back
 7 #define eb emplace_back
 8 #define maxn 100005
 9 #define eps 1e-8
10 #define pi acos(-1.0)
11 #define rep(k,i,j) for(int k=i;k<j;k++)
12 typedef long long ll;
13 typedef pair<int,int> pii;
14 typedef pair<long long,int>pli;
15 typedef pair<int,char> pic;
16 typedef pair<pair<int,string>,pii> ppp;
17 typedef unsigned long long ull;
18 const long long mod=1e9+7;
19 /*#ifndef ONLINE_JUDGE
20         freopen("1.txt","r",stdin);
21 #endif */
22 
23 string str;
24 ll sum[100005];
25 
26 ll pow_mul(ll a,ll b){
27     ll ans=1;
28     while(b){
29         if(b&1)
30             ans=ans*a%mod;
31         b>>=1;
32         a=a*a%mod;
33     }
34     return ans;
35 }
36 
37 
38 
39 int main(){
40     #ifndef ONLINE_JUDGE
41      //   freopen("1.txt","r",stdin);
42     #endif
43     int n,q;
44     cin>>n>>q;
45     cin>>str;
46     for(int i=0;i<n;i++){
47         sum[i+1]=sum[i]+str[i]-'0';
48     } 
49     int l,r;
50     while(q--){
51         cin>>l>>r;
52         ll ans=pow_mul(2,sum[r]-sum[l-1])-1;
53         if(ans==-1) ans+=mod;
54         ans=(ans+ans*(pow_mul(2,r-l+1+sum[l-1]-sum[r])-1+mod)%mod)%mod;
55         cout<<ans<<endl;
56     } 
57 }
View Code

 

D

数论

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define lson l,mid,rt<<1
 4 #define rson mid+1,r,rt<<1|1
 5 #define sqr(x) ((x)*(x))
 6 #define pb push_back
 7 #define eb emplace_back
 8 #define maxn 100005
 9 #define eps 1e-8
10 #define pi acos(-1.0)
11 #define rep(k,i,j) for(int k=i;k<j;k++)
12 typedef long long ll;
13 typedef pair<int,int> pii;
14 typedef pair<long long,int>pli;
15 typedef pair<int,char> pic;
16 typedef pair<pair<int,string>,pii> ppp;
17 typedef unsigned long long ull;
18 const long long mod=1e9+7;
19 /*#ifndef ONLINE_JUDGE
20         freopen("1.txt","r",stdin);
21 #endif */
22 
23 
24 
25 int main(){
26     #ifndef ONLINE_JUDGE
27      //   freopen("1.txt","r",stdin);
28     #endif
29     int n;
30     cin>>n;
31     ll ans=0;
32     for(int i=2;i<=n;i++){
33         for(int j=i+i;j<=n;j+=i){
34             ans+=j/i;
35         } 
36     }
37     cout<<ans*4<<endl;
38 }
View Code

 

E

lca+线段树+倍增

  1 #include<bits/stdc++.h>
  2 using namespace std;
  3 #define lson l,mid,rt<<1
  4 #define rson mid+1,r,rt<<1|1
  5 #define sqr(x) ((x)*(x))
  6 #define pb push_back
  7 #define eb emplace_back
  8 #define maxn 100005
  9 #define eps 1e-8
 10 #define pi acos(-1.0)
 11 #define rep(k,i,j) for(int k=i;k<j;k++)
 12 typedef long long ll;
 13 typedef pair<int,int> pii;
 14 typedef pair<long long,int>pli;
 15 typedef pair<int,char> pic;
 16 typedef pair<pair<int,string>,pii> ppp;
 17 typedef unsigned long long ull;
 18 const long long mod=1e9+7;
 19 /*#ifndef ONLINE_JUDGE
 20         freopen("1.txt","r",stdin);
 21 #endif */
 22 
 23 //int phi[maxn];
 24 
 25 vector<int>ve[maxn];
 26 
 27 int dep[maxn],parent[maxn][25],n,cnt;
 28 int Max[maxn<<2],Min[maxn<<2],id[maxn],s[maxn];
 29 
 30 void dfs(int now,int fa,int deep){
 31     dep[now]=deep;
 32     parent[now][0]=fa;
 33     s[now]=++cnt;
 34     id[cnt]=now;
 35     for(int i=0;i<ve[now].size();i++){
 36         dfs(ve[now][i],now,deep+1);
 37     }
 38 }
 39 
 40 void Init(){
 41     for(int j=1;j<19;j++){
 42         for(int i=1;i<=n;i++){
 43             parent[i][j]=parent[parent[i][j-1]][j-1];
 44         }
 45     }
 46 }
 47 
 48 int lca(int x,int y){
 49     if(dep[x]<dep[y]) swap(x,y);
 50     for(int i=18;i>=0;i--){
 51         if(dep[parent[x][i]]>=dep[y]){
 52             x=parent[x][i];
 53         }
 54     }
 55     if(x==y) return dep[x]-1;
 56     for(int i=18;i>=0;i--){
 57         if(parent[x][i]!=parent[y][i]){
 58             x=parent[x][i];
 59             y=parent[y][i];
 60         }
 61     }
 62     return dep[parent[x][0]]-1;
 63 }
 64 
 65 int query1(int L,int R,int l,int r,int rt){
 66     if(L<=l&&R>=r) return Max[rt];
 67     int mid=l+r>>1;
 68     int ans=0;
 69     if(L<=mid) ans=max(ans,query1(L,R,lson));
 70     if(R>mid) ans=max(ans,query1(L,R,rson));
 71     return ans;
 72 }
 73 
 74 int query2(int L,int R,int l,int r,int rt){
 75     if(L<=l&&R>=r) return Min[rt];
 76     int mid=l+r>>1;
 77     int ans=0x3f3f3f3f;
 78     if(L<=mid) ans=min(ans,query2(L,R,lson));
 79     if(R>mid) ans=min(ans,query2(L,R,rson));
 80     return ans;
 81 }
 82 
 83 void push_up(int rt){
 84     Max[rt]=max(Max[rt<<1],Max[rt<<1|1]);
 85     Min[rt]=min(Min[rt<<1],Min[rt<<1|1]);
 86 }
 87 
 88 void update(int L,int v,int l,int r,int rt){
 89     if(l==r){
 90         Max[rt]=Min[rt]=v;
 91         return;
 92     }
 93     int mid=l+r>>1;
 94     if(L<=mid) update(L,v,lson);
 95     else update(L,v,rson);
 96     push_up(rt);
 97 }
 98 
 99 int main(){
100     #ifndef ONLINE_JUDGE
101      //   freopen("1.txt","r",stdin);
102     #endif
103     std::ios::sync_with_stdio(false);
104     int q;
105     cin>>n>>q;
106     int x,y;
107     for(int i=2;i<=n;i++){
108         cin>>x;
109         ve[x].pb(i);
110     }
111     dfs(1,0,1);
112     Init();
113     for(int i=1;i<=n;i++){
114         update(i,s[i],1,n,1);
115     }
116     while(q--){
117         cin>>x>>y;
118         int min1=query2(x,y,1,n,1);
119         int max1=query1(x,y,1,n,1);
120         update(id[min1],0x3f3f3f3f,1,n,1);
121         int min2=query2(x,y,1,n,1);
122         update(id[min1],min1,1,n,1);
123         update(id[max1],0,1,n,1);
124         int max2=query1(x,y,1,n,1);
125         update(id[max1],max1,1,n,1);///?
126         int ans1=lca(id[min1],id[max2]),ans2=lca(id[min2],id[max1]);
127         if(ans1>=ans2){
128             cout<<id[max1]<<" "<<ans1<<endl;
129         }
130         else{
131             cout<<id[min1]<<" "<<ans2<<endl;
132         }
133     }
134 
135 }
View Code

 

F

拓扑排序

参考博客:https://www.cnblogs.com/yqgAKIOI/p/10012279.html

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 #define lson l,mid,rt<<1
 4 #define rson mid+1,r,rt<<1|1
 5 #define sqr(x) ((x)*(x))
 6 #define pb push_back
 7 #define eb emplace_back
 8 #define maxn 300005
 9 #define eps 1e-8
10 #define pi acos(-1.0)
11 #define rep(k,i,j) for(int k=i;k<j;k++)
12 typedef long long ll;
13 typedef pair<int,int> pii;
14 typedef pair<long long,int>pli;
15 typedef pair<int,char> pic;
16 typedef pair<pair<int,string>,pii> ppp;
17 typedef unsigned long long ull;
18 const long long mod=1e9+7;
19 /*#ifndef ONLINE_JUDGE
20         freopen("1.txt","r",stdin);
21 #endif */
22 
23 
24 
25 int n,m;
26 int a[maxn],b[maxn],d[maxn],cnt[maxn];
27 vector<int>ve[maxn];
28 
29 void topsort(int a[],int b[]){
30     for(int i=0;i<n;i++) ve[i].clear();
31     for(int i=0;i<m;i++){
32         ve[a[i]].pb(b[i]);
33         d[b[i]]++;
34     }
35     int num=n;
36     queue<int>Q;
37     for(int i=0;i<n;i++){
38         if(!d[i]){
39             Q.push(i);
40             num--;
41         }
42     } 
43     while(!Q.empty()){
44         int now=Q.front();
45         Q.pop();
46         if(Q.empty()) cnt[now]+=num;
47         else if(Q.size()==1){
48             int tmp=1;
49             for(int i=0,y=Q.front();i<ve[y].size();i++){
50                 tmp&=(d[ve[y][i]]>1);
51             }
52             cnt[now]+=num*tmp;
53         }    
54         for(int i=0;i<ve[now].size();i++)
55             if(!--d[ve[now][i]]){
56                 Q.push(ve[now][i]);
57                 num--;
58             }
59     }
60     
61 }
62 
63 int main(){
64     #ifndef ONLINE_JUDGE
65      //   freopen("1.txt","r",stdin);
66     #endif
67     std::ios::sync_with_stdio(false);
68     cin>>n>>m;
69     for(int i=0;i<m;i++){
70         cin>>a[i]>>b[i];
71         a[i]--,b[i]--;
72     }
73     topsort(a,b);
74     topsort(b,a);
75     int ans=0;
76     for(int i=0;i<n;i++)
77         ans+=(cnt[i]+2>=n);
78     cout<<ans<<endl;
79 
80 }
View Code

 

posted on 2019-03-17 13:47  Fighting_sh  阅读(252)  评论(0编辑  收藏  举报

导航