Sunday, December 4, 2016

LightOJ - 1042 - Secret Origins

This is the tale of Zephyr, the greatest time traveler the world will never know. Even those who are aware of Zephyr's existence know very little about her. For example, no one has any clue as to which time period she is originally from.
But we do know the story of the first time she set out to chart her own path in the time stream. Zephyr had just finished building her time machine which she named - "Dokhina Batash". She was making the final adjustments for her first trip when she noticed that a vital program was not working correctly. The program was supposed to take a number N, and find what Zephyr called its Onoroy value.
The Onoroy value of an integer N is the number of ones in its binary representation. For example, the number 13 (11012) has an Onoroy value of 3. Needless to say, this was an easy problem for the great mind of Zephyr. She solved it quickly, and was on her way.
You are now given a similar task. Find the first number after N which has the same Onoroy value as N.

Input

Input starts with an integer T (≤ 65), denoting the number of test cases.
Each case begins with an integer N (1 ≤ N ≤ 109).

Output

For each case of input you have to print the case number and the desired result.

Sample Input

Output for Sample Input

5
23
14232
391
7
8
Case 1: 27
Case 2: 14241
Case 3: 395
Case 4: 11
Case 5: 16

Solution:

  1. #include <bits/stdc++.h>
  2. #define pb push_back
  3. using namespace std;
  4. typedef long long LL;
  5. const int S = 100003;
  6. const int MOD = 1e9+7;
  7. const double pi = 2 * acos (0.0);
  8.  
  9. int _Int(){int x; scanf("%d"&x); return x;}
  10. LL _LLi(){LL x; scanf("%lld"&x); return x;}
  11. void pruts(){puts("-1");exit(EXIT_SUCCESS);}
  12.  
  13. int dirX[] = { 10-101-11-1 };
  14. int dirY[] = { 010-11-1-11 };
  15. int rX[] = { 1122-1-1-2-2 };
  16. int rY[] = { 2-21-12-21-1 };
  17.  
  18. template < class T > T tri_Area( T x1, T y1, T x2, T y2, T x3, T y3 ){ return abs( x1*( y2-y3 ) - y1*( x2-x3 ) + ( x2*y3-x3*y2 ) );};
  19. template < class T > T Distance( T x1, T y1, T x2, T y2 ){ return sqrt( ( x1-x2 ) * ( x1-x2 ) + ( y1-y2 ) * ( y1-y2 ) ); };
  20. template < class T > T bigMod( T n, T p, T m ){ if( p == 0 )return 1; if( p&1 )return ( n*bigMod( n, p-1, m ) )%m; T x = bigMod( n, p/2, m ); return ( x*)%m; };
  21.  
  22. int sset(int N, int pos){return N=N|(1<<pos);}
  23. bool check(int N, int pos){return (bool)(N&(1<<pos));}
  24. int reset(int N, int pos){return N=N&~(1<<pos);}
  25. /*******************###########################################********************
  26. ********************##   MD. YA-SEEN ARAFAT(ThunderStroke)   ##********************
  27. ********************##    CSE, University of Asia Pacific    ##********************
  28. ********************###########################################********************/
  29.  
  30. vector < int > V, temp;
  31.  
  32. void Love(){
  33.     int t = _Int(), cs = 0;
  34.     while( t-- ){
  35.         V.clear();
  36.         temp.clear();
  37.         int n = _Int();
  38.         int x = __builtin_popcount( n );
  39.         while( n ){
  40.             V.pb( n%2 );
  41.             n /= 2;
  42.         }
  43.         reverse( V.begin(), V.end() );
  44.         int sz = V.size();
  45.         if( sz == x ){
  46.             reverse( V.begin(), V.end() );
  47.             V.pop_back();
  48.             V.pb( 0 );
  49.             V.pb( 1 );
  50.             reverse( V.begin(), V.end() );
  51.         }
  52.         else{
  53.             for( int i = 0; i < sz; i++ ){
  54.                 if( V[ i ] == 1 )x--;
  55.                 else break;
  56.             }
  57.             if( !){
  58.                 V[ 0 ] = 0;
  59.                 temp.pb(1);
  60.                 for( int i = 0; i < sz; i++ )temp.pb( V[ i ] );
  61.                 V.clear();
  62.                 for( int i = 0; i <= sz; i++ )V.pb( temp[ i ] );
  63.                 int z = sz;
  64.                 for( int i = 2; ( i <= sz ) && ( i < z ); i++ ){
  65.                     if( V[ i ] )swap( V[ i ], V[ z-- ] );
  66.                     else break;
  67.                 }
  68.                 temp.clear();
  69.             }
  70.             else{
  71.                 for( int i = sz-2; i >= 0; i-- ){
  72.                     if( !V[ i ] && V[ i+1 ] ){
  73.                         swap( V[ i ], V[ i+1 ] );
  74.                         int z = sz-1;
  75.                         for( int j = i+2; ( j < sz ) && ( j < z ); j++ ){
  76.                             if( V[ j ] )swap( V[ j ], V[ z-- ] );
  77.                             else break;
  78.                         }
  79.                         break;
  80.                     }
  81.                 }
  82.             }
  83.         }
  84.         LL ans = 0LL;
  85.         sz = V.size();
  86.         for( int i = 0; i < sz; i++ )ans = ( ans*2LL ) + V[ i ];
  87.         printf("Case %d: %lld\n"++cs, ans);
  88.     }
  89. }
  90.  
  91. int main(){
  92.     #ifndef ONLINE_JUDGE
  93. //        freopen("in.txt", "r", stdin);
  94. //        freopen("n.txt", "w", stdout);
  95.     #endif
  96.     Love();
  97.     return 0;
  98. }
  99.  

No comments:

Post a Comment