HDU 全国多校第四场 题解

题解


 

A AND Minimum Spanning Tree

 

 参考代码:

#include
#define maxl 200010
using namespace std;

int n,ans1;
int mi[31];
int ans[maxl];

inline void prework()
{
    scanf("%d",&n);
}

inline int find(int x)
{
    for(int j=0;j<=30;j++)
    if((x&mi[j])==0)
        return mi[j];
}

inline void mainwork()
{
    ans1=0;int x;
    for(int i=2;i<=n;i++)
    {
        if(i&1)
        {
            x=find(i);
            if(x<=n)
                ans[i]=x;
            else
                ans1++,ans[i]=1;    
        }
        else
            ans[i]=1;
    }
}

inline void print()
{
    printf("%d\n",ans1);
    for(int i=2;i<=n;i++)
        printf("%d%c",ans[i],(i==n)?'\n':' ');
}

int main()
{
    mi[0]=1;
    for(int i=1;i<=30;i++)
        mi[i]=mi[i-1]*2;
    int t;
    scanf("%d",&t);
    for(int i=1;i<=t;i++)
    {
        prework();
        mainwork();
        print();
    }
    return 0;
}
View Code

 

B Colored Tree

 unsolved.

C Divide the Stones

 

D Enveloping Convex

 unsolved.

E Good Numbers

 unsolved.

F Horse

 unsolved.

G Just an Old Puzzle

题解:百度“15难题”

  参考代码

#include
using namespace std;

int ans;
int a[17];
int x[17];

inline void prework()
{
    for(int i=1;i<=16;i++)
    { 
        scanf("%d",&a[i]);
        if(a[i]==0)
        { 
            a[i]=16;
            ans=x[i]; 
        } 
    } 
}

inline void mainwork()
{
    for(int i=1;i<=16;i++)
    {
        for(int j=i+1;j<=16;j++)
        if(a[j]<a[i])
            ans++;
    }
    
}

inline void print()
{
    if(ans&1)
        puts("No");
    else
        puts("Yes");
}

int main()
{
    x[2]=1;x[4]=1;x[5]=1;x[7]=1;
    x[10]=1;x[12]=1;x[13]=1;x[15]=1;
    int t;
    scanf("%d",&t);
    for(int cas=1;cas<=t;cas++)
    {
        prework();
        mainwork();
        print();
    }
    return 0;
}
View Code

 

H K-th Closest Distance

题解:主席树+二分

  参考代码

#include
#define maxl 100010
using namespace std;

const int nn=1e6;

int n,m,tot,ans;
int rt[maxl],a[maxl];
struct node
{
    int ls,rs,sum;
}tree[maxl*10*35];

inline void insert(int num,int &x,int l,int r)
{
    tree[++tot]=tree[x];x=tot;
    ++tree[x].sum;
    if(l==r) return;
    int mid=(l+r)>>1;
    if(num<=mid)
        insert(num,tree[x].ls,l,mid);
    else
        insert(num,tree[x].rs,mid+1,r);
}

inline void prework()
{
    scanf("%d%d",&n,&m);
    for(int i=1;i<=n;i++)
        scanf("%d",&a[i]);
    tree[0].ls=tree[0].rs=tree[0].sum=0;
    rt[0]=0;
    tot=0;
    for(int i=1;i<=n;i++)
    {
        rt[i]=rt[i-1];
        insert(a[i],rt[i],1,nn);
    }
}

inline int query(int i,int j,int l,int r,int i1,int j1)
{
    if(i1==l && j1==r)
        return tree[j].sum-tree[i].sum;
    int mid=(i1+j1)>>1,ret;
    if(r<=mid)
        ret=query(tree[i].ls,tree[j].ls,l,r,i1,mid);
    else if(l>mid)
        ret=query(tree[i].rs,tree[j].rs,l,r,mid+1,j1);
    else
    {
        ret=query(tree[i].ls,tree[j].ls,l,mid,i1,mid);
        ret+=query(tree[i].rs,tree[j].rs,mid+1,r,mid+1,j1);
    }
    return ret;
}

inline bool jug(int l,int r,int mid,int p,int k)
{
    int up=min(p+mid,nn),lo=max(1,p-mid);
    int sum=query(rt[l-1],rt[r],lo,up,1,nn);
    if(sum<k)
        return false;
    else
        return true;
}

inline void mainwork()
{
    ans=0;int up,lo,p,k,l,r,mid;
    for(int i=1;i<=m;i++)
    {
        scanf("%d%d%d%d",&lo,&up,&p,&k);
        lo^=ans;up^=ans;p^=ans;k^=ans;
        l=0;r=nn;
        while(l+1<r)
        {
            mid=(l+r)>>1;
            if(!jug(lo,up,mid,p,k))// 
                l=mid;
            else
                r=mid;
        }
        if(jug(lo,up,l,p,k))
            ans=l;
        else
            ans=l+1;
        printf("%d\n",ans);
    }
}

inline void print(){}

int main()
{
    int t;
    scanf("%d",&t);
    for(int i=1;i<=t;i++)
    {
        prework();
        mainwork();
        print();
    }
    return 0;
}
View Code

I Linear Functions

 unsolved.

J Minimal Power of Prime

 参考代码

#include
using namespace std;
typedef long long LL;
const int size=1e6+5;
double eps=1e-8;
int p[size];bool prime[size];
int mpri[size];
int tot=0;
void init()
{
    for(int i=1;itrue;
    for(int i=2;i)
    {
        if(prime[i])
        {
            p[++tot]=i;
            mpri[i]=i;
        }
        for(int j=1;j<=tot&&p[j]*i)
        {
            prime[i*p[j]]=false;mpri[i*p[j]]=p[j];
            if(i%p[j]==0) break;
        }
    }
}
int main()
{
    init();
    int t;
    long long x;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%lld",&x);
        int cnt=0;
        int ans=64;
        if(x<size){
            int ps=mpri[x];
            while(x!=1)
            {
                do x/=ps,cnt++;
                while(mpri[x]==ps);
                ps=mpri[x];
                ans=min(ans,cnt);
            }
            printf("%d\n",ans);
            continue;
        }
        bool flag=false;
        for(int i=1;i<=tot;i++)
        {
            cnt=0;
            if(x%p[i]==0)
            {
                do x/=p[i],cnt++;
                while(x%p[i]==0);
            }
            if(cnt==1)
            {
                puts("1");
                flag=true;
                break;
            }
        }
        if(flag) continue;
        LL sq=sqrt(x)+eps;
        if(sq*sq==x)
        {
            printf("2\n");
        }
        else 
        {
            puts("1");
        }
    }
}    
View Code

 


 

你可能感兴趣的:(HDU 全国多校第四场 题解)