Codeforces Round #553 (Div. 2) Solution

Codeforces Round #553 (Div. 2) Solution

This morning opened a \ (\ RM vp \) , the first four questions is water, looked \ (\ rm E, F \ ) after me to write a \ (\ RM F \) , and then not tune out of blood loss ... After the game only to find \ (\ rm E \) is really water ...


Game Portal: https://codeforces.com/contest/1151

A. Maxim and Biology

https://codeforces.com/contest/1151/problem/A

Direct violence just fine.

#include<bits/stdc++.h>
using namespace std;

#define int long long 

void read(int &x) {
    x=0;int f=1;char ch=getchar();
    for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-f;
    for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';x*=f;
}
 
void print(int x) {
    if(x<0) putchar('-'),x=-x;
    if(!x) return ;print(x/10),putchar(x%10+48);
}
void write(int x) {if(!x) putchar('0');else print(x);putchar('\n');}

#define lf long double
#define ll long long 

#define pii pair<int,int >
#define vec vector<int >

#define pb push_back
#define mp make_pair
#define fr first
#define sc second

#define FOR(i,l,r) for(int i=l,i##_r=r;i<=i##_r;i++) 

const int maxn = 2e5+10;
const int inf = 1e9;
const lf eps = 1e-12;
const int mod = 1e9+7;

int n;
char s[maxn];

int get(int a,int b) {
    return min(abs(a-b),26-max(a,b)+min(a,b));
}

signed main() {
    read(n);scanf("%s",s+1);
    int ans=1e9;
    for(int i=1;i<=n-3;i++) ans=min(ans,get(s[i],'A')+get(s[i+1],'C')+get(s[i+2],'T')+get(s[i+3],'G'));
    write(ans);
    return 0;
}

B. Dima and a Bad XOR

https://codeforces.com/contest/1151/problem/B

This problem is very strange ... I just fucks ...

We consider each column random number out of a random ten thousand times, the probability of error tends to \ (0 \) a.

#include<bits/stdc++.h>
using namespace std;

#define int long long 

void read(int &x) {
    x=0;int f=1;char ch=getchar();
    for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-f;
    for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';x*=f;
}
 
void print(int x) {
    if(x<0) putchar('-'),x=-x;
    if(!x) return ;print(x/10),putchar(x%10+48);
}
void write(int x) {if(!x) putchar('0');else print(x);putchar('\n');}

#define lf long double
#define ll long long 

#define pii pair<int,int >
#define vec vector<int >

#define pb push_back
#define mp make_pair
#define fr first
#define sc second

#define FOR(i,l,r) for(int i=l,i##_r=r;i<=i##_r;i++) 

const int maxn = 2e5+10;
const int inf = 1e9;
const lf eps = 1e-12;
const int mod = 998244353;
const int inv2 = 499122177;

int n,m,a[505][505],t[505];

signed main() {
    srand(time(0));
    read(n),read(m);FOR(i,1,n) FOR(j,1,m) read(a[i][j]);
    for(int i=1;i<=20000;i++) {
        int res=0;
        for(int j=1;j<=n;j++) res^=a[j][t[j]=rand()%m+1];
        if(res) {
            puts("TAK");
            for(int k=1;k<=n;k++) printf("%d ",t[k]);
            puts("");return 0;
        }
    }puts("NIE");
    return 0;
}

C. Problem for Nazar

https://codeforces.com/contest/1151/problem/C

Multiplication, and then press the meaning of the questions simulation.

#include<bits/stdc++.h>
using namespace std;

#define int long long 

void read(int &x) {
    x=0;int f=1;char ch=getchar();
    for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-f;
    for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';x*=f;
}
 
void print(int x) {
    if(x<0) putchar('-'),x=-x;
    if(!x) return ;print(x/10),putchar(x%10+48);
}
void write(int x) {if(!x) putchar('0');else print(x);putchar('\n');}

#define lf long double
#define ll long long 

#define pii pair<int,int >
#define vec vector<int >

#define pb push_back
#define mp make_pair
#define fr first
#define sc second

#define FOR(i,l,r) for(int i=l,i##_r=r;i<=i##_r;i++) 

