Cod sursa(job #1034015)

Utilizator Teodor94Teodor Plop Teodor94 Data 17 noiembrie 2013 16:54:48
Problema Dtcsu Scor 0
Compilator cpp Status done
Runda Arhiva de probleme Marime 1.53 kb
#include <cstdio>
#include <vector>
#include <algorithm>

using namespace std;

#define TOTAL 276997
#define LEN 20
#define ll long long

char s[LEN];
vector< ll > v;
ll p2[6];
int exp[6] = { 1, 2, 4, 8, 16, 32 };

bool is_digit( char c ) {
    return c >= '0' && c <= '9';
}

ll get_int( char A[] ) {
    ll x = 0;
    int i = 0;
    while ( is_digit( A[i] ) ) {
        x = ( ll ) x * 10 + ( A[i] - '0' );
        ++i;
    }
    return x;
}

void read( FILE *fin ) {
    for ( int i = 0; i < TOTAL; ++i ) {
        fgets( s, LEN, fin );
        ll x = get_int( s );

        if ( x & 1 )
            v.push_back( x );
    }
}

void pregen() {
    p2[0] = 2;
    for ( int i = 1; i < 6; ++i )
        p2[i] = ( ll ) p2[i - 1] * p2[i - 1];
}

bool binary_search( ll x ) {
    int i, step = 1 << 17;
    for ( i = 0; step; step >>= 1 )
        if ( i + step < v.size() && v[i + step] <= x )
            i += step;
    return v[i] == x;
}

int main() {
    FILE *fin, *fout;

    fin = fopen( "dtcsu.in", "r" );
    fout = fopen( "dtcsu.out", "w" );

    read( fin );
    pregen();

    sort( v.begin(), v.end() );
    
    int q, ans = 0;
    fscanf( fin, "%d\n", &q );
    while ( q ) {
        fgets( s, LEN, fin );
        ll x = get_int( s );
        
        if ( !( x & 1 ) )
            for ( int i = 5; i >= 0; --i )
                if ( ( x & ( p2[i] - 1 ) ) == 0 )
                    x >>= exp[i];
        
        if ( binary_search( x ) )
            ++ans;

        --q;
    }
    
    fprintf( fout, "%d\n", ans );
    fclose( fin );
    fclose( fout );
}