import java.io.*;
import java.util.*;
import java.math.*;
//3/25/12 12:46 PM
//by Abrackadabra
public class B {
long X;
class Segment implements Comparable
<Segment
> { @Override
if (hash < o.hash) return -1;
if (hash > o.hash) return 1;
if (first < o.first) return -1;
if (first > o.first) return 1;
return 0;
}
int n;
long first;
this.n = n;
this.first = first;
hash = first ^ X;
}
long hash;
}
if (args.length > 0 && args[0].equals("Abra")) debugMode = true;
new B().run();
}
int IOMode = 0; //0 - consoleIO, 1 - <taskName>.in/out, 2 - input.txt/output.txt, 3 - test case generator
ArrayList<Segment> list =new ArrayList<Segment>();
void getSegments(long L, long R, int lev){
if(R<L)
return;
if(L % 2 == 1){
list.
add(new Segment(lev, L
<< lev
)); getSegments(L+1, R,lev);
return;
}
if(R % 2 == 0){
list.
add(new Segment(lev, R
<< lev
)); getSegments(L, R - 1,lev);
return;
}
getSegments(L>>1,R>>1, lev + 1);
}
long L=nextLong();
long R=nextLong();
X=nextLong();
long K=nextLong();
getSegments(L, R, 0);
if((1L << seg.n) < K){
K-=(1L<<seg.n);
continue;
}
out.print((((1L<<seg.n)-1 & X)^(K-1)) + seg.first);
return;
}
}
long startTime
= System.
nanoTime(), tempTime
= startTime, finishTime
= startTime
; long startMem
= Runtime.
getRuntime().
totalMemory(), finishMem
= startMem
;
init();
if (debugMode) {
con.println("Start");
con.println("Console:");
}
solve();
finishTime
= System.
nanoTime(); finishMem
= Runtime.
getRuntime().
totalMemory(); out.flush();
if (debugMode) {
int maxSymbols = 1000;
char[] a = new char[maxSymbols];
tbr.read(a);
if (a[0] != 0) {
con.println("File input");
con.println(a);
if (a[maxSymbols - 1] != 0) con.println("...");
}
a = new char[maxSymbols];
tbr.read(a);
if (a[0] != 0) {
con.println("File output");
con.println(a);
if (a[maxSymbols - 1] != 0) con.println("...");
}
con.println("Execution time: " + (finishTime - startTime) / 1000000000.0 + " sec");
con.println("Used memory: " + (finishMem - startMem) + " bytes");
con.
println("Total memory: " + Runtime.
getRuntime().
totalMemory() + " bytes"); }
}
boolean tick(double x) {
if (System.
nanoTime() - tempTime
> x
* 1e9
) { con.println("Tick at " + (tempTime - startTime) / 1000000000 + " sec");
con.print(" ");
return true;
}
return false;
}
static boolean debugMode = false;
if (debugMode && IOMode != 3) {
} else
switch (IOMode) {
case 0:
break;
case 1:
break;
case 2:
break;
case 3:
break;
}
}
while (in == null || !in.hasMoreTokens()) {
if (line == null) return false;
}
return true;
}
return hasMoreTokens() ? in.nextToken() : null;
}
return Integer.
parseInt(nextString
()); }
return Long.
parseLong(nextString
()); }
return Double.
parseDouble(nextString
()); }
}
aW1wb3J0IGphdmEuaW8uKjsKaW1wb3J0IGphdmEudXRpbC4qOwppbXBvcnQgamF2YS5tYXRoLio7CgovLzMvMjUvMTIgMTI6NDYgUE0KLy9ieSBBYnJhY2thZGFicmEKCnB1YmxpYyBjbGFzcyBCIHsKICAgIGxvbmcgWDsKICAgIGNsYXNzIFNlZ21lbnQgaW1wbGVtZW50cyBDb21wYXJhYmxlPFNlZ21lbnQ+IHsKICAgICAgICBAT3ZlcnJpZGUKICAgICAgICBwdWJsaWMgaW50IGNvbXBhcmVUbyhTZWdtZW50IG8pIHsKICAgICAgICAgICAgaWYgKGhhc2ggPCBvLmhhc2gpIHJldHVybiAtMTsKICAgICAgICAgICAgaWYgKGhhc2ggPiBvLmhhc2gpIHJldHVybiAxOwogICAgICAgICAgICBpZiAoZmlyc3QgPCBvLmZpcnN0KSByZXR1cm4gLTE7CiAgICAgICAgICAgIGlmIChmaXJzdCA+IG8uZmlyc3QpIHJldHVybiAxOwogICAgICAgICAgICByZXR1cm4gMDsKICAgICAgICB9CgogICAgICAgIGludCBuOwogICAgICAgIGxvbmcgZmlyc3Q7CgogICAgICAgIFNlZ21lbnQoaW50IG4sIGxvbmcgZmlyc3QpIHsKICAgICAgICAgICAgdGhpcy5uID0gbjsKICAgICAgICAgICAgdGhpcy5maXJzdCA9IGZpcnN0OwogICAgICAgICAgICBoYXNoID0gZmlyc3QgXiBYOwogICAgICAgIH0KCiAgICAgICAgbG9uZyBoYXNoOwogICAgfQogICAgcHVibGljIHN0YXRpYyB2b2lkIG1haW4oU3RyaW5nW10gYXJncykgdGhyb3dzIElPRXhjZXB0aW9uIHsKICAgICAgICBpZiAoYXJncy5sZW5ndGggPiAwICYmIGFyZ3NbMF0uZXF1YWxzKCJBYnJhIikpIGRlYnVnTW9kZSA9IHRydWU7CiAgICAgICAgbmV3IEIoKS5ydW4oKTsKICAgIH0KCiAgICBpbnQgSU9Nb2RlID0gMDsgLy8wIC0gY29uc29sZUlPLCAxIC0gPHRhc2tOYW1lPi5pbi9vdXQsIDIgLSBpbnB1dC50eHQvb3V0cHV0LnR4dCwgMyAtIHRlc3QgY2FzZSBnZW5lcmF0b3IKICAgIFN0cmluZyB0YXNrTmFtZSA9ICIiOwogICAgQXJyYXlMaXN0PFNlZ21lbnQ+IGxpc3QgPW5ldyBBcnJheUxpc3Q8U2VnbWVudD4oKTsKICAgIHZvaWQgZ2V0U2VnbWVudHMobG9uZyBMLCBsb25nIFIsIGludCBsZXYpewogICAgICAgIGlmKFI8TCkKICAgICAgICAgICAgcmV0dXJuOwogICAgICAgIGlmKEwgJSAyID09IDEpewogICAgICAgICAgICBsaXN0LmFkZChuZXcgU2VnbWVudChsZXYsIEwgPDwgbGV2KSk7CiAgICAgICAgICAgIGdldFNlZ21lbnRzKEwrMSwgUixsZXYpOwogICAgICAgICAgICByZXR1cm47CiAgICAgICAgfQogICAgICAgIGlmKFIgJSAyID09IDApewogICAgICAgICAgICBsaXN0LmFkZChuZXcgU2VnbWVudChsZXYsIFIgPDwgbGV2KSk7CiAgICAgICAgICAgIGdldFNlZ21lbnRzKEwsIFIgLSAxLGxldik7CiAgICAgICAgICAgIHJldHVybjsKICAgICAgICB9CiAgICAgICAgZ2V0U2VnbWVudHMoTD4+MSxSPj4xLCBsZXYgKyAxKTsKCiAgICB9CiAgICB2b2lkIHNvbHZlKCkgdGhyb3dzIElPRXhjZXB0aW9uIHsKICAgICAgICBsb25nIEw9bmV4dExvbmcoKTsKICAgICAgICBsb25nIFI9bmV4dExvbmcoKTsKICAgICAgICBYPW5leHRMb25nKCk7CiAgICAgICAgbG9uZyBLPW5leHRMb25nKCk7CgogICAgICAgIGdldFNlZ21lbnRzKEwsIFIsIDApOwogICAgICAgIENvbGxlY3Rpb25zLnNvcnQobGlzdCk7CgogICAgICAgIGZvcihTZWdtZW50IHNlZyA6IGxpc3QpewogICAgICAgICAgICBpZigoMUwgPDwgc2VnLm4pIDwgSyl7CiAgICAgICAgICAgICAgICBLLT0oMUw8PHNlZy5uKTsKICAgICAgICAgICAgICAgIGNvbnRpbnVlOwogICAgICAgICAgICB9CiAgICAgICAgICAgIG91dC5wcmludCgoKCgxTDw8c2VnLm4pLTEgJiBYKV4oSy0xKSkgKyBzZWcuZmlyc3QpOwogICAgICAgICAgICByZXR1cm47CiAgICAgICAgfQogICAgfQoKICAgIGxvbmcgc3RhcnRUaW1lID0gU3lzdGVtLm5hbm9UaW1lKCksIHRlbXBUaW1lID0gc3RhcnRUaW1lLCBmaW5pc2hUaW1lID0gc3RhcnRUaW1lOwogICAgbG9uZyBzdGFydE1lbSA9IFJ1bnRpbWUuZ2V0UnVudGltZSgpLnRvdGFsTWVtb3J5KCksIGZpbmlzaE1lbSA9IHN0YXJ0TWVtOwoKICAgIHZvaWQgcnVuKCkgdGhyb3dzIElPRXhjZXB0aW9uIHsKICAgICAgICBpbml0KCk7CiAgICAgICAgaWYgKGRlYnVnTW9kZSkgewogICAgICAgICAgICBjb24ucHJpbnRsbigiU3RhcnQiKTsKICAgICAgICAgICAgY29uLnByaW50bG4oIkNvbnNvbGU6Iik7CiAgICAgICAgfQogICAgICAgIHNvbHZlKCk7CiAgICAgICAgZmluaXNoVGltZSA9IFN5c3RlbS5uYW5vVGltZSgpOwogICAgICAgIGZpbmlzaE1lbSA9IFJ1bnRpbWUuZ2V0UnVudGltZSgpLnRvdGFsTWVtb3J5KCk7CiAgICAgICAgb3V0LmZsdXNoKCk7CiAgICAgICAgaWYgKGRlYnVnTW9kZSkgewogICAgICAgICAgICBpbnQgbWF4U3ltYm9scyA9IDEwMDA7CiAgICAgICAgICAgIEJ1ZmZlcmVkUmVhZGVyIHRiciA9IG5ldyBCdWZmZXJlZFJlYWRlcihuZXcgRmlsZVJlYWRlcigiaW5wdXQudHh0IikpOwogICAgICAgICAgICBjaGFyW10gYSA9IG5ldyBjaGFyW21heFN5bWJvbHNdOwogICAgICAgICAgICB0YnIucmVhZChhKTsKICAgICAgICAgICAgaWYgKGFbMF0gIT0gMCkgewogICAgICAgICAgICAgICAgY29uLnByaW50bG4oIkZpbGUgaW5wdXQiKTsKICAgICAgICAgICAgICAgIGNvbi5wcmludGxuKGEpOwogICAgICAgICAgICAgICAgaWYgKGFbbWF4U3ltYm9scyAtIDFdICE9IDApIGNvbi5wcmludGxuKCIuLi4iKTsKICAgICAgICAgICAgfQogICAgICAgICAgICB0YnIgPSBuZXcgQnVmZmVyZWRSZWFkZXIobmV3IEZpbGVSZWFkZXIoIm91dHB1dC50eHQiKSk7CiAgICAgICAgICAgIGEgPSBuZXcgY2hhclttYXhTeW1ib2xzXTsKICAgICAgICAgICAgdGJyLnJlYWQoYSk7CiAgICAgICAgICAgIGlmIChhWzBdICE9IDApIHsKICAgICAgICAgICAgICAgIGNvbi5wcmludGxuKCJGaWxlIG91dHB1dCIpOwogICAgICAgICAgICAgICAgY29uLnByaW50bG4oYSk7CiAgICAgICAgICAgICAgICBpZiAoYVttYXhTeW1ib2xzIC0gMV0gIT0gMCkgY29uLnByaW50bG4oIi4uLiIpOwogICAgICAgICAgICB9CiAgICAgICAgICAgIGNvbi5wcmludGxuKCJFeGVjdXRpb24gdGltZTogIiArIChmaW5pc2hUaW1lIC0gc3RhcnRUaW1lKSAvIDEwMDAwMDAwMDAuMCArICIgc2VjIik7CiAgICAgICAgICAgIGNvbi5wcmludGxuKCJVc2VkIG1lbW9yeTogICAgIiArIChmaW5pc2hNZW0gLSBzdGFydE1lbSkgKyAiIGJ5dGVzIik7CiAgICAgICAgICAgIGNvbi5wcmludGxuKCJUb3RhbCBtZW1vcnk6ICAgIiArIFJ1bnRpbWUuZ2V0UnVudGltZSgpLnRvdGFsTWVtb3J5KCkgKyAiIGJ5dGVzIik7CiAgICAgICAgfQogICAgfQoKICAgIGJvb2xlYW4gdGljayhkb3VibGUgeCkgewogICAgICAgIGlmIChTeXN0ZW0ubmFub1RpbWUoKSAtIHRlbXBUaW1lID4geCAqIDFlOSkgewogICAgICAgICAgICB0ZW1wVGltZSA9IFN5c3RlbS5uYW5vVGltZSgpOwogICAgICAgICAgICBjb24ucHJpbnRsbigiVGljayBhdCAiICsgKHRlbXBUaW1lIC0gc3RhcnRUaW1lKSAvIDEwMDAwMDAwMDAgKyAiIHNlYyIpOwogICAgICAgICAgICBjb24ucHJpbnQoIiAgICIpOwogICAgICAgICAgICByZXR1cm4gdHJ1ZTsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIGZhbHNlOwogICAgfQoKICAgIHN0YXRpYyBib29sZWFuIGRlYnVnTW9kZSA9IGZhbHNlOwogICAgUHJpbnRTdHJlYW0gY29uID0gU3lzdGVtLm91dDsKCiAgICB2b2lkIGluaXQoKSB0aHJvd3MgSU9FeGNlcHRpb24gewogICAgICAgIGlmIChkZWJ1Z01vZGUgJiYgSU9Nb2RlICE9IDMpIHsKICAgICAgICAgICAgYnIgPSBuZXcgQnVmZmVyZWRSZWFkZXIobmV3IEZpbGVSZWFkZXIoImlucHV0LnR4dCIpKTsKICAgICAgICAgICAgb3V0ID0gbmV3IFByaW50V3JpdGVyKG5ldyBGaWxlV3JpdGVyKCJvdXRwdXQudHh0IikpOwogICAgICAgIH0gZWxzZQogICAgICAgICAgICBzd2l0Y2ggKElPTW9kZSkgewogICAgICAgICAgICAgICAgY2FzZSAwOgogICAgICAgICAgICAgICAgICAgIGJyID0gbmV3IEJ1ZmZlcmVkUmVhZGVyKG5ldyBJbnB1dFN0cmVhbVJlYWRlcihTeXN0ZW0uaW4pKTsKICAgICAgICAgICAgICAgICAgICBvdXQgPSBuZXcgUHJpbnRXcml0ZXIoU3lzdGVtLm91dCk7CiAgICAgICAgICAgICAgICAgICAgYnJlYWs7CiAgICAgICAgICAgICAgICBjYXNlIDE6CiAgICAgICAgICAgICAgICAgICAgYnIgPSBuZXcgQnVmZmVyZWRSZWFkZXIobmV3IEZpbGVSZWFkZXIodGFza05hbWUgKyAiLmluIikpOwogICAgICAgICAgICAgICAgICAgIG91dCA9IG5ldyBQcmludFdyaXRlcihuZXcgRmlsZVdyaXRlcih0YXNrTmFtZSArICIub3V0IikpOwogICAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgY2FzZSAyOgogICAgICAgICAgICAgICAgICAgIGJyID0gbmV3IEJ1ZmZlcmVkUmVhZGVyKG5ldyBGaWxlUmVhZGVyKCJpbnB1dC50eHQiKSk7CiAgICAgICAgICAgICAgICAgICAgb3V0ID0gbmV3IFByaW50V3JpdGVyKG5ldyBGaWxlV3JpdGVyKCJvdXRwdXQudHh0IikpOwogICAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICAgICAgY2FzZSAzOgogICAgICAgICAgICAgICAgICAgIG91dCA9IG5ldyBQcmludFdyaXRlcihuZXcgRmlsZVdyaXRlcigiaW5wdXQudHh0IikpOwogICAgICAgICAgICAgICAgICAgIGJyZWFrOwogICAgICAgICAgICB9CiAgICB9CgogICAgQnVmZmVyZWRSZWFkZXIgYnI7CiAgICBQcmludFdyaXRlciBvdXQ7CiAgICBTdHJpbmdUb2tlbml6ZXIgaW47CgogICAgYm9vbGVhbiBoYXNNb3JlVG9rZW5zKCkgdGhyb3dzIElPRXhjZXB0aW9uIHsKICAgICAgICB3aGlsZSAoaW4gPT0gbnVsbCB8fCAhaW4uaGFzTW9yZVRva2VucygpKSB7CiAgICAgICAgICAgIFN0cmluZyBsaW5lID0gYnIucmVhZExpbmUoKTsKICAgICAgICAgICAgaWYgKGxpbmUgPT0gbnVsbCkgcmV0dXJuIGZhbHNlOwogICAgICAgICAgICBpbiA9IG5ldyBTdHJpbmdUb2tlbml6ZXIobGluZSk7CiAgICAgICAgfQogICAgICAgIHJldHVybiB0cnVlOwogICAgfQoKICAgIFN0cmluZyBuZXh0U3RyaW5nKCkgdGhyb3dzIElPRXhjZXB0aW9uIHsKICAgICAgICByZXR1cm4gaGFzTW9yZVRva2VucygpID8gaW4ubmV4dFRva2VuKCkgOiBudWxsOwogICAgfQoKICAgIGludCBuZXh0SW50KCkgdGhyb3dzIElPRXhjZXB0aW9uIHsKICAgICAgICByZXR1cm4gSW50ZWdlci5wYXJzZUludChuZXh0U3RyaW5nKCkpOwogICAgfQoKICAgIGxvbmcgbmV4dExvbmcoKSB0aHJvd3MgSU9FeGNlcHRpb24gewogICAgICAgIHJldHVybiBMb25nLnBhcnNlTG9uZyhuZXh0U3RyaW5nKCkpOwogICAgfQoKICAgIGRvdWJsZSBuZXh0RG91YmxlKCkgdGhyb3dzIElPRXhjZXB0aW9uIHsKICAgICAgICByZXR1cm4gRG91YmxlLnBhcnNlRG91YmxlKG5leHRTdHJpbmcoKSk7CiAgICB9Cn0=