2020牛客多校第九场By Rynar

A.Groundhog and 2-Power Representation

赛中

s=str(input())
b=0
def dfs(l,r):
    if r-l==1:
        return ord(s[l])-ord('0')
    ans=0
    i=l
    while i<r:
        if (s[i]=='2'):
            if s[i+1]=='(':
                h=1
                for j in range(i+2,r):
                    if s[j]==')':
                        h-=1
                    elif s[j]=='(':
                        h+=1
                    if h==0:
                        ans+=1<<dfs(i+2,j)
                        i=j
                        break
            else:
                ans+=2
        i+=1
    return ans
lenth=len(s)
s+="sss"
print(dfs(0,lenth))

赛后

s=input()
ss=s.replace("(","**(")
ans=eval(ss)
print(ans)

B.Groundhog and Apple Tree

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+10;
#define int ll
typedef pair<int,int> pii;
int n,m;
int a[N];
vector<pii>v[N];
int dp[N][2];
void dfs(int x,int fa){
    dp[x][1]=a[x];
    priority_queue <pii,vector<pii>,greater<pii> > q;
    priority_queue <pii>q1;
    for (pii i:v[x]){
        if (i.first==fa)continue;
        dfs(i.first,x);
        dp[i.first][1]-=2*i.second;
        dp[i.first][0]+=i.second;
        dp[x][1]+=dp[i.first][1];
        if (dp[i.first][1]>=0){
            q.push({dp[i.first][0],dp[i.first][1]});
        }
        else{
            q1.push({dp[i.first][0]+dp[i.first][1],dp[i.first][0]});
        }
    }
    int r=a[x];
    int o=0;
    while (!q.empty()){
        pii t=q.top();q.pop();
        if (r<t.first)o+=t.first-r,r=t.first;
        r+=t.second;
    }
    while (!q1.empty()){
        pii t=q1.top();q1.pop();
        int e=t.first-t.second;
        if (r<t.second)o+=t.second-r,r=t.second;
        r+=e;
    }
    dp[x][0]=max(o,-dp[x][1]);
}
signed main(){
    int T,x,y,z;
    scanf("%lld",&T);
    while (T--){
        scanf("%lld",&n);
        for (int i=1;i<=n;i++){
            scanf("%lld",&a[i]);
            v[i].clear();
        }
        for (int i=1;i<n;i++){
            scanf("%lld%lld%lld",&x,&y,&z);
            v[x].push_back({y,z});v[y].push_back({x,z});
        }
        dfs(1,0);
        printf("%lld\n",dp[1][0]);
    }
    return 0;
}

C.Groundhog and Gaming Time

D.Groundhog and Golden Apple

E.Groundhog Chasing Death

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define int ll
const int N=32000+10;
const int mod=998244353;
int f1[N],f2[N];
int qpow(int x,int y){
    int ans=1;
    while (y){if (y&1)ans=ans*x%mod;y>>=1;x=x*x%mod;}
    return ans;
}
vector<int>v;
signed main(){
    int a,b,c,d,x,y;
    scanf("%lld%lld%lld%lld%lld%lld",&a,&b,&c,&d,&x,&y);
    for (int i=2;i*i<=x;i++){
        if (x%i==0){
            int cnt=0;
            while (x%i==0){cnt++;x/=i;}
            f1[i]=cnt;
        }
    }
    if (x<N)f1[x]++,x=1;
    for (int i=2;i*i<=y;i++){
        if(y%i==0){
            int cnt=0;
            while (y%i==0){cnt++;y/=i;}
            f2[i]=cnt;
            if (f1[i])v.push_back(i);
        }
    }
    if (y<N){
        if (f1[y])v.push_back(y);
        f2[y]++,y=1;
    }
    int ans=1;
    int r=mod-1;
    for (int i:v){
        int res=0;
        for (int j=a;j<=b;j++){
            int p=j*f1[i]/f2[i];
            if (p<c)res=(res+j*f1[i]*(d-c+1))%r;
            else if (p>=d)res=(res+f2[i]*(c+d)*(d-c+1)/2)%r;
            else res=(res+j*f1[i]*(d-p)+f2[i]*(p+c)*(p-c+1)/2)%r;
        }
        ans=ans*qpow(i,res)%mod;
    }
    if (x==y&&x!=1){
        int res=0;
        for (int j=a;j<=b;j++){
            if (j<c)res=(res+j*(d-c+1))%r;
            else if (j>=d)res=(res+(c+d)*(d-c+1)/2)%r;
            else res=(res+j*(d-j)+(j+c)*(j-c+1)/2)%r;
        }
        ans=ans*qpow(x,res)%mod;
    }
    printf("%lld\n",ans);
    return 0;
}

F.Groundhog Looking Dowdy

