#55007: 最快的解法 AC (2ms, 2.6MB)


benjaminshih1229@gmail.com (benjaminshih)


極致優化後的結果

 

 

#pragma GCC optimize("Ofast,unroll-loops")
#pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")

#include <stdio.h>
#include <unistd.h>

// 1MB 的快取區
#define BUF_SIZE 1048576

static char in_buf[BUF_SIZE];
static int in_pos = 0, in_len = 0;

static inline char get_char() {
    if (in_pos == in_len) {
        in_len = fread(in_buf, 1, BUF_SIZE, stdin);
        in_pos = 0;
        if (in_len == 0) return EOF;
    }
    return in_buf[in_pos++];
}

static inline int read_int() {
    int x = 0;
    char c = get_char();
    while (c < '0' || c > '9') {
        if (c == EOF) return -1;
        c = get_char();
    }
    while (c >= '0' && c <= '9') {
        x = x * 10 + (c - '0');
        c = get_char();
    }
    return x;
}

// 快速輸出:手動處理字串轉換
static char out_buf[BUF_SIZE];
static int out_pos = 0;

static inline void write_int(int x) {
    if (out_pos > BUF_SIZE - 20) { // 空間不足先刷出
        fwrite(out_buf, 1, out_pos, stdout);
        out_pos = 0;
    }
    char temp[10];
    int tp = 0;
    if (x == 0) temp[tp++] = '0';
    while (x > 0) {
        temp[tp++] = (x % 10) + '0';
        x /= 10;
    }
    while (tp--) out_buf[out_pos++] = temp[tp];
    out_buf[out_pos++] = '\n';
}

int main() {
    int t = read_int();
    if (t == -1) return 0;

    while (t--) {
        read_int(); // skip a
        int b = read_int();
        // __builtin_ctz 是目前最快的位元運算指令
        write_int(__builtin_ctz(b) + 1);
    }

    // 最後刷出輸出快取
    fwrite(out_buf, 1, out_pos, stdout);
    return 0;
}