HDU 5676(枚举 + 二分)

hdu 5676
题目大意:
已知幸运数的条件为其十进制的每一位都由 4 7 组成,且 4 7 的个数相同;
思路:
从小到大枚举每一位幸运数,二分寻找,特判大于 77..744..4(97,94) 的数字;

#include 
#include 
#include 
#include 
#include 
#define LL long long

using namespace std;

int cnt = 0;
int a[20];
LL b[68000];

void init()
{
    memset(b, 0, sizeof(b));

    for (int i = 1; i <= 9; i++)
    {

        for (int j = 0; j < i; j++)
        {
            a[j] = 4;
            a[j+i] = 7;  
        }

        do
        {
            LL sum = 0;
            for (int j = 0; j < 2 * i; j++)
            {
                sum = sum * 10 + a[j];
            }
            b[cnt++] = sum;

        } while (next_permutation(a, a + 2 * i));
    }
}

int main()
{
    init();

    int T;
    scanf("%d", &T);

    while (T--)
    {
        LL n;
        scanf("%lld", &n);

        if (n > 777777777444444444LL)
        {
            printf("44444444447777777777\n");
            continue;
        }

        printf("%lld\n", b[lower_bound(b, b + cnt, n) - b]);
    }

    return 0;
}

你可能感兴趣的:(hdu)