丢人。
todo
写了自己也不会看吧(笑
- 补一补交换代数
My QQ
Let
My QQ number is
luogu: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss ht syscall nx pdpe1gb rdtscp lm constant_tsc rep_good nopl xtopology nonstop_tsc cpuid aperfmperf tsc_known_freq pni pclmulqdq monitor ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt aes xsave avx f16c rdrand hypervisor lahf_lm abm 3dnowprefetch invpcid_single pti fsgsbase tsc_adjust bmi1 hle avx2 smep bmi2 erms invpcid rtm mpx avx512f avx512dq rdseed adx smap clflushopt clwb avx512cd avx512bw avx512vl xsaveopt xsavec xgetbv1 xsaves avx512_bf16 ida arat avx512_vnni
全国统一评测时采用的机器配置为:Intel(R) Core(TM) i7-8700K CPU @3.70GHz, 内存 32GB。上述时限以此配置为准。
Useful facts and tricks
Miller-Rabin 底数:2, 325, 9375, 28178, 450775, 9780504, 1795265022
不要试图用手写的函数替换 __builtin 系列。
另外,__builtin_expect(long e, long c) 返回 e 并告诉编译器 e 很有可能等于 c。
__builtin_ffs(l/ll) 返回 1 + ctz。
__builtin_parity(l/ll) 返回 1 的个数的奇偶性。
__builtin_bswap(16/32/64/128) 反转 byte 的顺序。
Montgomery 约化:
令 为模数, 为一个比 大的 的幂(一般取 )。预处理 .
对一个 的数 计算 :令 ,那么答案就是 ,可能要减去一个 . 注意 要用 __uint128_t 存。
模 的逆可以通过 Hensel lifting 计算:
u64 inv(u64 a)
{
u64 x = a;
for (int i = 0; i < 5; i++) x *= (2 - x * a);
return x;
}平方根:
/* If x is square, return +sqrt(x).
* Otherwise, return -1.
*/
long long isqrt(unsigned long long x) {
/* Precondition: make x odd if possible. */
int sh = __builtin_ctzll(x);
x >>= (sh&~1);
/* Early escape. */
if (x&6) return -1;
/* 2-adic Newton */
int i;
const int ITERATIONS = 5; // log log x - 1
unsigned long long z = (3-x)>>1, y=x*z;
for (i=0; i<ITERATIONS; i++) {
unsigned long long w = (3 - z*y) >> 1;
y *= w;
z *= w;
}
assert(x==0 || (y*z == 1 && x*z == y));
/* Get the positive square root. Also the top bit
* might be wrong. */
if (y & (1ull<<62)) y = -y;
y &= ~(1ull<<63);
/* Is it the square root in Z? */
if (y >= 1ull<<32) return -1;
/* Yup. */
return y<<(sh/2);
}