Codeforces Round #615 (Div. 3)

A Collecting Coins

先补齐到最大数,然后平均分配

T = int(input())
for kase in range(T):
    a, b, c, n = [int(x) for x in input().split()]
    v = sorted([a,b,c])
    t = v[2]-v[1] + v[2]-v[0]
    if n>=t and (n-t)%3==0:
        print("YES")
    else:
        print("NO")

B Collecting Packages

贪心地先往右走再往上走

T = int(input())
for kase in range(T):
    lis = []
    n = int(input())
    ans = ''
    for i in range(n):
        t = [int(x) for x in input().split()]
        lis.append((t[0],t[1]))
    lis.sort()
    now = (0,0)
    for p in lis:
        ans += 'R'*(p[0]-now[0])
        ans += 'U'*(p[1]-now[1])
        if p[1]<now[1]:
            ans = -1
            break
        else:
            now = p
    if ans!=-1:
        print('YES')
        print(ans)
    else:
        print("NO")

C Product of Three Numbers

分解质因数,假设质因数个数为 q q q,分 q = 1 , q = 2 , q ≥ 3 q=1,q=2,q\ge 3 q=1,q=2,q3三种情况讨论

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define iinf 0x3f3f3f3f
#define linf (1ll<<60)
#define eps 1e-8
#define maxn 1000010
#define maxe 1000010
#define cl(x) memset(x,0,sizeof(x))
#define rep(_,__) for(_=1;_<=(__);_++)
#define em(x) emplace(x)
#define emb(x) emplace_back(x)
#define emf(x) emplace_front(x)
#define fi first
#define se second
#define de(x) cerr<<#x<<" = "<<x<<endl
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll read(ll x=0)
{
    ll c, f(1);
    for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-f;
    for(;isdigit(c);c=getchar())x=x*10+c-0x30;
    return f*x;
}
struct EasyMath
{
    ll prime[maxn], phi[maxn], mu[maxn];
    bool mark[maxn];
    ll fastpow(ll a, ll b, ll c)
    {
        ll t(a%c), ans(1ll);
        for(;b;b>>=1,t=t*t%c)if(b&1)ans=ans*t%c;
        return ans;
    }
    void exgcd(ll a, ll b, ll &x, ll &y)
    {
        if(!b){x=1,y=0;return;}
        ll xx, yy;
        exgcd(b,a%b,xx,yy);
        x=yy, y=xx-a/b*yy;
    }
    ll inv(ll x, ll p)  //p是素数
    {return fastpow(x%p,p-2,p);}
    ll inv2(ll a, ll p)
    {
        ll x, y;
        exgcd(a,p,x,y);
        return (x+p)%p;
    }
    void shai(ll N)
    {
        ll i, j;
        for(i=2;i<=N;i++)mark[i]=false;
        *prime=0;
        phi[1]=mu[1]=1;
        for(i=2;i<=N;i++)
        {
            if(!mark[i])prime[++*prime]=i, mu[i]=-1, phi[i]=i-1;
            for(j=1;j<=*prime and i*prime[j]<=N;j++)
            {
                mark[i*prime[j]]=true;
                if(i%prime[j]==0)
                {
                    phi[i*prime[j]]=phi[i]*prime[j];
                    break;
                }
                mu[i*prime[j]]=-mu[i];
                phi[i*prime[j]]=phi[i]*(prime[j]-1);
            }
        }
    }
    ll CRT(vector<ll> a, vector<ll> m) //要求模数两两互质
    {
        ll M=1, ans=0, n=a.size(), i;
        for(i=0;i<n;i++)M*=m[i];
        for(i=0;i<n;i++)(ans+=a[i]*(M/m[i])%M*inv2(M/m[i],m[i]))%=M;
        return ans;
    }
}em;
struct CombinatorialNumber_mod
{
    ll p[20], q[20], fact[20][maxn], tot, t[20];
    void init(ll P)
    {
        tot=0;
        ll i, j;
        for(i=2;i*i<=P;i++)
            if(P%i==0)
            {
                p[++tot]=i;
                q[tot]=0;
                while(P%i==0)q[tot]++, P/=i;
            }
        if(P>1)p[++tot]=P, q[tot]=1;
        // rep(i,tot)
        // {
        //     fact[i][0]=1;
        //     t[i]=1;
        //     rep(j,q[i])t[i]*=p[i];
        //     rep(j,maxn-1)
        //     {
        //         if(j%p[i]==0)fact[i][j]=fact[i][j-1];
        //         else fact[i][j]=fact[i][j-1]*j%t[i];
        //     }
        // }
    }
    pll fact_mod(ll n, ll id)
    {
        if(n<p[id])return pll(0,fact[id][n]);
        pll ans = pll( n/p[id], em.fastpow(fact[id][t[id]],n/t[id],t[id]) );
        (ans.second*=fact[id][n%t[id]])%t[id];
        auto nex = fact_mod(n/p[id],id);
        ans.first+=nex.first;
        (ans.second*=nex.second)%=t[id];
        return ans;
    }
    ll exlucas(ll n, ll m, ll id)
    {
        ll cnt;
        auto a=fact_mod(n,id), b=fact_mod(m,id), c=fact_mod(n-m,id);
        a.first-=b.first+c.first;
        (a.second*=em.inv2(b.second*c.second%t[id],t[id]))%t[id];
        return em.fastpow(p[id],a.first,t[id])*a.second%t[id];
    }
    ll calc(ll n, ll m)
    {
        if(m>n or m<0 or n<0)return 0;
        vector<ll> a, v(t+1,t+tot+1);
        ll i;
        rep(i,tot)a.emb(exlucas(n,m,i));
        return em.CRT(a,v);
    }
}Cmod;
int main()
{
    ll T=read(), n;
    while(T--)
    {
        n=read();
        Cmod.init(n);
        if(Cmod.tot==1)
        {
            if(Cmod.q[1]<6)
            {
                printf("NO\n");
            }
            else
            {
                ll p=Cmod.p[1];
                ll a=p, b=p*p, c=n/a/b;
                printf("YES\n%lld %lld %lld\n",a,b,c);
            }            
        }
        else if(Cmod.tot==2)
        {
            if(Cmod.q[1]+Cmod.q[2]<4)
            {
                printf("NO\n");
            }
            else
            {
                ll a=Cmod.p[1], b=Cmod.p[2], c=n/a/b;
                printf("YES\n%lld %lld %lld\n",a,b,c);
            }   
        }
        else
        {
            ll a=Cmod.p[1], b=Cmod.p[2], c=n/a/b;
            printf("YES\n%lld %lld %lld\n",a,b,c);
        }
    }
    return 0;
}