const int maxn = 2e5+10;
const int inf = 1e9;
const lf eps = 1e-12;
const int mod = 1e9+7;
const int inv2 = 5e8+4;

int a[2];

int sum(int x,int y,int t) {
    int qwe=(y-x+1)%mod;
    x<<=1,y<<=1;if(t) x--,y--;
    x%=mod,y%=mod;
    return 1ll*(x+y)*qwe%mod*inv2%mod;
}

int solve(int r) {
    int tot=0,now=1,kd=1;
    int ans=0;a[0]=a[1]=1;
    while(tot<r) {
        if(tot+now>r) ans+=sum(a[kd],a[kd]+r-now,kd);
        else ans+=sum(a[kd],a[kd]+now-1,kd);
        a[kd]+=now,tot+=now,now<<=1,kd^=1;
    }return (ans%mod+mod)%mod; 
}

signed main() {
    int l,r;read(l),read(r);
    write((solve(r)-solve(l-1)+mod)%mod);
    return 0;
}

D. Stas and the Queue at the Buffet

https://codeforces.com/contest/1151/problem/D

The formula is open:
\ [nb_i-a_i + J \ CDOT (a_i-B_i) \]
Therefore, according to the direct greedy \ (a_i-b_i \) sorting like.

#include<bits/stdc++.h>
using namespace std;

#define int long long 

void read(int &x) {
    x=0;int f=1;char ch=getchar();
    for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-f;
    for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';x*=f;
}
 
void print(int x) {
    if(x<0) putchar('-'),x=-x;
    if(!x) return ;print(x/10),putchar(x%10+48);
}
void write(int x) {if(!x) putchar('0');else print(x);putchar('\n');}

#define lf long double
#define ll long long 

#define pii pair<int,int >
#define vec vector<int >

#define pb push_back
#define mp make_pair
#define fr first
#define sc second

#define FOR(i,l,r) for(int i=l,i##_r=r;i<=i##_r;i++) 

const int maxn = 2e5+10;
const int inf = 1e9;
const lf eps = 1e-12;
const int mod = 998244353;
const int inv2 = 499122177;

int n,a[maxn],b[maxn],t[maxn];

signed main() {
    read(n);FOR(i,1,n) read(a[i]),read(b[i]),t[i]=a[i]-b[i];
    sort(t+1,t+n+1);int res=0;
    for(int i=n;i;i--) res+=t[n-i+1]*i;
    for(int i=1;i<=n;i++) res+=-1*a[i]+n*b[i];
    write(res);
    return 0;
}

E. Number of Components

https://codeforces.com/contest/1151/problem/E

Noting forest communication block number \ (= \) Points \ (- \) number of sides.

Enumeration of each point and each edge contributions on the line.

#include<bits/stdc++.h>
using namespace std;

#define int long long 

void read(int &x) {
    x=0;int f=1;char ch=getchar();
    for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-f;
    for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';x*=f;
}
 
#define ll long long

void print(ll x) {
    if(x<0) putchar('-'),x=-x;
    if(!x) return ;print(x/10),putchar(x%10+48);
}
void write(ll x) {if(!x) putchar('0');else print(x);putchar('\n');}

#define lf double

#define pii pair<int,int >
#define vec vector<int >

#define pb push_back
#define mp make_pair
#define fr first
#define sc second

#define FOR(i,l,r) for(int i=l,i##_r=r;i<=i##_r;i++) 

const int maxn = 2e5+10;
const int inf = 1e9;
const lf eps = 1e-8;

ll ans,s;
int n,a[maxn];

int c(int x) {return x*(x-1)/2+x;}

signed main() {
    read(n);FOR(i,1,n) read(a[i]),ans+=c(n)-c(n-a[i])-c(a[i]-1);
    FOR(i,1,n-1) {
        int l=min(a[i],a[i+1]),r=max(a[i],a[i+1]);
        ans-=1ll*l*(n-r+1);
    }write(ans);
    return 0;
}

F. Sonya and Informatics

https://codeforces.com/contest/1151/problem/F

This is a bit difficult in this unique title of ...Perhaps I was too dishes right

