BestCoder Round #77

6 篇文章 0 订阅
5 篇文章 0 订阅

此处有目录↑

HDU-5650 so easy (数学)

http://acm.hdu.edu.cn/showproblem.php?pid=5650

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)


Problem Description
Given an array with
n
integers, assume  f(S)  as the result of executing xor operation among all the elements of set  S . e.g. if  S={1,2,3}  then  f(S)=0 .

your task is: calculate xor of all  f(s) , here  sS .
 

Input
This problem has multi test cases. First line contains a single integer  T(T20)  which represents the number of test cases.
For each test case, the first line contains a single integer number  n(1n1,000)  that represents the size of the given set. then the following line consists of  n different integer numbers indicate elements( 109 ) of the given set.
 

Output
For each test case, print a single integer as the answer.
 

Sample Input
  
  
1 3 1 2 3
 

Sample Output
  
  
0 In the sample,S = {1, 2, 3}, subsets of S are: Φ, {1}, {2}, {3}, {1, 2}, {1, 3}, {2, 3}, {1, 2, 3}

题目大意:记f(s)为s集合中所有元素的异或,给定一个集合S,求所有f(s)的异或,sS。

做的时候没有仔细分析,想了个错误的解法(n为奇数时输出所有元素的异或,否则输出0),竟然水过了,原来数据这么水...

官方题解:

设集合有n个数,则包含x的子集个数有2^(n-1)个。 那么当n > 1时,x出现了偶数次,所以其对答案的贡献就是0;当 n = 1时,其对答案的贡献是 x。

#include <cstdio>

using namespace std;

int T,n;
long long ans,tmp;

int main() {
    scanf("%d",&T);
    while(T--) {
        scanf("%d",&n);
        ans=0;
        for(int i=0;i<n;++i) {
            scanf("%I64d",&tmp);
            ans^=tmp;
        }
        printf("%I64d\n",(n==1)?ans:0);
    }
    return 0;
}


HDU-5651 xiaoxin juju needs help (数学)

http://acm.hdu.edu.cn/showproblem.php?pid=5651

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)



Problem Description
As we all known, xiaoxin is a brilliant coder. He knew **palindromic** strings when he was only a six grade student at elementry school.

This summer he was working at Tencent as an intern. One day his leader came to ask xiaoxin for help. His leader gave him a string and he wanted xiaoxin to generate palindromic strings for him. Once xiaoxin generates a different palindromic string, his leader will give him a watermelon candy. The problem is how many candies xiaoxin's leader needs to buy?
 

Input
This problem has multi test cases. First line contains a single integer  T(T20)  which represents the number of test cases.
For each test case, there is a single line containing a string  S(1length(S)1,000) .
 

Output
For each test case, print an integer which is the number of watermelon candies xiaoxin's leader needs to buy after mod  1,000,000,007 .
 

Sample Input
  
  
3 aa aabb a
 

Sample Output
  
  
1 2 1

题目大意:给定一行小写字母组成的字符串,求能形成多少个回文串?

若出现奇数次的字母的个数大于1,则无法组成回文串

否则只考虑左半边的情况,答案为左半边的排列数


写到一半,换了个变量名,导致初始化错误,查错半天...

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

int T,len,cnt[127],tol,t,gcd,num[505],nn,deno[505],nd;
long long ans;
char s[1005];

const long long mod=1000000007;
int main() {
    scanf("%d",&T);
    while(T--) {
        memset(cnt,0,sizeof(cnt));
        scanf("%s",s);
        len=strlen(s);
        for(int i=0;i<len;++i)
            ++cnt[s[i]];
        tol=0;
        for(int i='a';i<='z';++i)
            if((cnt[i]&1)==1) {
                ++tol;
                --cnt[i];
            }
        if(tol>1) {
            printf("0\n");
            continue;
        }
        nn=len>>1;
        for(int i=0;i<nn;++i)
            num[i]=i+1;
        for(int i='a';i<='z';++i) {//枚举每个字母
            for(int j=cnt[i]>>1;j>1;--j) {
                t=j;
                for(int k=0;k<nn&&t>1;++k) {//分子约去分母
                    gcd=__gcd(num[k],t);
                    if(gcd>1) {
                        num[k]/=gcd;
                        t/=gcd;
                    }
                }
            }
        }
        ans=1;
        for(int i=0;i<nn;++i)
            ans=(ans*num[i])%mod;
        printf("%I64d\n",ans);
    }
    return 0;
}