D MEX maximizing

按模 x x x的余数分组,每组从小到大来填数字

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define iinf 0x3f3f3f3f
#define linf (1ll<<60)
#define eps 1e-8
#define maxn 1000010
#define maxe 1000010
#define cl(x) memset(x,0,sizeof(x))
#define rep(_,__) for(_=1;_<=(__);_++)
#define em(x) emplace(x)
#define emb(x) emplace_back(x)
#define emf(x) emplace_front(x)
#define fi first
#define se second
#define de(x) cerr<<#x<<" = "<<x<<endl
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll read(ll x=0)
{
    ll c, f(1);
    for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-f;
    for(;isdigit(c);c=getchar())x=x*10+c-0x30;
    return f*x;
}
bitset<maxn> b;
ll cnt[maxn];
int main()
{
    ll q=read(), i, x=read(), mex=0;
    while(q--)
    {
        ll y=read()%x;
        if(cnt[y]*x+y<maxn)b.set(cnt[y]*x+y);
        cnt[y]++;
        while(b.test(mex))mex++;
        printf("%lld\n",mex);
    }
    return 0;
}

E Obtain a Permutation

每一列分别做

假设有 c n t [ x ] cnt[x] cnt[x]个数字可以通过 x x x次旋转回到原位,那么旋转 x x x次还需要修改 n − c n t [ x ] n-cnt[x] ncnt[x]

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define iinf 0x3f3f3f3f
#define linf (1ll<<60)
#define eps 1e-8
#define maxn 1000010
#define maxe 1000010
#define cl(x) memset(x,0,sizeof(x))
#define rep(_,__) for(_=1;_<=(__);_++)
#define em(x) emplace(x)
#define emb(x) emplace_back(x)
#define emf(x) emplace_front(x)
#define fi first
#define se second
#define de(x) cerr<<#x<<" = "<<x<<endl
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll read(ll x=0)
{
    ll c, f(1);
    for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-f;
    for(;isdigit(c);c=getchar())x=x*10+c-0x30;
    return f*x;
}
struct Array2
{
    ll n, m, a[maxn];
    void init(ll N, ll M)
    {
        n=N, m=M;
        for(ll i=0;i<n*m;i++)a[i]=0;
    }
    ll index(ll i, ll j){return i*m+j;}
    ll& at(ll i, ll j){return a[index(i,j)];}
    ll* operator[](ll i){return a+i*m;}
}mat;
ll cnt[maxn], should[maxn];
int main()
{
    ll n=read(), m=read(), i, j, ans=0, mn;
    mat.init(n,m);
    rep(i,n)rep(j,m)mat[i][j]=read();
    rep(j,m)
    {
        mn=linf;
        rep(i,n)should[j+(i-1)*m]=i;
        rep(i,n)
        {
            ll x=mat[i][j];
            if(should[x])
            {
                cnt[(n+i-should[x])%n]++;
            }
        }
        for(i=0;i<n;i++)mn=min(mn,i+n-cnt[i]);
        for(i=0;i<n;i++)cnt[i]=0;
        rep(i,n)should[j+(i-1)*m]=0;
        ans+=mn;
    }
    cout<<ans;
    return 0;
}

