提交记录 30388


用户 题目 状态 得分 用时 内存 语言 代码长度
saffah_codex_260812 1002i. 【模板题】多项式乘法 Accepted 100 26.598 ms 3592 KB C 7.75 KB
提交时间 评测时间
2026-08-12 21:18:52 2026-08-12 21:19:48
#ifndef DUCK_FASTIO_H
#define DUCK_FASTIO_H

typedef unsigned long duck_u64;
typedef long duck_i64;

typedef struct {
    duck_u64 abi_version;
    const char *stdin_ptr;
    duck_u64 stdin_size;
    char *stdout_ptr;
    duck_u64 stdout_limit;
    duck_u64 stdout_size;
    char *stderr_ptr;
    duck_u64 stderr_limit;
    duck_u64 stderr_size;
    const char *ib_ptr;
    duck_u64 ib_limit;
    char *ob_ptr;
    duck_u64 ob_limit;
    duck_u64 tsc_frequency;
} __attribute__((packed)) DuckInfo;

static __attribute__((always_inline)) inline DuckInfo *duck_info(long argc, char **argv) {
    char **p = argv + argc + 1;
    while (*p) ++p;
    duck_u64 *aux = (duck_u64 *)(p + 1);
    while (aux[0]) {
        if (aux[0] == 0x6b637564UL) return (DuckInfo *)aux[1];
        aux += 2;
    }
    return (DuckInfo *)0;
}

static __attribute__((always_inline)) inline duck_u64 duck_read_u64(const char **cursor) {
    const char *p = *cursor;
    while ((unsigned char)(*p - '0') > 9) ++p;
    duck_u64 value = 0;
    do {
        value = value * 10 + (unsigned char)(*p - '0');
        ++p;
    } while ((unsigned char)(*p - '0') <= 9);
    *cursor = p;
    return value;
}

static __attribute__((always_inline)) inline duck_i64 duck_read_i64(const char **cursor) {
    const char *p = *cursor;
    while (*p != '-' && (unsigned char)(*p - '0') > 9) ++p;
    int negative = *p == '-';
    p += negative;
    duck_u64 value = 0;
    do {
        value = value * 10 + (unsigned char)(*p - '0');
        ++p;
    } while ((unsigned char)(*p - '0') <= 9);
    *cursor = p;
    return negative ? -(duck_i64)value : (duck_i64)value;
}

static __attribute__((always_inline)) inline char *duck_write_u64(char *out, duck_u64 value) {
    char tmp[24];
    unsigned n = 0;
    do {
        tmp[n++] = (char)('0' + value % 10);
        value /= 10;
    } while (value);
    do *out++ = tmp[--n]; while (n);
    return out;
}

static __attribute__((always_inline)) inline char *duck_write_i64(char *out, duck_i64 value) {
    if (value < 0) {
        *out++ = '-';
        return duck_write_u64(out, (duck_u64)(-value));
    }
    return duck_write_u64(out, (duck_u64)value);
}

static __attribute__((always_inline, noreturn)) inline void duck_exit(void) {
    __asm__ volatile("mov $60,%%eax;xor %%edi,%%edi;syscall" ::: "rax", "rdi", "rcx", "r11", "memory");
    __builtin_unreachable();
}

#endif


typedef unsigned int u32;
typedef unsigned long u64;

enum { MOD = 998244353u, MAXL = 1 << 18 };
static u32 fa[MAXL], fb[MAXL];

static __attribute__((always_inline)) inline u32 mont_mul(u32 a, u32 b) {
    u64 t = (u64)a * b;
    u32 m = (u32)t * 0x3b7fffffu;
    return (u32)((t + (u64)m * MOD) >> 32);
}

static u32 mod_pow(u32 a, u32 e) {
    u64 r = 1;
    while (e) {
        if (e & 1) r = r * a % MOD;
        a = (u32)((u64)a * a % MOD);
        e >>= 1;
    }
    return (u32)r;
}

static void forward(u32 *a, int n) {
    const u32 mod2 = 2u * MOD;
    for (int len = n; len >= 2; len >>= 1) {
        int half = len >> 1;
        u32 step = mont_mul(mod_pow(3, (MOD - 1u) / (u32)len),
                            932051910u);
        for (int i = 0; i < n; i += len) {
            u32 w = 301989884u;
            for (int j = 0; j < half; ++j) {
                u32 x = a[i + j], y = a[i + j + half];
                u32 sum = x + y;
                if (sum >= mod2) sum -= mod2;
                u32 diff = x + mod2 - y;
                if (diff >= mod2) diff -= mod2;
                a[i + j] = sum;
                a[i + j + half] = mont_mul(diff, w);
                w = mont_mul(w, step);
            }
        }
    }
}

static void inverse(u32 *a, int n) {
    const u32 mod2 = 2u * MOD;
    for (int len = 2; len <= n; len <<= 1) {
        int half = len >> 1;
        u32 root = mod_pow(3, (MOD - 1u) / (u32)len);
        u32 step = mont_mul(mod_pow(root, MOD - 2u), 932051910u);
        for (int i = 0; i < n; i += len) {
            u32 w = 301989884u;
            for (int j = 0; j < half; ++j) {
                u32 x = a[i + j];
                u32 y = mont_mul(a[i + j + half], w);
                u32 sum = x + y;
                if (sum >= mod2) sum -= mod2;
                u32 diff = x + mod2 - y;
                if (diff >= mod2) diff -= mod2;
                a[i + j] = sum;
                a[i + j + half] = diff;
                w = mont_mul(w, step);
            }
        }
    }
    u32 inv_n = mod_pow((u32)n, MOD - 2u);
    for (int i = 0; i < n; ++i) {
        u32 x = mont_mul(a[i], inv_n);
        if (x >= MOD) x -= MOD;
        a[i] = x;
    }
}

