Codeforces Round #538 (Div. 2)解题报告(有待更新)

题目链接:http://codeforces.com/contest/1114

第一题(贪心):

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

#define debug puts("YES");
#define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define ll long long

#define lrt int l,int r,int rt
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define root l,r,rt
#define mst(a,b) memset((a),(b),sizeof(a))
#define pii pair<int,int>
#define fi first
#define se second
#define mk(x,y) make_pair(x,y)
const int mod=1e9+7;
const int maxn=1e5+4;
const int ub=1e6;
ll powmod(ll x,ll y){ll t; for(t=1;y;y>>=1,x=x*x%mod) if(y&1) t=t*x%mod; return t;}
ll gcd(ll x,ll y){return y?gcd(y,x%y):x;}
int x,y,z;
int a,b,c,flag;
int main()
{
    cin>>x>>y>>z;
    cin>>a>>b>>c;
    flag=1;
    if(a>=x) a-=x;
    else flag=0;
    if(a+b>=y&&flag) b-=y-a;
    else flag=0;
    if(b+c>=z&&flag) flag=1;
    else flag=0;
    if(flag) puts("YES");
    else puts("NO");


    return 0;
}

第二题(思维+贪心):

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

#define debug puts("YES");
#define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define ll long long

#define lrt int l,int r,int rt
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define root l,r,rt
#define mst(a,b) memset((a),(b),sizeof(a))
#define pii pair<int,int>
#define fi first
#define se second
#define mk(x,y) make_pair(x,y)
const int mod=1e9+7;
const int maxn=2e5+4;
const int ub=1e6;
ll powmod(ll x,ll y){ll t; for(t=1;y;y>>=1,x=x*x%mod) if(y&1) t=t*x%mod; return t;}
ll gcd(ll x,ll y){return y?gcd(y,x%y):x;}

pair<int,int> p[maxn];
int pos[maxn];
int n,m,k;
ll ans=0;
int main()
{
    cin>>n>>m>>k;
    rep(i,0,n){
        int x;
        cin>>x;
        p[i].fi=-x,p[i].se=i+1;
    }
    sort(p,p+n);///
    rep(i,0,n) p[i].fi=-p[i].fi;
    rep(i,0,m*k) pos[i]=p[i].se,ans+=1LL*p[i].fi;
    sort(pos,pos+m*k);
    cout<<ans<<endl;
    for(int i=1;i<k;i++) cout<<pos[i*m-1]<<" ";

    return 0;
}

第三题(数论):

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

#define debug puts("YES");
#define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define ll long long

#define lrt int l,int r,int rt
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define root l,r,rt
#define mst(a,b) memset((a),(b),sizeof(a))
#define pii pair<int,int>
#define fi first
#define se second
#define mk(x,y) make_pair(x,y)
const int mod=1e9+7;
const int maxn=1e5+4;
const int ub=1e6;
ll powmod(ll x,ll y){ll t; for(t=1;y;y>>=1,x=x*x%mod) if(y&1) t=t*x%mod; return t;}
ll gcd(ll x,ll y){return y?gcd(y,x%y):x;}

ll n,m;

int main()
{
    cin>>n>>m;
    ll ans=-1;
    for(ll i=2;1LL*i*i<=m;i++)if(m%i==0){
        ll cnt1=0;while(m%i==0) m/=i,cnt1++;
        ll cnt2=0,tn=n;while(tn) cnt2+=tn/i,tn/=i;
        if(ans==-1) ans=cnt2/cnt1;
        else ans=min(ans,cnt2/cnt1);
    }
    if(m>1){
        ll cnt=0;
        while(n) cnt+=n/m,n/=m;
        if(ans==-1) ans=cnt;
        else ans=min(ans,cnt);
    }
    ans=max(ans,0LL);
    cout<<ans<<endl;
    return 0;
}

第四题(区间DP):

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

#define debug puts("YES");
#define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define ll long long

#define lrt int l,int r,int rt
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define root l,r,rt
#define mst(a,b) memset((a),(b),sizeof(a))
#define pii pair<int,int>
#define fi first
#define se second
#define mk(x,y) make_pair(x,y)
const int mod=1e9+7;
const int maxn=5e3+10;
const int ub=1e6;
ll powmod(ll x,ll y){ll t; for(t=1;y;y>>=1,x=x*x%mod) if(y&1) t=t*x%mod; return t;}
ll gcd(ll x,ll y){return y?gcd(y,x%y):x;}
int n,a[maxn],x,cnt=0;
int f[maxn][maxn][2];
int main(){
    cin>>n;
    rep(i,1,n+1){
        scanf("%d",&x);
        if(cnt==0||a[cnt-1]!=x) a[cnt++]=x;
    }
    n=cnt;for(int i=n;i>=1;i--) a[i]=a[i-1];
    mst(f,0xf);///置为无限大
    rep(i,1,n+1) f[i][i][0]=f[i][i][1]=0;
    rep(i,2,n+1) rep(j,1,n-i+2){
        int l=j,r=j+i-1;
        f[l][r][0]=min(f[l][r][0],f[l+1][r][0]+1);
        f[l][r][0]=min(f[l][r][0],f[l+1][r][1]+((a[r]==a[l])?0:1));
        f[l][r][1]=min(f[l][r][1],f[l][r-1][1]+1);
        f[l][r][1]=min(f[l][r][1],f[l][r-1][0]+((a[l]==a[r])?0:1));
    }
    cout<<min(f[1][n][0],f[1][n][1])<<'\n';
    return 0;
}

