HDU 4876 ZCC loves cards

来源:互联网 发布:上海西服定制 知乎 编辑:程序博客网 时间:2024/04/29 18:16

ZCC loves cards

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 992    Accepted Submission(s): 241


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 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, but once 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.
 

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.
 

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
Hint
⊕ means xor
 

Author
镇海中学
 

Source
2014 Multi-University Training Contest 2
 


官方思路:


我觉得已经说得很清楚了。。

某神犇的解题报告  http://blog.csdn.net/sf____/article/details/38097159

比赛时想到了C(n,k),但是一想对于每组K个数全排列肯定超时,然后我就yy了一个结论。。。对K个数从小到大排序。。伤。。


#include <cstdlib>#include <cctype>#include <cstring>#include <cstdio>#include <cmath>#include <algorithm>#include <vector>#include <string>#include <iostream>#include <sstream>#include <map>#include <set>#include <queue>#include <stack>#include <fstream>#include <numeric>#include <iomanip>#include <bitset>#include <list>#include <stdexcept>#include <functional>#include <utility>#include <ctime>using namespace std;vector<int> s;int a[25];int b[25];int n,k,l,id;int ans;int vis[129];int pos[256];int dp[9];bool check(){    int tot=0;    pos[tot++]=0;    for(int i=0;i<k;i++)    {        int t=tot;        for(int j=0;j<tot;j++)            pos[t++]=pos[j]^b[i];        tot=t;    }    ++id;    for(int i=0;i<tot;i++)        vis[pos[i]]=id;    for(int i=l;i<=ans+1;i++)        if(vis[i]!=id)         return false;    return true;}void solve(int x){    if(s.size()==k)    {        memset(b,0,sizeof(b));        memset(vis,0,sizeof(vis));        int g=0;        for(vector<int>::iterator iter=s.begin();iter!=s.end();iter++)        {           //printf("%d ",a[*iter]);           b[g++]=a[*iter];        }        //cout<<g<<endl;        if(check()==false)            return;        sort(b,b+k);        do{             ++id;             memset(dp,0,sizeof(dp));             int m=0;             int i,j;             for(int p=0;p<k;p++)             {                 i=m,j=0;                 while(1)                {                   dp[j]^=b[i];                   vis[dp[j]]=id;                   i++; j++;                   if(i>=k)i=0;                   if(i==m)break;                 }                 m++;             }            if(vis[l]!=id) continue;            int r=l;            while(vis[r+1]==id)                r++;            ans=max(ans,r);        }while(next_permutation(b+1,b+k));       // printf("\n");        return;    }    for(int i=x;i<=n;i++)    {        s.push_back(i);        solve(i+1);        s.pop_back();    }}int main(){    id=100;    while(scanf("%d%d%d",&n,&k,&l)!=EOF)    {        ans=0;        s.clear();        for(int i=1;i<=n;i++)        {            scanf("%d",&a[i]);        }        solve(1);        cout<<ans<<endl;    }    return 0;}


0 0
原创粉丝点击