static const char digit_pairs[201] =
    "00010203040506070809"
    "10111213141516171819"
    "20212223242526272829"
    "30313233343536373839"
    "40414243444546474849"
    "50515253545556575859"
    "60616263646566676869"
    "70717273747576777879"
    "80818283848586878889"
    "90919293949596979899";

static __attribute__((always_inline)) inline char *small(char *p, u32 x) {
    if (x >= 1000u) {
        u32 a = x / 100u;
        *(unsigned short *)p = *(const unsigned short *)(digit_pairs + 2 * a);
        *(unsigned short *)(p + 2) =
            *(const unsigned short *)(digit_pairs + 2 * (x - a * 100u));
        return p + 4;
    }
    if (x >= 100u) {
        u32 a = x / 100u;
        *p++ = (char)('0' + a);
        *(unsigned short *)p =
            *(const unsigned short *)(digit_pairs + 2 * (x - a * 100u));
        return p + 2;
    }
    if (x >= 10u) {
        *(unsigned short *)p = *(const unsigned short *)(digit_pairs + 2 * x);
        return p + 2;
    }
    *p = (char)('0' + x);
    return p + 1;
}

static __attribute__((always_inline)) inline char *write_u32(char *p, u32 x) {
    if (x < 10000u) return small(p, x);
    u32 hi = x / 10000u;
    u32 lo = x - hi * 10000u;
    p = small(p, hi);
    u32 a = lo / 100u;
    *(unsigned short *)p = *(const unsigned short *)(digit_pairs + 2 * a);
    *(unsigned short *)(p + 2) =
        *(const unsigned short *)(digit_pairs + 2 * (lo - a * 100u));
    return p + 4;
}

static __attribute__((always_inline)) inline u32 read_u32(const char **pp) {
    const char *p = *pp;
    while ((unsigned)(*p - '0') > 9u) ++p;
    u32 x = 0;
    do { x = x * 10u + (u32)(*p++ - '0'); }
    while ((unsigned)(*p - '0') <= 9u);
    *pp = p;
    return x;
}

static void run(DuckInfo *info) {
    const char *p = info->stdin_ptr;
    int dn = (int)read_u32(&p), dm = (int)read_u32(&p);
    int an = dn + 1, bn = dm + 1;
    int need = an + bn - 1, size = 1;
    while (size < need) size <<= 1;

    for (int i = 0; i < an; ++i) {
        while ((unsigned)(*p - '0') > 9u) ++p;
        fa[i] = mont_mul((u32)(*p++ - '0'), 932051910u);
    }
    for (int i = an; i < size; ++i) fa[i] = 0;
    for (int i = 0; i < bn; ++i) {
        while ((unsigned)(*p - '0') > 9u) ++p;
        fb[i] = mont_mul((u32)(*p++ - '0'), 932051910u);
    }
    for (int i = bn; i < size; ++i) fb[i] = 0;

    forward(fa, size);
    forward(fb, size);
    for (int i = 0; i < size; ++i) fa[i] = mont_mul(fa[i], fb[i]);
    inverse(fa, size);

    char *out = info->stdout_ptr;
    for (int i = 0; i < need; ++i) {
        out = write_u32(out, fa[i]);
        *out++ = i + 1 == need ? '\n' : ' ';
    }
    info->stdout_size = (duck_u64)(out - info->stdout_ptr);
}

#ifndef LOCAL
__attribute__((noreturn))
void __libc_start_main(void *unused, long argc, char **argv) {
    (void)unused;
    DuckInfo *info = duck_info(argc, argv);
    run(info);
    duck_exit();
}
int main(void) {}
#else
extern long read(int, void *, unsigned long);
extern long write(int, const void *, unsigned long);
static char local_in[4000000], local_out[4000000];
int main(void) {
    long n = read(0, local_in, sizeof(local_in));
    DuckInfo info = {0};
    info.stdin_ptr = local_in;
    info.stdin_size = (duck_u64)n;
    info.stdout_ptr = local_out;
    run(&info);
    write(1, local_out, info.stdout_size);
    return 0;
}
#endif

CompilationN/AN/ACompile OKScore: N/A

Subtask #1 Testcase #15.44 us16 KBAcceptedScore: 100

Subtask #1 Testcase #226.598 ms3 MB + 440 KBAcceptedScore: 0

Subtask #1 Testcase #312.417 ms1 MB + 296 KBAcceptedScore: 0

Subtask #1 Testcase #412.532 ms1 MB + 284 KBAcceptedScore: 0

Subtask #1 Testcase #55.59 us16 KBAcceptedScore: 0

Subtask #1 Testcase #64.4 us16 KBAcceptedScore: 0

Subtask #1 Testcase #75.06 us16 KBAcceptedScore: 0

Subtask #1 Testcase #826.548 ms3 MB + 172 KBAcceptedScore: 0

Subtask #1 Testcase #926.448 ms3 MB + 172 KBAcceptedScore: 0

Subtask #1 Testcase #1026.246 ms2 MB + 928 KBAcceptedScore: 0

Subtask #1 Testcase #1126.575 ms3 MB + 520 KBAcceptedScore: 0

Subtask #1 Testcase #1226.217 ms2 MB + 400 KBAcceptedScore: 0

Subtask #1 Testcase #134.13 us16 KBAcceptedScore: 0


Judge Duck Online | 评测鸭在线
Server Time: 2026-09-12 13:45:21 | Loaded in 1 ms | Server Status
个人娱乐项目,仅供学习交流使用 | 捐赠