Saturday, January 4, 2014

codechef DRAGNXOR - "open the dragon scroll" guidance and solution

codechef DRAGNXOR - "open the dragon scroll": http://www.codechef.com/problems/DRAGNXOR

codechef DRAGNXOR - "open the dragon scroll" solution: http://ideone.com/gwFWnw

#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;

int main() {
// your code goes here
int t, n, a, b, at, bt, ac, bc, total, r;
scanf("%d", &t);
while(t--) {
scanf("%d %d %d", &n, &a, &b);
at=0; bt=0; ac=0; bc=0; r=0;
while(a) { if(a%2) ac++; at=at*10+a%2; a=a/2; }
while(b) { if(b%2) bc++; bt=bt*10+b%2; b=b/2; }
total=ac+bc;
if(total<n);
else total=2*n-(ac+bc);
while(total--) { r=r+pow(2, (n-1)); n--; }
printf("%d\n", r);
}
return 0;
}


codechef DRAGNXOR - "open the dragon scroll" guidance: 

after all, you have to know how many "1" in total of "a" and "b". in order to get maximum number, starting from leftmost value, put "1", from rightmost value put "0", if any "0" or "1" && "1" exist. if total "1" in "a" and "b" exceed "n"(number of digits), then put "0" starting from rightmost value. change number from binary representation to decimal.  

No comments:

Post a Comment