0w1

GCJ 2016 QR C. Coin Jam ( Ad hoc )

Dashboard - Qualification Round 2016 - Google Code Jam
There are several solutions, and mine is of the easier one. Since the input is fixed, 16 and 32, observing that both of them are even numbers, we will come up to that, given any number x in binary representation, concatenating it after itself, it will definitely be divisible by x. So let's talk about the case where N = 32, we will only have to enumerate the first J = 500 numbers that starts with 1 and ends in 1 and consists of 16 bits in binary representation.
There are other solutions interesting as well, here is a link:
pekempey.hatenablog.com
It uses the property where, when the sum of odd position digits are equal to the sum of even position digits, in x based representation, such number will be a multiple of x + 1. See the proof in the link above.

#include <bits/stdc++.h>
using namespace std;
const int MAXN = 32 + 1;
const int MAXJ = 500 + 5;
typedef long long ll;

int N, J;

ll valBase( int x, int b ){
    ll res = 0;
    ll fac = 1;
    while( x ){
        if( x & 1 )
            res += fac;
        x >>= 1;
        fac *= b;
    }
    return res;
}

void print( int x ){
    x <<= 1, x |= 1;
    x |= ( 1 << N / 2 - 1 );
    for( int t = 0; t < 2; ++t )
        for( int i = N / 2 - 1; i >= 0; --i )
            printf( "%d", ( x >> i ) & 1 );
    for( int i = 2; i <= 10; ++i ){
        ll val = valBase( x, i );
        printf(" %lld", val);
    }
    puts( "" );
}

void solve(){
    for( int i = 0; i < ( 1 << N / 2 - 2 ); ++i ){
        print( i );
        if( --J == 0 ) break;
    }
}

int main(){
    int T; scanf("%d", &T);
    for( int kase = 1; kase <= T; ++kase ){
        printf("Case #%d:\n", kase);
        scanf("%d%d", &N, &J);
        solve();
    }
    return 0;
}