The final state must be noted that the foregoing \ (K \) a \ (0 \) , followed by all \ (1 \) , where \ (K \) is a start \ (0 \) number.

We consider a violent \ (dp \) , set \ (f [i] [j ] \) representation of a \ (i \) operations, before \ (k \) positions have \ (j \) a \ (0 \) of the total number of cases.

And then transferred to enumerate several situations:

  • Front (front means \ (K \) a white th position) \ ((0) \) a black and behind \ ((. 1) \) \ (\ RM the swap \) , then the number in front of the white \ ( -1 \) , the program number \ (I (NK- (Ki)) \) .
  • Front and rear black white change, the number of white \ (1 + \) , the program number and the same reason as above.
  • The same color change, the same number of white.
  • And in front of or behind the front and rear of the transducer change, the same number of white.

Such complexity is transferred \ (O (NK) \) , but \ (k \) too had not.

Noting \ (n-\) only \ (100 \) we can optimize the transfer matrix above, the complexity of the \ (O (n-^. 3 \ log K) \) .

#include<bits/stdc++.h>
using namespace std;

#define int long long 

void read(int &x) {
    x=0;int f=1;char ch=getchar();
    for(;!isdigit(ch);ch=getchar()) if(ch=='-') f=-f;
    for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';x*=f;
}
 
void print(int x) {
    if(x<0) putchar('-'),x=-x;
    if(!x) return ;print(x/10),putchar(x%10+48);
}
void write(int x) {if(!x) putchar('0');else print(x);putchar('\n');}

#define lf long double
#define ll long long 

#define pii pair<int,int >
#define vec vector<int >

#define pb push_back
#define mp make_pair
#define fr first
#define sc second

#define FOR(i,l,r) for(int i=l,i##_r=r;i<=i##_r;i++) 

const int maxn = 2e5+10;
const int inf = 1e9;
const lf eps = 1e-12;
const int mod = 1e9+7;

int add(int x,int y) {return x+y>=mod?x+y-mod:x+y;}
int del(int x,int y) {return x-y<0?x-y+mod:x-y;}
int mul(int x,int y) {return 1ll*x*y-1ll*x*y/mod*mod;}
void inc(int &x,int y) {x+=y;x%=mod;}

int n,k,a[maxn],res,c;

int qpow(int a,int x) {
    int res=1;
    for(;x;x>>=1,a=1ll*a*a%mod) if(x&1) res=1ll*res*a%mod;
    return res;
}

struct Matrix {
    int a,b,r[110][110];

    Matrix () {a=b=0,memset(r,0,sizeof r);}
    
    Matrix operator * (const Matrix &t) const {
        Matrix res;res.a=a,res.b=t.b;
        for(int i=0;i<=a;i++)
            for(int j=0;j<=t.b;j++)
                for(int k=0;k<=b;k++)
                    res.r[i][j]=add(res.r[i][j],mul(r[i][k],t.r[k][j]));
        return res;
    }
}tr,ans;

signed main() {
    read(n),read(k);
    for(int i=1;i<=n;i++) read(a[i]),res+=!(a[i]&1);
    for(int i=1;i<=res;i++) c+=!a[i];
    ans.a=0,ans.b=res;ans.r[0][c]=1;tr.a=tr.b=res;
    for(int i=0;i<=res;i++) {
        int lw=i,lb=res-i,rw=res-lw,rb=n-res-lb;
        if(i!=res) inc(tr.r[i][i+1],lb*rw%mod);
        if(i!=0) inc(tr.r[i][i-1],lw*rb%mod);
        inc(tr.r[i][i],(res*(res-1)+(n-res)*(n-res-1))%mod*qpow(2,mod-2)%mod);
        inc(tr.r[i][i],(lw*lb+rw*rb)%mod);
    }
    int x=k,f=0;
    for(;x;x>>=1,tr=tr*tr) if(x&1) ans=ans*tr;
    FOR(i,0,res) f=(f+ans.r[0][i])%mod;write(ans.r[0][res]*qpow(f,mod-2)%mod);
    return 0;
}

Guess you like

Origin www.cnblogs.com/hbyer/p/11025016.html