hduacm 5255

http://acm.hdu.edu.cn/showproblem.php?pid=5255

 

枚举a和c  求解b

#include <cstdio>
#include <cstring>
#include <map>
#include <string>
#include <algorithm>
using namespace std ;

typedef long long LL ;

#define clr( a , x ) memset ( a , x , sizeof a )
#define ls ( o << 1 )
#define rs ( o << 1 | 1 )
#define lson ls , l , m
#define rson rs , m + 1 , r
#define mid ( ( l + r ) >> 1 )
#define root 1 , 1 , n

const int L = 1000000 ;

LL p[15] ;
LL S[L] , top ;

LL gcd ( LL a , LL b ) {
    return b ? gcd ( b , a % b ) : a ;
}

LL f ( LL pp , LL a , LL c , LL x1 , LL y1 ) {
    LL x = a * pp * x1 - c * pp * y1 + c * x1 - a * y1 ;
    LL y = ( y1 - x1 ) * 10 ;
    if ( x == 0 ) return 0 ;
    else if ( x > 0 && y < 0 || x < 0 && y > 0 ) return -1 ;
    else {
        if ( x % y ) return - 1 ;
        else return x / y ;
    }
}

char s[100] ;

void solve () {
    int x = 0 , y = 1 , loc = 0 ;
    top = 0 ;
    scanf ( "%s" , s ) ;
    int n = strlen ( s ) ;
    for ( int i = 0 ; i < n ; ++ i ) {
        if ( s[i] >= '0' ) {
            x = x * 10 + s[i] - '0' ;
            if ( loc ) y *= 10 ;
        } else loc = 1 ;
    }
    int g = gcd ( x , y ) ;
    int x1 = x / g ;
    int y1 = y / g ;
    for ( int i = 1 ; i <= 9 ; ++ i ) {
        for ( int a = 1 ; a <= 9 ; ++ a ) {
            for ( int c = 0 ; c <= 9 ; ++ c ) {
                LL b = f ( p[i] , a , c , x1 , y1 ) ;
                if ( b < 0 || b * 10 >= p[i] ) continue ;
                LL t = a * p[i] + b * 10 + c ;
                S[top ++] = t ;
            }
        }
    }
    printf ( "%d\n" , top ) ;
    sort ( S , S + top ) ;
    for ( int i = 0 ; i < top ; ++ i ) {
        printf ( "%I64d%c" , S[i] , i < top - 1 ? ' ' : '\n' ) ;
    }
}


int main () {
    int T ;
    p[0] = 1 ;
    for ( int i = 1 ; i <= 12 ; ++ i ) {
        p[i] = p[i - 1] * 10 ;
    }
    scanf ( "%d" , &T ) ;
    for ( int i = 1 ; i <= T ; ++ i ) {
        printf ( "Case #%d:\n" , i ) ;
        solve () ;
    }
    return 0 ;
}

 

posted on 2015-06-03 00:08  yifi  阅读(154)  评论(0编辑  收藏  举报

导航