CodeForces - 1362

CodeForces - 1362

A - Johnny and Ancient Computer

int t;
ll a,b;
int main()
{
     
    scanf("%d",&t);
    while(t--)
    {
     
        int num=0;
        scanf("%lld%lld",&a,&b);
        if (a<=b)swap(a,b);
        if (a%b!=0)W(-1);
        else
        {
     
            ll x=a/b;
            while(x%8==0)
            {
     
                x/=8;
                num++;
            }
            while(x%4==0)
            {
     
                x/=4;
                num++;
            }
            while(x%2==0)
            {
     
                x/=2;
                num++;
            }
            if (x==1)W(num);
            else W(-1);
        }
    }
}



B - Johnny and His Hobbies

直接暴力

int t,n,a[maxn],b[maxn];
int main()
{
     
    scanf("%d",&t);
    while(t--)
    {
     
        bool ok=false;
        scanf("%d",&n);
        rep(i,1,n)scanf("%d",&a[i]);
        sort(a+1,a+1+n);
        rep(i,1,1024)
        {
     
            rep(j,1,n)b[j]=a[j]^i;
            sort(b+1,b+1+n);
            bool mark=true;
            rep(j,1,n)if (a[j]!=b[j])mark=false;
            if (mark)
            {
     
                W(i);
                ok=true;
                break;
            }
        }
        if (!ok)W(-1);
    }
    return 0;
}



C - Johnny and Another Rating Drop

打表发现:
序列为1 2 1 3 1 2 1 4 1 2 1 3...
也就说每2^i个位置就理应出现一个i(存在覆盖问题)

int m=0,t;
ll x,kk[100],num[100];
int main()
{
     

    for (ll i=1;i<=INFF;i*=2)kk[m++]=i;
    scanf("%d",&t);
    while(t--)
    {
     
        scanf("%lld",&x);
        for (int i=m-1;i>=0;i--)num[i]=x/kk[i];
        ll s=0,sum=0;
        for (int i=m-1;i>=0;i--)
        {
     
            num[i]-=s;
            s+=num[i];
        }
        rep(i,0,m-1)sum+=(i+1)*num[i];
        WW(sum);
    }
    return 0;
}



D - Johnny and Contribution

排序完之后直接验证是否正确即可

int n,m,a,b,now[maxn];
vector<int>G[maxn];
bool vis[maxn];
struct node
{
     
    int first,second;
}p[maxn];
bool cmp(node a,node b)
{
     
    return a.second<b.second;
}
int main()
{
     
    mem(vis,false);
    scanf("%d%d",&n,&m);
    rep(i,1,m)
    {
     
        scanf("%d%d",&a,&b);
        G[a].pb(b);
        G[b].pb(a);
    }
    rep(i,1,n)
    {
     
        scanf("%d",&p[i].second);
        p[i].first=i;
        now[i]=1;
    }
    sort(p+1,p+1+n,cmp);
    bool mark=true;
    rep(i,1,n)
    {
     
        if (now[p[i].first]==p[i].second)vis[p[i].first]=true;
        else
        {
     
            mark=false;
            break;
        }
        for (int j=0;j<G[p[i].first].size();j++)
        {
     
            if (vis[G[p[i].first][j]])continue;
            if (now[G[p[i].first][j]]!=p[i].second)continue;
            now[G[p[i].first][j]]++;
        }
    }
    if (!mark)
    {
     
        W(-1);
        return 0;
    }
    rep(i,1,n)printf("%d ",p[i].first);puts("");
    return 0;
}

你可能感兴趣的:(Codeforces)