F Three Paths on a Tree

两遍 d f s dfs dfs

对于每个点 u u u,对于其每个相邻的 v v v,都算出从 u u u走到 v v v之后最远能走多少步

最后找所有度 ≥ 3 \ge 3 3的点,找出前三大加起来

特判一条链

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define iinf 0x3f3f3f3f
#define linf (1ll<<60)
#define eps 1e-8
#define maxn 1000010
#define maxe 1000010
#define cl(x) memset(x,0,sizeof(x))
#define rep(_,__) for(_=1;_<=(__);_++)
#define em(x) emplace(x)
#define emb(x) emplace_back(x)
#define emf(x) emplace_front(x)
#define fi first
#define se second
#define de(x) cerr<<#x<<" = "<<x<<endl
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll read(ll x=0)
{
    ll c, f(1);
    for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-f;
    for(;isdigit(c);c=getchar())x=x*10+c-0x30;
    return f*x;
}
struct Graph
{
    int etot, head[maxn], to[maxe], next[maxe], w[maxe];
    void clear(int N)
    {
        for(int i=1;i<=N;i++)head[i]=0;
        etot=0;
    }
    void adde(int a, int b, int c=0){to[++etot]=b;w[etot]=c;next[etot]=head[a];head[a]=etot;}
    #define forp(pos,G) for(auto p=G.head[pos];p;p=G.next[p])
}G;
ll n, depth[maxn];
vector<pll> lis[maxn];
map<pll,ll> from[maxn];
pll mxdep[maxn];
void dfs1(ll pos, ll pre)
{
    depth[pos]=depth[pre]+1;
    mxdep[pos]=pll(depth[pos],pos);
    forp(pos,G)if(G.to[p]!=pre)
    {
        ll to=G.to[p];
        dfs1(to,pos);
        lis[pos].emb( pll(mxdep[to].first-depth[pos], mxdep[to].second) );
        from[pos][lis[pos].back()]=to;
        mxdep[pos]=max(mxdep[pos],mxdep[to]);
    }
}
void dfs2(ll pos, ll pre, pll up)
{
    sort(lis[pos].begin(),lis[pos].end(),greater<pll>());
    forp(pos,G)if(G.to[p]!=pre)
    {
        ll to=G.to[p];
        if(to==from[pos][lis[pos].at(0)])
        {
            if(lis[pos].size()>1)
            {
                auto t=max(up,lis[pos].at(1));
                t.first++;
                dfs2(to,pos,t);
            }
            else
            {
                auto t=up;
                t.first++;
                dfs2(to,pos,t);
            }
        }
        else
        {
            auto t=max(up,lis[pos].at(0));
            t.first++;
            dfs2(to,pos,t);
        }
    }
    if(up.first)
    {
        lis[pos].emb(up);
    }
}
int main()
{
    ll u, v, i;
    tuple<ll,ll,ll,ll> ans(0,0,0,0);
    n=read();
    rep(i,n-1)
    {
        u=read(), v=read();
        G.adde(u,v);
        G.adde(v,u);
    }
    dfs1(1,0);
    dfs2(1,0,pll(0,1));
    vector<ll> lea;
    rep(i,n)
    {
        sort(lis[i].begin(),lis[i].end(),greater<pll>());
        if(lis[i].size()>=3)
        {
            auto tmp = make_tuple(lis[i][0].first+lis[i][1].first+lis[i][2].first,lis[i][0].second,lis[i][1].second,lis[i][2].second);
            ans=max(ans,tmp);
        }
        if(lis[i].size()==1)
        {
            lea.emb(i);
        }
    }
    if(get<0>(ans)==0)
    {
        ll t;
        rep(i,n)if(lis[i].size()==2)t=i;
        ans=make_tuple(n-1,lea[0],lea[1],t);
    }
    cout<<get<0>(ans)<<endl;
    cout << get<1>(ans) << ' ' << get<2>(ans) << ' ' << get<3>(ans) << endl;
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值