HDU 4876 ZCC loves cards

来源:互联网 发布:c语言图书管理系统 编辑:程序博客网 时间:2024/05/16 08:58
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 several consecutive 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,RL,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 aii satisfies 1≤aii≤100. 
 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
7暴力枚举C(n,k)的组合,对于组合内的选择,做一次2^k的剪枝,不行就暴力k!
#include<map>#include<set>#include<ctime>#include<cmath>#include<queue>#include<string>#include<vector>#include<cstdio>#include<cstring>#include<iostream>#include<algorithm>#include<functional>using namespace std;#define ms(x,y) memset(x,y,sizeof(x))#define rep(i,j,k) for(int i=j;i<=k;i++)#define per(i,j,k) for(int i=j;i>=k;i--)#define loop(i,j,k) for (int i=j;i!=-1;i=k[i])#define inone(x) scanf("%d",&x)#define intwo(x,y) scanf("%d%d",&x,&y)#define inthr(x,y,z) scanf("%d%d%d",&x,&y,&z)#define infou(x,y,z,p) scanf("%d%d%d%d",&x,&y,&z,&p)#define lson x<<1,l,mid#define rson x<<1|1,mid+1,r#define mp(i,j) make_pair(i,j)#define ft first#define sd secondtypedef long long LL;typedef pair<int, int> pii;const int low(int x) { return x&-x; }const int INF = 0x7FFFFFFF;const int mod = 1e9 + 7;const int N = 1e5 + 10;const double eps = 1e-10;int n,m,L,a[N],b[N],c[N],d[N],cnt,R;void check(){    ++cnt;    rep(i,1,(1<<m)-1)    {        int now=0;        rep(j,0,m-1) if (i&(1<<j)) now^=b[j];        c[now]=cnt;    }    int G=0;    for (int i=L;c[i]==cnt;i++) G=max(G,i);    if (R>=G) return;    rep(i,0,m-1) d[i]=b[i];    do    {        ++cnt;        rep(i,0,m-1)        {            int now=0;            rep(j,0,m-1)            {                c[now^=d[(i+j)%m]]=cnt;            }        }        for (int i=L;c[i]==cnt;i++) R=max(R,i);    }while (next_permutation(d,d+m));}void dfs(int x,int y){    if (y==m) { check(); return;}    if (x>n) return;    dfs(x+1,y);    b[y]=a[x];    dfs(x+1,y+1);}int main(){    cnt=0;    while (~inthr(n,m,L))    {        rep(i,1,n) inone(a[i]);        sort(a+1,a+n+1);        R=0; dfs(1,0);        printf("%d\n",R);    }    return 0;}

0 0
原创粉丝点击