lets denote the i-th element of the set S as si; value
must be as small as pZ??http://www.2cto.com/kf/ware/vc/" target="_blank" class="keylink">vc3NpYmxlLgo8cD4KSGVscCBWaWN0b3IgZmluZCB0aGUgZGVzY3JpYmVkIHNldC48L3A+CgoKCklucHV0CjxwPgpUaGUgZmlyc3QgbGluZSBjb250YWlucyB0aHJlZSBzcGFjZS1zZXBhcmF0ZWQgaW50ZWdlcnMgPGVtPmw8L2VtPiw/PGVtPnI8L2VtPiw/PGVtPms8L2VtPiAoMT+h3D88ZW0+bDwvZW0+P6HcPzxlbT5yPC9lbT4/odw/MTAxMjsgMT+h3D88ZW0+azwvZW0+P6HcPzxlbT5taW48L2VtPigxMDYsPzxlbT5yPC9lbT4/LT88ZW0+bDwvZW0+PyYjNDM7PzEpKS48L3A+CgoKCk91dHB1dAo8cD4KUHJpbnQgdGhlIG1pbmltdW0gcG9zc2libGUgdmFsdWUgb2YgPGVtPmY8L2VtPig8ZW0+UzwvZW0+KS4gVGhlbiBwcmludCB0aGUgY2FyZGluYWxpdHkgb2Ygc2V0IA=="S|. Then print the elements of the set in any order. If there are multiple optimal sets, you can print any of them.
Sample test(s) input 8 15 3
output 1
2
10 11
input 8 30 7
output 0
5
14 9 28 11 16
Note Operation
represents the operation of bitwise exclusive OR. In other words, it is the XOR operation.
【题意】给定范围L和R,在这之间选P个不同的自然数,其中1<=P<=k,求选出的数最小异或和及某个方案。
【分析】很显然的结论,K^(K+1)=1,其中K是偶数。当K>3时,我们可以选连续的4个自然数使异或和为0。(当然注意要特判R-L+1的大小)。当K=1时,就是L。当K=2时,显然只能构造异或为1的情况。
所有的推论都指向一个问题:当K=3的一般情况怎么做?
【题解】对于那个情况,我一直觉得能贪心构造,但是怎么也想不出简单易行且效率高的算法。
其实很简单。我们设L<=X
在二进制中,异或和为0的情况是1,1,0或0,0,0。显然Z的第一位是1,然后X和Y是0。
因为是贪心,我们要尽量使Y靠近Z(因为如果Z符合范围,Y显然越大越好)。
那么第二位我们就让Y靠近Z。我们把Z那位设成0,X和Y都设成1,即如下形式:
110000000
101111111
011111111
当然脑补可能会萎... 为了少特判,我在R-L+1小的时候直接暴力寻找。
【代码】
#include
#include
#include
#define E endl #define INF 999999999999999ll #define RE return 0 using namespace std; typedef long long LL; LL len,sum,ans,C,wri[15],temp[15],i,S,L,R,k,x,z; inline void DFS(LL now,LL C,LL sum) { if (now==R+1) { if (sum>=ans||!C) return;len=C;ans=sum; for (int i=1;i<=C;i++) wri[i]=temp[i]; return; } if (now>R) return; DFS(now+1,C,sum);if (C+1>k) return; temp[C+1]=now;DFS(now+1,C+1,sum^now); } int main() { cin>>L>>R>>k; if (L==R) {cout<
3) { S=(L&1)?L+1:L; cout<<0<
=L) {cout<<0<