第六题(简单数论+线段树+位运算):

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

#define debug puts("YES");
#define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define ll long long

#define lrt int l,int r,int rt
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define root l,r,rt
#define mst(a,b) memset((a),(b),sizeof(a))
#define pii pair<int,int>
#define fi first
#define se second
#define mk(x,y) make_pair(x,y)
const int mod=1e9+7;
const int maxn=4e5+10;
const int ub=1e6;
const double inf=1e-4;
ll powmod(ll x,ll y){ll t; for(t=1;y;y>>=1,x=x*x%mod) if(y&1) t=t*x%mod; return t;}
ll gcd(ll x,ll y){return y?gcd(y,x%y):x;}

int p[400],vis[400],tot=0;
ll inv[301];
void sieve(){
    rep(i,2,300){
        if(!vis[i]) p[tot++]=i;
        rep(j,0,tot){
            if(1LL*i*p[j]>=300) break;
            vis[i*p[j]]=1;
            if(i%p[j]==0) break;
        }
    }
    mst(inv,0),inv[0]=inv[1]=1;
    for(int i=2;i<301;i++) inv[i]=1LL*(mod-mod/i)*inv[mod%i]%mod;
    ///cout<<inv[59]<<" "<<powmod(59LL,mod-2)<<endl;
}
int n,m,x;
ll a[maxn],b[maxn];
struct seg{
    ll tree[maxn<<2],lazy[maxn<<2];
    int flag;
    void build(lrt){
        lazy[rt]=flag;
        if(l==r){
            if(flag) tree[rt]=b[l];
            else tree[rt]=a[l];
            return ;
        }
        int mid=l+r>>1;
        build(lson),build(rson);
        if(flag) tree[rt]=tree[rt<<1]*tree[rt<<1|1]%mod;
        else tree[rt]=tree[rt<<1]|tree[rt<<1|1];
    }
    void pushdown(int l,int r,int rt){
        int mid=l+r>>1;
        if(lazy[rt]!=flag){
            if(flag){
                (lazy[rt<<1]*=lazy[rt])%=mod;
                (lazy[rt<<1|1]*=lazy[rt])%=mod;
                tree[rt<<1]=tree[rt<<1]*powmod(lazy[rt],mid-l+1)%mod;
                tree[rt<<1|1]=tree[rt<<1|1]*powmod(lazy[rt],r-mid)%mod;
            }
            else{
                lazy[rt<<1]|=lazy[rt];
                lazy[rt<<1|1]|=lazy[rt];
                tree[rt<<1]|=lazy[rt];
                tree[rt<<1|1]|=lazy[rt];
            }
            lazy[rt]=flag;
        }
    }
    void update(lrt,int L,int R,ll v){
        if(L<=l&&r<=R){
            if(flag)tree[rt]=tree[rt]*powmod(v,r-l+1)%mod;
            else tree[rt]|=v;

            if(flag) (lazy[rt]*=v)%=mod;
            else lazy[rt]|=v;
            return ;
        }
        pushdown(l,r,rt);
        int mid=l+r>>1;
        if(L<=mid)update(lson,L,R,v);
        if(mid<R) update(rson,L,R,v);
        if(flag) tree[rt]=tree[rt<<1]*tree[rt<<1|1]%mod;
        else tree[rt]=tree[rt<<1]|tree[rt<<1|1];
    }
    ll query(lrt,int L,int R){
        if(L<=l&&r<=R)  return tree[rt];
        int mid=l+r>>1;
        pushdown(l,r,rt);
        ll ans=flag;
        if(L<=mid){
            if(flag) ans=ans*query(lson,L,R)%mod;
            else ans|=query(lson,L,R);
        }
        if(mid<R){
            if(flag) ans=ans*query(rson,L,R)%mod;
            else ans|=query(rson,L,R);
        }
        return ans;
    }
}tree1,tree2;
int main(){
    sieve();
    cin>>n>>m;
    rep(i,1,n+1){
        cin>>x;b[i]=x;///
        a[i]=0;
        for(int j=2;j<=x;j++)if(x%j==0){
            int pos=lower_bound(p,p+tot,j)-p;
            while(x%j==0) x/=j;
            a[i]|=1LL<<pos;
        }
    }
    tree1.flag=1,tree2.flag=0;
    tree1.build(1,n,1);
    tree2.build(1,n,1);
    rep(i,0,m){
        char op[20];scanf("%s",op);
        int x,y,z;
        if(op[0]=='T'){
           cin>>x>>y;
           ll ret=tree2.query(1,n,1,x,y),ans=tree1.query(1,n,1,x,y);
           rep(j,0,tot) if((1LL<<j)&ret)///这里有点玄学,用==1LL就不行
                ans=ans*(p[j]-1)%mod*inv[p[j]]%mod;
            cout<<ans<<endl;
        }
        else{
            cin>>x>>y>>z;
            tree1.update(1,n,1,x,y,z);
            ll sta=0;
            for(int j=2;j<=z;j++)if(z%j==0){
                int pos=lower_bound(p,p+tot,j)-p;
                sta|=1LL<<pos;
                while(z%j==0) z/=j;
            }
            tree2.update(1,n,1,x,y,sta);
        }
    }
    return 0;
}

 

没有更多推荐了,返回首页