#include<bits/stdc++.h>
using namespace std;
const int N=2e6+10;
const int inf=0x3f3f3f3f;
int n,m,k;
struct node{
    int w,id;
}a[N];
bool cmp(node a,node b){
    return a.w<b.w;
}
queue<pair<int,int>>q;
int f[N];
signed main(){
    int T,x;
    scanf("%d%d",&n,&m);
    int cnt=0;
    for (int i=1;i<=n;i++){
        scanf("%d",&k);
        for (int j=1;j<=k;j++){
            scanf("%d",&x);
            a[++cnt].w=x;a[cnt].id=i;
        }
    }
    sort(a+1,a+1+cnt,cmp);
    int r=0,mx=inf;
    for (int i=1;i<=cnt;i++){
        if (!f[a[i].id])r++;
        while(r>=m){
            int t=q.front().second;
            if (f[t]==1&&r==m)break;
            f[t]--;
            if (!f[t])r--;
            q.pop();
        }
        if (r==m)mx=min(mx,a[i].w-q.front().first);
        q.push({a[i].w,a[i].id});
        f[a[i].id]++;
    }
    printf("%d\n",mx);
    return 0;
}

G.Groundhog Playing Scissors

H.Groundhog Speaking Groundhogish

I.The Crime-solving Plan of Groundhog

#include <bits/stdc++.h>
using namespace std;
int a[100000+10];
int main(){
    int T,n;
    scanf("%d",&T);
    while (T--){
        scanf("%d",&n);
        for (int i=0;i<n;i++)scanf("%d",&a[i]);
        sort(a,a+n);
        int t=0;
        for (int i=0;i<n;i++){
            if (a[i]){
                swap(a[t],a[i]);
                t++;
            }
            if (t==2)break;
        }
        for (int i=1;i<n;i++)a[i]*=a[0];
        a[0]=0;
        for (int i=n-1;i>=1;i--){
            a[i-1]+=a[i]/10;
            a[i]%=10;
        }
        if (a[0]){
            printf("%d",a[0]);
        }
        for (int i=1;i<n;i++)printf("%d",a[i]);
        puts("");
    }
    return 0;
}

J.The Escape Plan of Groundhog

#include<bits/stdc++.h>
using namespace std;
const int N=500+10;
int n,m;
int a[N][N],b[N][N],f[N];
int cnt[N*N*2],v[N];
void clear(int tot){
    for (int i=1;i<=tot;i++)cnt[v[i]]--;
}
int main(){
    scanf("%d%d",&n,&m);
    for (int i=1;i<=n;i++){
        for (int j=1;j<=m;j++){
            scanf("%d",&a[i][j]);
            if (!a[i][j])a[i][j]=-1;
        }
    }
    for (int i=1;i<=n;i++){
        for (int j=1;j<=m;j++){
            b[i][j]+=b[i-1][j]+a[i][j];
        }
    }
    int base=N*N;
    int ans=0;
    for (int u=1;u<n;u++){
        for (int d=u+1;d<=n;d++){
            int sum=0,tot=0;
            for (int i=1;i<=m;i++){
                if (a[u][i]==-1||a[d][i]==-1){
                    sum=0;
                    clear(tot);tot=0;
                    continue;
                }
                if(b[d][i]-b[u-1][i]==d-u+1){
                    ans+=cnt[sum+base-(d-u-1)]+cnt[sum+base-(d-u-1)-1]+cnt[sum+base-(d-u-1)+1];
                    cnt[sum+base]++;
                    v[++tot]=sum+base;
                }
                sum+=b[d-1][i]-b[u][i];
            }
            clear(tot);
        }
    }
    printf("%d\n",ans);
    return 0;
}

K.The Flee Plan of Groundhog

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=1e5+10;
const int mod=1e9+7;
const int inf=0x3f3f3f3f;
int n,m,k,t;
bool f=0;
vector<int>v[N];
int a[N],dep[N],depn[N],cnt=0,mx=0;
void dfs(int x,int fa){
    a[++cnt]=x;
    if (x==n){
        f=1;
        return;
    }
    for (int i:v[x]){
        if (i==fa)continue;
        dfs(i,x);
        if (f)return;
    }
    cnt--;
}
void dfs1(int x,int fa){
    depn[x]=depn[fa]+1;
    for (int i:v[x]){
        if (i==fa)continue;
        dfs1(i,x);
    }
}
void dfs2(int x,int fa){
    dep[x]=dep[fa]+1;
    mx=max(dep[x],mx);
    if (dep[x]>=(depn[x]+1)/2)return;
    mx=max((depn[x]+1)/2,mx);
    for (int i:v[x]){
        if (i==fa)continue;
        dfs2(i,x);
    }
}
signed main(){
    int T,x,y;
    scanf("%d%d",&n,&t);
    for (int i=1;i<n;i++){
        scanf("%d%d",&x,&y);
        v[x].push_back(y);v[y].push_back(x);
    }
    dfs(1,0);
    if (t>=cnt){
        puts("0");
        return 0;
    }
    depn[0]=-1;
    dfs1(n,0);
    dep[0]=-1;
    dfs2(a[t+1],0);
    printf("%d\n",mx);
    return 0;
}

L.The Shopping Plan of Groundhog

posted @ 2020-08-08 17:29  Rynar  阅读(227)  评论(0)    收藏  举报