hdu 4876 ZCC loves cards(暴搜+剪枝)
来源:互联网 发布:新能源乘用车销量数据 编辑:程序博客网 时间:2024/05/16 05:37
http://acm.hdu.edu.cn/showproblem.php?pid=4876
ZCC loves cards
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1683 Accepted Submission(s): 424
Problem Description
ZCC loves playing cards. He has n magical cards and each has a number on it. He wants to choose k cards and place them around in any order to form a circle. He can choose any severalconsecutive cards the number of which is m(1<=m<=k) to play a magic. The magic is simple that ZCC can get a number x=a1⊕a2...⊕am, which ai means the number on the ith card he chooses. He can play the magic infinite times, butonce he begin to play the magic, he can’t change anything in the card circle including the order.
ZCC has a lucky number L. ZCC want to obtain the number L~R by using one card circle. And if he can get other numbers which aren’t in the range [L,R], it doesn’t matter. Help him to find the maximal R.
ZCC has a lucky number L. ZCC want to obtain the number L~R by using one card circle. And if he can get other numbers which aren’t in the range [L,R], it doesn’t matter. Help him to find the maximal R.
Input
The input contains several test cases.The first line in each case contains three integers n, k and L(k≤n≤20,1≤k≤6,1≤L≤100). The next line contains n numbers means the numbers on the n cards. The ith number a[i] satisfies 1≤a[i]≤100.
You can assume that all the test case generated randomly.
You can assume that all the test case generated randomly.
Output
For each test case, output the maximal number R. And if L can’t be obtained, output 0.
Sample Input
4 3 12 3 4 5
Sample Output
7Hint⊕ means xor
Author
镇海中学
Source
2014 Multi-University Training Contest 2
题意:给n个数,现在要从n个中选K个出来,并把它们排成一圈,现在可以从K个数中选连续的m个(1<=m<=k),将选中数异或起来就会得到一个数,给定一个L,问最大的R,使得一个圈中L,R中的所有数都出现过。
这个题暴力的复杂度为c(n,k)*k!*k*k,复杂度明显太高,由于数据是随机的,所以暴力+强剪枝应该能过。具体是在求得K个数的时候,先用2^k,判断一下在不排序的情况下能否更新答案,如果不能那么排序以后一定也不能,由于数据随机,所以可以剪掉很多情况。由于是一个环,所以(k-1)!就够了。敲代码的时候注意细节问题,尽量优化常数,这题就能过了。代码如下:
#include<iostream>#include<stdio.h>#include<algorithm>#include<string.h>#include<math.h>#define nn 110000#define inff 0x3fffffffusing namespace std;typedef long long LL;int n,k,l;int a[30];int b[10];int tem[10];bool use[210];int ans;bool check(){ int i,j,ix; for(i=l;i<=l+k*(k-1)+1;i++) use[i]=false; for(i=0;i<(1<<k);i++) { ix=0; for(j=0;j<k;j++) { if(i&(1<<j)) { ix=ix^b[j]; } } use[ix]=true; } for(i=l;i<=l+ans-1;i++) { if(!use[i]) return false; } return true;}void solve(){ int i,j,ix; if(check()) { for(i=0;i<k;i++) tem[i]=b[i]; do { for(i=l;i<=l+k*(k-1)+1;i++) use[i]=false; for(i=0;i<k;i++) { ix=0; for(j=i;j<i+k;j++) { ix=ix^tem[j%k]; use[ix]=true; } } for(i=l;i<=l+k*(k-1)+1;i++) { if(!use[i]) { ans=max(ans,i-l); break; } } } while(next_permutation(tem+1,tem+k)); }}void dfs(int id,int num){ if(num==k) { solve(); return ; } if(id>=n||n-id<k-num) return ; dfs(id+1,num); b[num]=a[id]; dfs(id+1,num+1);}int main(){ int i; while(scanf("%d%d%d",&n,&k,&l)!=EOF) { for(i=0;i<n;i++) scanf("%d",&a[i]); sort(a,a+n); ans=0; dfs(0,0); if(ans) printf("%d\n",l+ans-1); else puts("0"); } return 0;}
0 0
- hdu 4876 ZCC loves cards(暴搜+剪枝)
- HDU 4876 ZCC loves cards(暴力剪枝)
- hdu 4876 ZCC loves cards (搜索+剪枝)
- hdu 4876 ZCC loves cards(暴力+剪枝)
- HDU 4876 ZCC loves cards【暴力+深搜+剪枝】
- HDU 4876 ZCC loves cards
- HDU-4876-ZCC loves cards
- hdu 4876 ZCC loves cards
- HDU 4876 ZCC loves cards
- HDU 4876 ZCC loves cards
- HDU 4876 ZCC loves cards (2014多校联合训练第二场1005) 解题报告(暴力+剪枝)
- 【HDU 4876多校】ZCC loves cards【搜索】
- hdu 4876 ZCC loves cards(暴力)
- 【HDU】4876 ZCC loves cards 暴力
- hdu 4876 ZCC loves cards 多校第二场 暴力+剪枝
- HDOJ 4876 ZCC loves cards
- HDu 4876(2014多校第二场 ZCC loves cards)
- [HDOJ 4876] ZCC loves cards [搜索]
- Codeforces 344B Simple Molecules
- pku 2992 因子和因子个数的应用
- 数据库索引创建规则
- 删除结点 (双向链表)
- android onTouchEvent 左右手势滑动事件处理
- hdu 4876 ZCC loves cards(暴搜+剪枝)
- 【python 数据结构 1:排序】冒泡排序和快速排序
- 数据包协议结构,粘包问题,半包问题,缝包 C++ 实现
- vs2010sp1 安装停滞在KB983509处长时间无进展的解决办法
- 数学基础_1: 级数
- 汇编——学生成绩管理系统
- 机房收费系统之设置文件夹为完全控制
- Oracle创建表、约束、视图、索引、序列、同义词、表空间
- AC自动机算法