——————————————————2题的旅游分割线———————————————————

HDU-5652 India and China Origins (二分||并查集)

http://acm.hdu.edu.cn/showproblem.php?pid=5652

Time Limit: 2000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)


Problem Description
A long time ago there are no himalayas between India and China, the both cultures are frequently exchanged and are kept in sync at that time, but eventually himalayas rise up. With that at first the communation started to reduce and eventually died.



Let's assume from my crude drawing that the only way to reaching from India to China or viceversa is through that grid, blue portion is the ocean and people haven't yet invented the ship. and the yellow portion is desert and has ghosts roaming around so people can't travel that way. and the black portions are the location which have mountains and white portions are plateau which are suitable for travelling. moutains are very big to get to the top, height of these mountains is infinite. So if there is mountain between two white portions you can't travel by climbing the mountain.
And at each step people can go to 4 adjacent positions.

Our archeologists have taken sample of each mountain and estimated at which point they rise up at that place. So given the times at which each mountains rised up you have to tell at which time the communication between India and China got completely cut off.
 

Input
There are multi test cases. the first line is a sinle integer  T  which represents the number of test cases.

For each test case, the first line contains two space seperated integers  N,M . next  N  lines consists of strings composed of  0,1  characters.  1  denoting that there's already a mountain at that place,  0  denoting the plateau. on  N+2  line there will be an integer  Q  denoting the number of mountains that rised up in the order of times. Next  Q  lines contain  2  space seperated integers  X,Y  denoting that at ith year a mountain rised up at location  X,Y .

T10

1N500

1M500

1QNM

0X<N

0Y<M
 

Output
Single line at which year the communication got cut off.

print -1 if these two countries still connected in the end.

Hint:



From the picture above, we can see that China and India have no communication since 4th year.
 

Sample Input
  
  
1 4 6 011010 000010 100001 001000 7 0 3 1 5 1 3 0 0 1 2 2 4 2 1
 

Sample Output
  
  
4

题目大意:给一个n*m的01地图(0代表能走,1代表不能走),和q个操作,第i个操作令(x,y)处为1,判断多少个操作后无法从最上面走到最下面(只能向相邻的四个方向走)。

二分:

转换成判断1是否能从最左边到达最右边时出错,判断'0'时只用判断四个方向,而判断'1'时必须判断8个方向,一直没想到,比赛时没调出来...

#include <cstdio>
#include <cstring>
#include <queue>
#include <algorithm>

using namespace std;

struct Node {
    int x,y;
    Node(int xx=0,int yy=0):x(xx),y(yy) {}
}u;

int T,n,m,num,x[250005],y[250005];
char ori[505][505],cur[505][505];
bool vis[505][505];
queue<Node> q;

const int dx[8]={-1,-1,0,1,1,1,0,-1};
const int dy[8]={0,1,1,1,0,-1,-1,-1};

inline bool isInside(int xx,int yy) {
    return 0<=xx&&xx<n&&0<=yy&&yy<m;
}

