2018 Multi-University Training Contest 1 hdu搬运工

2018-07-26  00:26:29http://acm.hdu.edu.cn/contests/contest_show.php?cid=802

被三整除一定是三个数相等,其次是被2整除且被四整除的数可取

s=2x1;

s=4x2;

 

s=x1+2x2

A题

// A
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const ll mod=1000000007;
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
// head
 
int n,_;
int main() {
    for (scanf("%d",&_);_;_--) {
        scanf("%d",&n);
        if (n%3==0) printf("%lld\n",1ll*n*n*n/27);
        else if (n%4==0) printf("%lld\n",1ll*n*n*n/32);
        else puts("-1");
    }
View Code

 

  B 题

 

 1 // B
 2 #include <stdio.h>
 3 #include <string.h>
 4 #include <stdlib.h>
 5 #include <iostream>
 6 #include <algorithm>
 7 using namespace std;
 8 const int MAXN = 101010;
 9 const int MAXS = 101010;
10 int n;
11 class Str
12 {
13 public:
14     int l, r, add;
15     bool operator <(const Str &b) const
16     {
17         if(l >= r && b.l < b.r)
18             return false;
19         if(l < r && b.l >= b.r)
20             return true;
21         if(l >= r && b.l >= b.r)
22             return r > b.r;
23         return l < b.l;
24     }
25 }a[MAXN];
26 char s[MAXS];
27 void solve()
28 {
29     scanf("%d", &n);
30     for(int i = 1; i <= n; i++)
31     {
32         scanf("%s", s);
33         int len = strlen(s);
34         a[i].l = a[i].r = a[i].add = 0;
35         for(int j = 0; j < len; j++)
36         {
37             if(s[j] == '(')
38                 a[i].r++;
39             else
40             {
41                 if(a[i].r > 0)
42                     a[i].r--, a[i].add++;
43                 else
44                     a[i].l++;
45             }
46         }
47     }
48     sort(a + 1, a + n + 1);
49     int ans = 0;
50     int now = 0;
51     for(int i = 1; i <= n; i++)
52     {
53         if(a[i].l > now)
54             a[i].l = now;
55         ans += a[i].l + a[i].add;
56         now -= a[i].l;
57         now += a[i].r;
58     }
59     printf("%d\n", ans * 2);
60 }
61 int main()
62 {
63     int T;
64     scanf("%d", &T);
65     for(int t1 = 1; t1 <= T; t1++)
66         solve();
67     return 0;
68 }
View B Code

 

C题

 1 // C
 2 #include <bits/stdc++.h>
 3 using namespace std;
 4 #define rep(i,a,n) for (int i=a;i<n;i++)
 5 #define per(i,a,n) for (int i=n-1;i>=a;i--)
 6 #define pb push_back
 7 #define mp make_pair
 8 #define all(x) (x).begin(),(x).end()
 9 #define fi first
10 #define se second
11 #define SZ(x) ((int)(x).size())
12 typedef vector<int> VI;
13 typedef long long ll;
14 typedef pair<int,int> PII;
15 const ll mod=1000000007;
16 ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
17 ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
18 // head
19  
20 const int N=101000;
21 int _,n,x,y;
22 pair<PII,int> p[N];
23 int main() {
24     for (scanf("%d",&_);_;_--) {
25         scanf("%d",&n);
26         rep(i,0,3*n) {
27             scanf("%d%d",&x,&y);
28             p[i]=mp(mp(x,y),i);
29         }
30         sort(p,p+3*n);
31         rep(i,0,n) {
32             printf("%d %d %d\n",p[3*i].se+1,p[3*i+1].se+1,p[3*i+2].se+1);
33         }
34     }
35 }
View C Code

 

D题

 1 // D
 2 #include <bits/stdc++.h>
 3 using namespace std;
 4 #define rep(i,a,n) for (int i=a;i<n;i++)
 5 #define per(i,a,n) for (int i=n-1;i>=a;i--)
 6 #define pb push_back
 7 #define mp make_pair
 8 #define all(x) (x).begin(),(x).end()
 9 #define fi first
10 #define se second
11 #define SZ(x) ((int)(x).size())
12 typedef vector<int> VI;
13 typedef long long ll;
14 typedef pair<int,int> PII;
15 const ll mod=1000000007;
16 ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
17 ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
18 // head
19  
20 const int N=101000;
21 int _,n,m,pre[N],l,r,ret[N];
22 int main() {
23     for (scanf("%d",&_);_;_--) {
24         scanf("%d%d",&n,&m);
25         rep(i,1,n+1) pre[i]=i;
26         rep(i,0,m) {
27             scanf("%d%d",&l,&r);
28             pre[r]=min(pre[r],l);
29         }
30         per(i,1,n) pre[i]=min(pre[i],pre[i+1]);
31         int pl=1;
32         set<int> val;
33         rep(i,1,n+1) val.insert(i);
34         rep(i,1,n+1) {
35             while (pl<pre[i]) {
36                 val.insert(ret[pl]);
37                 pl++;
38             }
39             ret[i]=*val.begin();
40             val.erase(ret[i]);
41         }
42         rep(i,1,n+1) printf("%d%c",ret[i]," \n"[i==n]);
43     }
44 }
View D Code

 

E题

  1 // E
  2 #include <bits/stdc++.h>
  3 using namespace std;
  4 #define rep(i,a,n) for (int i=a;i<n;i++)
  5 #define per(i,a,n) for (int i=n-1;i>=a;i--)
  6 #define pb push_back
  7 #define mp make_pair
  8 #define all(x) (x).begin(),(x).end()
  9 #define fi first
 10 #define se second
 11 #define SZ(x) ((int)(x).size())
 12 typedef vector<int> VI;
 13 typedef long long ll;
 14 typedef pair<int,int> PII;
 15 const ll mod=1000000007;
 16 ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
 17 ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
 18 // head
 19  
 20 const int N=201000;
 21 struct tSPnode {
 22     int ty,u,v,w;
 23     ll dp[2][2];
 24     ll way[2][2];
 25     tSPnode *l,*r;
 26 }tpool[N*2],*tcur,*E[N];
 27 int dq[N],inq[N],_,n,m,u,v,w;
 28 map<int,tSPnode*> se[N];
 29 tSPnode* newtnode(int u,int v,int w=0) {
 30     tSPnode* p=tcur++;
 31     p->ty=-2; p->u=u; p->v=v; p->l=p->r=0; p->w=w;
 32     memset(p->dp,0xee,sizeof(p->dp));
 33     memset(p->way,0,sizeof(p->way));
 34     return p;
 35 }
 36 void insertSP(int u,int v,tSPnode *c) {
 37     if (se[u].count(v)) {
 38         tSPnode *p=newtnode(u,v);
 39         p->ty=-1; // par
 40         p->l=se[u][v];
 41         p->r=c;
 42         se[u][v]=se[v][u]=p;
 43     } else {
 44         se[u][v]=se[v][u]=c;
 45     }
 46 }
 47  
 48 void gao(tSPnode *Rt) {
 49     if (Rt->ty==-2) {
 50         Rt->dp[0][0]=0;
 51         Rt->way[0][0]=1;
 52         Rt->dp[1][1]=Rt->w;
 53         Rt->way[1][1]=1;
 54     } else if (Rt->ty==-1) {
 55         if (Rt->l->u!=Rt->u) swap(Rt->l->u,Rt->l->v);
 56         if (Rt->r->v!=Rt->v) swap(Rt->r->u,Rt->r->v);
 57         assert(Rt->l->u==Rt->u);
 58         assert(Rt->r->u==Rt->u);
 59         assert(Rt->l->v==Rt->v);
 60         assert(Rt->r->v==Rt->v);
 61         gao(Rt->l); gao(Rt->r);
 62         rep(pl,0,3) rep(pr,0,3) {
 63             int fl=(pl==1),gl=(pl==2),fr=(pr==1),gr=(pr==2);
 64             if (Rt->dp[fl+gl][fr+gr]<Rt->l->dp[fl][fr]+Rt->r->dp[gl][gr]) {
 65                 Rt->dp[fl+gl][fr+gr]=Rt->l->dp[fl][fr]+Rt->r->dp[gl][gr];
 66                 Rt->way[fl+gl][fr+gr]=0;
 67             }
 68             if (Rt->dp[fl+gl][fr+gr]==Rt->l->dp[fl][fr]+Rt->r->dp[gl][gr]) {
 69                 (Rt->way[fl+gl][fr+gr]+=Rt->l->way[fl][fr]*Rt->r->way[gl][gr])%=mod;
 70             }
 71         }
 72     } else {
 73         if (Rt->l->u!=Rt->u&&Rt->l->v!=Rt->u) swap(Rt->l,Rt->r);
 74         if (Rt->l->u!=Rt->u) swap(Rt->l->u,Rt->l->v);
 75         if (Rt->r->v!=Rt->v) swap(Rt->r->u,Rt->r->v);
 76 //        printf("%d %d %d %d %d %d\n",Rt->u,Rt->v,Rt->l->u,Rt->l->v,Rt->r->u,Rt->r->v);
 77         assert(Rt->l->v==Rt->r->u);
 78         gao(Rt->l); gao(Rt->r);
 79         rep(fl,0,2) rep(gr,0,2) rep(fmid,0,3) {
 80             int gl=(fmid==1),fr=(fmid==2);
 81             if (Rt->dp[fl][gr]<Rt->l->dp[fl][fr]+Rt->r->dp[gl][gr]) {
 82                 Rt->dp[fl][gr]=Rt->l->dp[fl][fr]+Rt->r->dp[gl][gr];
 83                 Rt->way[fl][gr]=0;
 84             }
 85             if (Rt->dp[fl][gr]==Rt->l->dp[fl][fr]+Rt->r->dp[gl][gr]) {
 86                 (Rt->way[fl][gr]+=Rt->l->way[fl][fr]*Rt->r->way[gl][gr])%=mod;
 87             }
 88         }
 89     }
 90 //    printf("%d %d %d\n",Rt->u,Rt->v,Rt->ty);
 91 //    printf("%lld %lld %lld %lld\n",Rt->dp[0][0],Rt->dp[0][1],Rt->dp[1][0],Rt->dp[1][1]);
 92 }
 93 void parse() {
 94     rep(i,0,n) se[i].clear();
 95     rep(i,0,m) {
 96         int u=E[i]->u,v=E[i]->v;
 97         insertSP(u,v,E[i]);
 98     }
 99     int tot=0;
100     rep(u,0,n) {
101         inq[u]=0;
102         if (SZ(se[u])==2) dq[tot++]=u,inq[u]=1;
103     }
104     VI rv;
105     rep(i,0,tot) {
106         int u=dq[i];
107         if (SZ(se[u])!=2) { rv.pb(u); continue;}
108         auto it=se[u].begin(),nit=it;
109         ++it;
110         int p=it->fi,q=nit->fi;
111         tSPnode *s=it->se,*t=nit->se;
112         se[p].erase(u); se[q].erase(u);
113         tSPnode *r=newtnode(p,q); r->ty=u;
114         r->l=s, r->r=t;
115         insertSP(p,q,r);
116         if (!inq[p]&&SZ(se[p])==2) dq[tot++]=p,inq[p]=1;
117         if (!inq[q]&&SZ(se[q])==2) dq[tot++]=q,inq[q]=1;
118     }
119     rep(i,0,n) if (!inq[i]) rv.pb(i);
120     assert(SZ(rv)==2);
121     tSPnode *Rt=se[rv[0]][rv[1]];
122     gao(Rt);
123     ll ret=-1,way=0;
124     rep(i,0,2) rep(j,0,2) {
125         if (ret<Rt->dp[i][j]) {
126             ret=Rt->dp[i][j];
127             way=0;
128         }
129         if (Rt->dp[i][j]==ret) way=(way+Rt->way[i][j])%mod;
130     }
131     printf("%lld %lld\n",ret,way);
132 }
133  
134 void gao() {
135     scanf("%d%d",&n,&m);
136     tcur=tpool;
137     rep(i,0,m) {
138         scanf("%d%d%d",&u,&v,&w);
139         --u; --v;
140         E[i]=newtnode(u,v,w);
141     }
142     parse();
143 }
144  
145 int main() {
146     for (scanf("%d",&_);_;_--) gao();
147 }
148  
View E Code

 

F题

  1 // F
  2 #include <bits/stdc++.h>
  3 using namespace std;
  4 #define rep(i,a,n) for (int i=a;i<n;i++)
  5 #define per(i,a,n) for (int i=n-1;i>=a;i--)
  6 #define pb push_back
  7 #define mp make_pair
  8 #define all(x) (x).begin(),(x).end()
  9 #define fi first
 10 #define se second
 11 #define SZ(x) ((int)(x).size())
 12 typedef vector<int> VI;
 13 typedef long long ll;
 14 typedef pair<int,int> PII;
 15 const ll mod=1000000007;
 16 ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
 17 ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
 18 // head
 19  
 20 namespace polysum {
 21     const int D=101000;
 22     ll a[D],f[D],g[D],p[D],p1[D],p2[D],b[D],h[D][2],C[D];
 23     ll calcn(int d,ll *a,ll n) {
 24         if (n<=d) return a[n];
 25         p1[0]=p2[0]=1;
 26         rep(i,0,d+1) {
 27             ll t=(n-i+mod)%mod;
 28             p1[i+1]=p1[i]*t%mod;
 29         }
 30         rep(i,0,d+1) {
 31             ll t=(n-d+i+mod)%mod;
 32             p2[i+1]=p2[i]*t%mod;
 33         }
 34         ll ans=0;
 35         rep(i,0,d+1) {
 36             ll t=g[i]*g[d-i]%mod*p1[i]%mod*p2[d-i]%mod*a[i]%mod;
 37             if ((d-i)&1) ans=(ans-t+mod)%mod;
 38             else ans=(ans+t)%mod;
 39         }
 40         return ans;
 41     }
 42     void init(int M) {
 43         f[0]=f[1]=g[0]=g[1]=1;
 44         rep(i,2,M+5) f[i]=f[i-1]*i%mod;
 45         g[M+4]=powmod(f[M+4],mod-2);
 46         per(i,1,M+4) g[i]=g[i+1]*(i+1)%mod;
 47     }
 48     ll polysum(ll n,ll *a,ll m) { // a[0].. a[m] \sum_{i=0}^{n-1} a[i]
 49         a[m+1]=calcn(m,a,m+1);
 50         rep(i,1,m+2) a[i]=(a[i-1]+a[i])%mod;
 51         return calcn(m+1,a,n-1);
 52     }
 53     ll qpolysum(ll R,ll n,ll *a,ll m) { // a[0].. a[m] \sum_{i=0}^{n-1} a[i]*R^i
 54         if (R==1) return polysum(n,a,m);
 55         a[m+1]=calcn(m,a,m+1);
 56         ll r=powmod(R,mod-2),p3=0,p4=0,c,ans;
 57         h[0][0]=0;h[0][1]=1;
 58         rep(i,1,m+2) {
 59             h[i][0]=(h[i-1][0]+a[i-1])*r%mod;
 60             h[i][1]=h[i-1][1]*r%mod;
 61         }
 62         rep(i,0,m+2) {
 63             ll t=g[i]*g[m+1-i]%mod;
 64             if (i&1) p3=((p3-h[i][0]*t)%mod+mod)%mod,p4=((p4-h[i][1]*t)%mod+mod)%mod;
 65             else p3=(p3+h[i][0]*t)%mod,p4=(p4+h[i][1]*t)%mod;
 66         }
 67         c=powmod(p4,mod-2)*(mod-p3)%mod;
 68         rep(i,0,m+2) h[i][0]=(h[i][0]+h[i][1]*c)%mod;
 69         rep(i,0,m+2) C[i]=h[i][0];
 70         ans=(calcn(m,C,n)*powmod(R,n)-c)%mod;
 71         if (ans<0) ans+=mod;
 72         return ans;
 73     }
 74 }
 75  
 76 ll mul(ll a,ll b,ll c) {
 77     a%=mod; b%=mod; c%=mod;
 78     return a*b%mod*c%mod;
 79 }
 80  
 81 const int N=20100;
 82 int n,x,pos[N],_;
 83 pair<int,int> p[N];
 84 ll a,b,fl[N],fr[N],s[N];
 85 void solve() {
 86     scanf("%d%lld%lld",&n,&a,&b);
 87     rep(i,0,n) {
 88         scanf("%d",&x);
 89         p[i]=mp(x,i); 
 90     }
 91     ll ret=0;
 92     sort(p,p+n);
 93     rep(rem,0,n) {
 94         int t=0;
 95         rep(i,0,n) if (p[i].fi%n==rem) pos[t++]=p[i].se-(p[i].fi-rem);
 96         sort(pos,pos+t);
 97         rep(i,0,t) {
 98             fl[i]=(a-pos[i]+n-1)/n;
 99             fr[i]=(b-pos[i])/n;
100 //            printf("%d %lld %lld\n",i,fl[i],fr[i]);
101         }
102         rep(i,0,t) if (fl[i]<=fr[i]) {
103             rep(pl,0,5) {
104                 ll x=fl[i]+pl;
105                 s[pl]=mul(rem+x*n,(pos[i]+x*n)-a+1,b-(pos[i]+x*n)+1);
106                 if (pl>0) s[pl]=(s[pl]+s[pl-1])%mod;
107                 if (s[pl]<0) s[pl]+=mod;
108             }
109             ret=(ret+polysum::calcn(4,s,fr[i]-fl[i]))%mod;
110 //            printf("%d %d %lld\n",i,i,polysum::calcn(4,s,fr[i]-fl[i]));
111         }
112         rep(i,0,t) rep(j,i+1,t) if (max(fl[i],fl[j])<=min(fr[i],fr[j])) {
113             ll l=max(fl[i],fl[j]),r=min(fr[i],fr[j]);
114             rep(pl,0,5) {
115                 ll x=l+pl;
116                 s[pl]=mul(2*(rem+x*n),(pos[i]+x*n)-a+1,b-(pos[j]+x*n)+1);
117                 if (pl>0) s[pl]=(s[pl]+s[pl-1])%mod;
118                 if (s[pl]<0) s[pl]+=mod;
119             }
120             ret=(ret+polysum::calcn(4,s,r-l))%mod;
121 //            printf("%d %d %lld\n",i,j,polysum::calcn(4,s,r-l));
122         }
123     }
124     printf("%lld\n",ret);
125 }
126 int main() {
127     polysum::init(10);
128     for (scanf("%d",&_);_;_--) solve();
129 }
View F Code

 

G 题

 1 // G
 2 #include <bits/stdc++.h>
 3 using namespace std;
 4 #define rep(i,a,n) for (int i=a;i<n;i++)
 5 #define per(i,a,n) for (int i=n-1;i>=a;i--)
 6 #define pb push_back
 7 #define mp make_pair
 8 #define all(x) (x).begin(),(x).end()
 9 #define fi first
10 #define se second
11 #define SZ(x) ((int)(x).size())
12 typedef vector<int> VI;
13 typedef long long ll;
14 typedef pair<int,int> PII;
15 const ll mod=1000000007;
16 ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
17 ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
18 // head
19  
20 //int a[101000];
21 int _;
22 ll n;
23 ll mul(ll a,ll b) {
24     a%=mod; b%=mod;
25 //    printf("%lld %lld %lld %lld\n",a,b,c,d);
26     return a*b%mod;
27 }
28  
29  
30 ll mul(ll a,ll b,ll c) {
31     a%=mod; b%=mod; c%=mod;
32 //    printf("%lld %lld %lld %lld\n",a,b,c,d);
33     return a*b%mod*c%mod;
34 }
35 int main() {
36     for (scanf("%d",&_);_;_--) {
37         scanf("%lld",&n);
38         ll l=0,r=n;
39         while (l+1<r) {
40             ll md=(l+r)>>1;
41             if (2*md-__builtin_popcountll(md)<n) l=md; else r=md;
42         }
43         ll ans=(1+mul(n-1+n+1-2*r,r,(mod+1)/2))%mod;
44         rep(i,0,61) {
45             ll q=r/(1ll<<(i+1)),q2=r-(q<<(i+1));
46             if (q2<(1ll<<i)) q2=0; else q2-=(1ll<<i);
47             ans=(ans+mul(q,1ll<<i)+q2)%mod;
48         }
49 /*        rep(k,0,r) {
50             ans+=__builtin_popcount(k);
51         }*/
52         printf("%lld\n",ans);
53     }
54 }
View G Code

 

H题

 1 // H
 2  
 3 #include <bits/stdc++.h>
 4 using namespace std;
 5 #define rep(i,a,n) for (int i=a;i<n;i++)
 6 #define per(i,a,n) for (int i=n-1;i>=a;i--)
 7 #define pb push_back
 8 #define mp make_pair
 9 #define all(x) (x).begin(),(x).end()
10 #define fi first
11 #define se second
12 #define SZ(x) ((int)(x).size())
13 typedef vector<int> VI;
14 typedef long long ll;
15 typedef pair<int,int> PII;
16 const ll mod=1000000007;
17 ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
18 ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
19 // head
20  
21 const int N=1010000;
22 int stk[N],top,l[N],r[N],vis[N],n,x,_;
23 PII a[N];
24 ll inv[N],ret;
25  
26  
27 int dfs(int u) {
28     int s=1;
29     if (l[u]) s+=dfs(l[u]);
30     if (r[u]) s+=dfs(r[u]);
31     ret=ret*inv[s]%mod;
32     return s;
33 }
34 void build() {
35     int top=0;
36     rep(i,1,n+1) l[i]=0,r[i]=0,vis[i]=0;
37     rep(i,1,n+1) {
38         int k=top;
39         while (k>0&&a[stk[k-1]]>a[i]) --k;
40         if (k) r[stk[k-1]]=i;
41         if (k<top) l[i]=stk[k];
42         stk[k++]=i;
43         top=k;
44     }
45     rep(i,1,n+1) vis[l[i]]=vis[r[i]]=1;
46     int rt=0;
47     rep(i,1,n+1) if (vis[i]==0) rt=i;
48     dfs(rt);
49 }
50  
51 int main() {
52     inv[1]=1;
53     rep(i,2,1000001) inv[i]=inv[mod%i]*(mod-mod/i)%mod;
54     for (scanf("%d",&_);_;_--) {
55         scanf("%d",&n);
56         rep(i,1,n+1) {
57             scanf("%d",&x);
58             a[i]=mp(-x,i);
59         }
60         ret=inv[2]*n%mod;
61         build();
62         printf("%lld\n",ret);
63     }
64 }
View H Code

 

I题

  1 // I
  2 #include <bits/stdc++.h>
  3 using namespace std;
  4 #define rep(i,a,n) for (int i=a;i<n;i++)
  5 #define per(i,a,n) for (int i=n-1;i>=a;i--)
  6 #define pb push_back
  7 #define mp make_pair
  8 #define all(x) (x).begin(),(x).end()
  9 #define fi first
 10 #define se second
 11 #define SZ(x) ((int)(x).size())
 12 typedef vector<int> VI;
 13 typedef long long ll;
 14 typedef pair<int,int> PII;
 15 const ll mod1=1000000007;
 16 const ll mod2=1000000009;
 17 ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
 18 // head
 19  
 20 const int L=601000,N=101000;;
 21 char s[L];
 22 int ss[L],sa[L],rk[L],bp[N],mg[N];
 23  
 24 struct substr {
 25     int id,l,len;
 26 };
 27 typedef pair<int,int> hashv;
 28 vector<substr> g[N],sg[N];
 29 hashv base(13331,23333);
 30 hashv hv[L],pw[L];
 31  
 32 int n,m,u,v,_;
 33  
 34 hashv operator + (hashv a,hashv b) {
 35     int c1=a.fi+b.fi,c2=a.se+b.se;
 36     if (c1>=mod1) c1-=mod1;
 37     if (c2>=mod2) c2-=mod2;
 38     return mp(c1,c2);
 39 }
 40  
 41 hashv operator - (hashv a,hashv b) {
 42     int c1=a.fi-b.fi,c2=a.se-b.se;
 43     if (c1<0) c1+=mod1;
 44     if (c2<0) c2+=mod2;
 45     return mp(c1,c2);
 46 }
 47  
 48 hashv operator * (hashv a,hashv b) {
 49     return mp(1ll*a.fi*b.fi%mod1,1ll*a.se*b.se%mod2);
 50 }
 51  
 52 vector<substr> lyndon(char *s,int n,int id,int &mg){
 53     int k=0;
 54     mg=0;
 55     vector<substr> g;
 56     while (k<n) {
 57         int i=k+1,j=k+2;
 58         while (1) {
 59             if (j==n+1||s[j-1]<s[i-1]) {
 60                 int mul=0,b=k,l=j-i;
 61                 while (k<i) {
 62                     mul++;
 63                     k+=j-i;
 64                 }
 65                 g.pb((substr){id,b,l});
 66                 mg=max(mg,l);
 67                 break;
 68             } else {
 69                 if (s[j-1]>s[i-1]) {
 70                     i=k+1;
 71                 } else {
 72                     i=i+1;
 73                 }
 74                 j=j+1;
 75             }
 76         }
 77     }
 78     return g;
 79 }
 80  
 81 void buildSA(int *s,int *sa,int *rk,int n,int m=128) {
 82     static int X[L],Y[L],c[L];
 83     int *x=X,*y=Y;
 84     rep(i,0,m) c[i]=0;
 85     rep(i,0,n) c[x[i]=s[i]]++;
 86     rep(i,1,m) c[i]+=c[i-1];
 87     per(i,0,n) sa[--c[x[i]]]=i;
 88     for (int k=1;k<n;k<<=1) {
 89         int p=0;
 90         per(i,n-k,n) y[p++]=i;
 91         rep(i,0,n) if (sa[i]>=k) y[p++]=sa[i]-k;
 92         rep(i,0,m) c[i]=0;
 93         rep(i,0,n) c[x[y[i]]]++;
 94         rep(i,1,m) c[i]+=c[i-1];
 95         per(i,0,n) sa[--c[x[y[i]]]]=y[i];
 96         swap(x,y);
 97         p=1; x[sa[0]]=0; y[n]=-1;
 98         rep(i,1,n) x[sa[i]]=y[sa[i-1]]==y[sa[i]]&&
 99         y[sa[i-1]+k]==y[sa[i]+k]?p-1:p++;
100         if (p==n) break;
101         m=p;
102     }
103     rep(i,0,n)rk[sa[i]]=i;
104     rep(i,0,n) hv[i+1]=hv[i]*base+mp(s[i],s[i]);
105 }
106  
107 int pos(const substr &a) {
108     return bp[a.id]+a.l;
109 }
110  
111 hashv query(const substr &a) {
112     int posl=bp[a.id]+a.l,posr=bp[a.id]+a.l+a.len;
113     return hv[posr]-hv[posl]*pw[posr-posl];
114 }
115 int cmp(const substr& a,const substr& b) {
116     int l=min(a.len,b.len);
117     auto p1=query((substr){a.id,a.l,l}),p2=query((substr){b.id,b.l,l});
118     if (p1==p2) {
119         if (a.len<b.len) return -1;
120         else if (a.len==b.len) return 0;
121         else return 1;
122     }
123     return rk[pos(a)]<rk[pos(b)]?-1:1;
124 }
125  
126 substr tmpa[20],tmpb[20];
127 int tmp[20];
128 int cmp(substr* a,int p1,substr* b,int p2) {
129     int q1=0,q2=0;
130     while (q1<p1&&q2<p2) {
131         if (a[q1].len==b[q2].len) {
132             int r=cmp(a[q1],b[q2]);
133             if (r==0) q1++,q2++;
134             else return r;
135         } else if (a[q1].len<b[q2].len) {
136             substr c=b[q2];
137             c.len=a[q1].len;
138             int r=cmp(a[q1],c);
139             if (r!=0) return r;
140             else {
141                 b[q2].l+=a[q1].len;
142                 b[q2].len-=a[q1].len;
143                 q1++;
144             }
145         } else {
146             substr c=a[q1];
147             c.len=b[q2].len;
148             int r=cmp(c,b[q2]);
149             if (r!=0) return r;
150             else {
151                 a[q1].l+=b[q2].len;
152                 a[q1].len-=b[q2].len;
153                 q2++;
154             }            
155         }
156     }
157     if (q1==p1&&q2==p2) return 0;
158     if (q1==p1) return -1;
159     else return 1;
160 }
161  
162 substr t1[10],t2[10];
163 void solve() {
164     scanf("%d%d",&n,&m);
165     int cur=0;
166     rep(i,0,n) {
167         scanf("%s",s);
168         bp[i]=cur;
169         int len=strlen(s);
170         rep(j,0,len) ss[cur++]=s[j];
171         ss[cur++]=129+i;
172         g[i]=lyndon(s,len,i,mg[i]);
173         sg[i].clear();
174         for (auto p:g[i]) sg[i].pb((substr){i,p.l,len-p.l});
175     }
176     buildSA(ss,sa,rk,cur,130+n);
177     rep(i,0,m) {
178         scanf("%d%d",&u,&v);
179         --u; --v;
180         int r=cmp(g[u].back(),g[v][0]);
181         if (r>=0) {
182             printf("%d\n",max(mg[u],mg[v]));
183         } else {
184             int l=-1,r=SZ(g[u])-1;
185             substr sr=sg[v][0];
186             while (l+1<r) {
187                 int md=(l+r)>>1;
188                 t1[0]=sg[u][md]; t1[1]=sr;
189                 t2[0]=sg[u][md+1]; t2[1]=sr;
190                 if (cmp(t1,2,t2,2)==-1) r=md; else l=md;
191             }
192             int pl=0,pr=SZ(g[v]);
193             while (pl+1<pr) {
194                 int md=(pl+pr)>>1;
195                 t1[0]=sg[v][md];
196                 t2[0]=sg[u][r]; t2[1]=sr;
197                 if (cmp(t1,1,t2,2)==1) pl=md; else pr=md;
198             }
199             int ans=sg[u][r].len;
200             if (pl==SZ(sg[v])-1) ans+=sg[v][0].len;
201             else ans+=sg[v][pl+1].l;
202             printf("%d\n",max(ans,max(mg[u],mg[v])));
203         }
204     }
205 }
206 int main() {
207     pw[0]=mp(1,1);
208     rep(i,1,600001) pw[i]=pw[i-1]*base;
209     for (scanf("%d",&_);_;_--) solve();
210 }
View Code

 

J题

// J
#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(),(x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
typedef vector<int> VI;
typedef long long ll;
typedef pair<int,int> PII;
const ll mod=1000000007;
ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
// head
 
const int N=1010000;
int n,_;
bool f[N],g[N],preg[N],preg2[N];
char s[N];
int ret[N],ret2[N],cnt[N],cnt2[N];
void gao(bool *g,int *ret,int n) {
    rep(i,1,n+1) ret[i]=1<<30;
    int fo=n+1,lo=-1;
    rep(i,1,n+1) {
        if (g[i]) {
            if (fo==n+1) fo=i;
            lo=i;
        }
    }
    if (fo>lo) {
        rep(i,1,n+1) ret[i]=0;
        return;
    }
    rep(i,1,n+1) {
        preg[i]=preg[i-1]^g[i]^1;
        preg2[i]=preg2[i-1]^g[i];
        cnt[i]=cnt[i-1]+preg[i];
        cnt2[i]=cnt2[i-1]+preg2[i];
    }
    rep(i,0,lo+1) {
        if (i<=fo) {
            int fw=i,lw=lo;
            if (i==lo) {
                ret[i]=min(ret[i],3);
                continue;
            }
            int ans=lw-fw;
            if (preg[fw-1]) ans+=2*(cnt[lw-1]-cnt[fw-1]);
            else ans+=2*(lw-fw-cnt[lw-1]+cnt[fw-1]);
            if (preg[lw-1]^preg[fw-1]^1) ans--;
            ret[i]=min(ret[i],ans);
        } else {
            int fw=fo,lw=lo;
            int ans=i-fo+lw-fw;
            if (preg2[fw-1]) ans+=2*(cnt2[i-1]-cnt2[fw-1]);
            else ans+=2*(i-fw-cnt2[i-1]+cnt2[fw-1]);
            int x=preg2[i-1]^preg2[fw-1]^1;
            if (x==preg[i-1]) ans+=2*(cnt[lw-1]-cnt[i-1]);
            else ans+=2*(lw-i-cnt[lw-1]+cnt[i-1]);
            if (x^preg[i-1]^preg[lw-1]) ans--;
            ret[i]=min(ret[i],ans);
        }
    }
}
void solve() {
    scanf("%d",&n);
    scanf("%s",s+1);
    rep(i,1,n+1) g[i]=(s[i]=='1');
    gao(g,ret,n);
    reverse(g+1,g+n+1);
    gao(g,ret2,n);
    ll ans=0;
    rep(i,1,n+1) {
        ret[i]=min(ret[i],ret2[n+1-i]);
        ans=(ans+(ll)i*ret[i])%mod;
    }
    printf("%lld\n",ans);
}
int main() {
    for (scanf("%d",&_);_;_--) solve();
}
View Code

 

K题

 1 // K
 2 #include <bits/stdc++.h>
 3 using namespace std;
 4 #define rep(i,a,n) for (int i=a;i<n;i++)
 5 #define per(i,a,n) for (int i=n-1;i>=a;i--)
 6 #define pb push_back
 7 #define mp make_pair
 8 #define all(x) (x).begin(),(x).end()
 9 #define fi first
10 #define se second
11 #define SZ(x) ((int)(x).size())
12 typedef vector<int> VI;
13 typedef long long ll;
14 typedef pair<int,int> PII;
15 const ll mod=1000000007;
16 ll powmod(ll a,ll b) {ll res=1;a%=mod; assert(b>=0); for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;}
17 ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;}
18 // head
19  
20 double d;
21 int _,h,m,c,sign;
22 char s[20];
23 int main() {
24     for (scanf("%d",&_);_;_--) {
25         scanf("%d%d%s",&h,&m,s);
26         h=h*60+m;
27         sign=s[3]=='+'?1:-1;
28         sscanf(s+4,"%lf",&d);
29         c=(int)(d*10+0.1);
30         c=sign*c*6-8*60;
31         h+=c;
32         h%=(24*60);
33         if (h<0) h+=24*60;
34         printf("%02d:%02d\n",h/60,h%60);
35     }
36 }
View Code
 

 

 

posted @ 2018-07-23 20:47  163467  阅读(155)  评论(0编辑  收藏  举报