bool bfs(int cc) {
    for(int i=0;i<n;++i) {
        for(int j=0;j<m;++j) {
            cur[i][j]=ori[i][j];
            vis[i][j]=false;
        }
    }

    for(int i=1;i<=cc;++i)
        cur[x[i]][y[i]]='1';

    while(!q.empty())
        q.pop();

    int xx,yy;
    for(xx=0;xx<n;++xx) {//左起的山
        if(cur[xx][0]=='1') {
            vis[xx][0]=true;
            q.push(Node(xx,0));
        }
    }

    while(!q.empty()) {
        u=q.front();
        q.pop();

        for(int i=0;i<8;++i) {
            xx=u.x+dx[i];
            yy=u.y+dy[i];
            if(isInside(xx,yy)&&!vis[xx][yy]&&cur[xx][yy]=='1') {
                if(yy==m-1)//若能到达右边则不连通
                    return true;
                vis[xx][yy]=true;
                q.push(Node(xx,yy));
            }
        }
    }
    return false;
}

int main() {
    scanf("%d",&T);
    while(T--) {
        scanf("%d%d",&n,&m);
        for(int i=0;i<n;++i)
            scanf("%s",ori[i]);
        scanf("%d",&num);
        for(int i=1;i<=num;++i)
            scanf("%d%d",x+i,y+i);
        if(bfs(0)) {
            printf("0\n");
            continue;
        }

        int l=1,r=num+1,mid;
        while(l<=r) {
            mid=(l+r)>>1;
            if(bfs(mid))
                r=mid-1;
            else
                l=mid+1;
        }
        printf("%d\n",l>num?-1:l);
    }
    return 0;
}

并查集:

刚开始我也想到过并查集,但是没有想到要逆序处理,结果不知道怎么办,然后就写二分了,应该想到正难则反的
初始化联通块时,想着简便,只对当前点往右和往下进行合并操作,但是WA了好久,最后正常地对当前点的四个方向进行合并操作才AC,不知道WA在哪了。。。

#include <cstdio>
#include <cstring>

using namespace std;

int T,n,m,num,x[250005],y[250005];
int par[250005],u,xx,yy;
char ori[505][505];

const int sta=250002,des=250003;
const int dx[4]={-1,0,1,0};
const int dy[4]={0,1,0,-1};

inline bool isInside(int xx,int yy) {
    return 0<=xx&&xx<n&&0<=yy&&yy<m;
}

inline int cal(int x,int y) {
    if(x==-1)
        return sta;
    if(x==n)
        return des;
    return x*m+y;
}

int getPar(int a) {
    if(a==par[a])
        return a;
    return par[a]=getPar(par[a]);
}

inline void merg(int a,int b) {
    int pa=getPar(a),pb=getPar(b);
    if(pa!=pb)
        par[pa]=pb;
}

int main() {
    scanf("%d",&T);
    while(T--) {
        scanf("%d%d",&n,&m);
        for(int i=0;i<n;++i)
            scanf("%s",ori[i]);
        scanf("%d",&num);
        for(int i=1;i<=num;++i) {
            scanf("%d%d",x+i,y+i);
            ori[x[i]][y[i]]='1';
        }

        par[sta]=sta;
        par[des]=des;
        for(int i=0;i<n;++i) {//初始化每个点与自己联通
            for(int j=0;j<m;++j) {
                u=cal(i,j);
                par[u]=u;
            }
        }
        for(int i=0;i<n;++i) {
            for(int j=0;j<m;++j) {
                if(ori[i][j]=='0') {
                    u=cal(i,j);
                    for(int dir=0;dir<4;++dir) {
                        xx=i+dx[dir];
                        yy=j+dy[dir];
                        if(xx==-1||xx==n||(isInside(xx,yy)&&ori[xx][yy]=='0'))
                            merg(u,cal(xx,yy));
                    }
                }
            }
        }
        if(getPar(sta)==getPar(des)) {
            printf("-1\n");
            continue;
        }
        while(num>0) {
            ori[x[num]][y[num]]='0';
            u=cal(x[num],y[num]);
            for(int dir=0;dir<4;++dir) {
                xx=x[num]+dx[dir];
                yy=y[num]+dy[dir];
                if(xx==-1||xx==n||(isInside(xx,yy)&&ori[xx][yy]=='0'))
                    merg(u,cal(xx,yy));
            }

            if(getPar(sta)==getPar(des))
                break;
            --num;
        }
        printf("%d\n",num);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值