关于我不会写 PR 这件事 || P5071 [Ynoi2015] 此时此刻的光辉
各位同学大家好,我是月丸,今天来给大家看看这道 15 年的 Ynoi 题。
一开题,这个不是水题吗?怎么还要套个 Pollard-Rho 的,哎呀我今天真是爆炸了。
我不会写 Pollard-Rho,即使写了也等于白写,没有写出来或是常数爆炸,写完了就放进了应该去的地方(指 TLE)。
所以我就用了暴力
当然这样肯定过不去的,后面那玩意
仙布着急,加个 Exact Division 就可以了。跑出来表现良好,质因数分解部分时间不超过 250ms R48632123,总程序可在不加前缀和 / O2 优化的情况下通过本题 R48632082。
时间复杂度:
对 Exact Divison 的简单介绍
Exact Division 可用于优化整除计算的常数。
对于任意整数
其中
对每个
当
优点:
- 常数较其他的优化算法都小,有显著的优化效果
缺点:
- 适用范围小,多用于暴力质因数分解 / 部分筛法(e.g. 线性筛)的加速
可参见 Exact division by constants 简介 - hly1024 / 除算・剰余算の高速化 - Min_25。
证明正确性
当
同理,当
因此
证毕。
实现例
下面代码节选自 除算・剰余算の高速化 - Min_25。
using i64 = long long;
using u32 = unsigned;
using u64 = unsigned long long;
template <typename T>
struct ExactDiv {
ExactDiv() {}
ExactDiv(T n) : t(T(-1) / n), i(mul_inv(n)) {}
T mul_inv(T n) {
T x = n;
for (int i = 0; i < 5; ++i) x *= 2 - n * x;
return x;
}
friend T operator / (T n, ExactDiv d) { return n * d.i; };
bool divide(T n) { return n * this->i <= this->t; }
T t, i;
};
本题代码
// In the name of
//
// _/_/ _/_/_/ _/_/_/ _/_/_/_/ _/ _/ _/_/_/_/
// _/ _/ _/ _/ _/ _/ _/ _/ _/
// _/_/_/_/ _/_/_/_/ _/ _/_/_/_/ _/ _/ _/_/_/_/
// _/ _/ _/ _/ _/ _/ _/ _/ _/
// _/ _/ _/ _/ _/_/_/ _/_/_/_/ _/_/_/_/ _/_/_/_/
//
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cassert>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#include <bitset>
#define For(i, l, r) for (i = int64(l); i <= int64(r); i++)
#define Fo(i, n) For(i, 1, n)
#define Rof(i, r, l) for (i = int64(r); i >= int64(l); i--)
#define Ro(i, n) Rof(i, n, 1)
#define clr(a) memset(a, 0, sizeof(a))
#define cpy(a, b) memcpy(a, b, sizeof(b))
#define fc(a, ch) memset(a, ch, sizeof(a))
typedef double real;
typedef long double extended;
typedef unsigned uint;
typedef unsigned long long uint64;
typedef long long int64;
#define T1 template <class A>
#define T2 template <class A, class B>
#define T3 template <class A, class B, class C>
#define T4 template <class A, class B, class C, class D>
inline void read(char &x) {
do
x = getchar();
while (x <= ' ');
}
inline void read(char *s) {
char ch;
do
ch = getchar();
while (ch <= ' ');
while (ch > ' ') {
(*s) = ch;
s++;
ch = getchar();
}
(*s) = 0;
}
inline void read(std::string &s) {
char ch;
do
ch = getchar();
while (ch <= ' ');
while (ch > ' ') {
s.push_back(ch);
ch = getchar();
}
}
T1 inline void readint(A &x) {
bool neg = false;
char ch;
do
ch = getchar();
while (ch <= ' ');
if (ch == '-') {
neg = true;
ch = getchar();
}
x = 0;
while (ch > ' ') {
x = x * 10 + (ch & 15);
ch = getchar();
}
if (neg)
x = -x;
}
inline void read(int &x) { readint(x); }
inline void read(uint &x) { readint(x); }
inline void read(int64 &x) { readint(x); }
inline void read(uint64 &x) { readint(x); }
T2 inline void read(A &a, B &b) {
read(a);
read(b);
}
T3 inline void read(A &a, B &b, C &c) {
read(a);
read(b);
read(c);
}
T4 inline void read(A &a, B &b, C &c, D &d) {
read(a);
read(b);
read(c);
read(d);
}
inline void writeln() { putchar('\n'); }
T1 inline void writeint(A x) {
static char buf[20];
int top = 0;
if (!x) {
putchar('0');
return;
}
if (x < 0) {
putchar('-');
x = -x;
}
while (x) {
buf[++top] = (x % 10) | 48;
x /= 10;
}
while (top)
putchar(buf[top--]);
}
inline void write(int x) { writeint(x); }
inline void write(uint x) { writeint(x); }
inline void write(int64 x) { writeint(x); }
inline void write(uint64 x) { writeint(x); }
inline void write(char ch) { putchar(ch); }
inline void write(char *s) {
while (*s) {
putchar(*s);
s++;
}
}
inline void write(const char *s) { printf(s); }
T1 inline void write_(A x) {
write(x);
putchar(' ');
}
T1 inline void writeln(A x) {
write(x);
putchar('\n');
}
T2 inline void write(A a, B b) {
write_(a);
write(b);
}
T2 inline void writeln(A a, B b) {
write_(a);
writeln(b);
}
T3 inline void writeln(A a, B b, C c) {
write_(a);
write_(b);
writeln(c);
}
T4 inline void writeln(A a, B b, C c, D d) {
write_(a);
write_(b);
write_(c);
writeln(d);
}
#undef T1
#undef T2
#undef T3
#undef T4
const int N = 1e5 + 10;
const int M = N * 30;
const int A = 31622;
const int MOD = 19260817;
int n, q, B;
int pn;
uint pc[N], pd[N];
int p[N];
int a[N][15], b[N][15], an[N];
int c[N << 1];
int inv[M];
int qans[N];
std::map<int, int> idmap;
int idcnt = A;
struct node{
int id, l, r;
bool operator < (const node &b) const { return (l / B != b.l / B) ? (l < b.l) : ((l / B & 1) == (r < b.r)); }
};
node qs[N];
uint inv32(uint n) {
uint x = n, i;
Fo(i, 5) x *= 2 - n * x;
return x;
}
inline int qpow(int64 b, int p, int mod=MOD) {
int res = 1;
while(p) {
if(p & 1)
res = res * b % mod;
b = b * b % mod;
p >>= 1;
}
return res;
}
void init() {
static bool notp[N];
int i, j, x;
for(i=2; i<=A; i++) {
if(!notp[i]) {
p[++pn] = i;
pc[pn] = inv32(i);
pd[pn] = uint(-1) / i;
}
for(j=1; j<=pn and i*p[j]<=A; j++) {
notp[i*p[j]] = true;
if(!(i % p[j])) break;
}
}
inv[0] = x = 1;
Fo(i, M - 1) x = 1LL * x * i % MOD;
inv[M - 1] = qpow(x, MOD - 2);
Ro(i, M - 2) inv[i] = inv[i + 1] * (i + 1LL) % MOD;
x = 1; Fo(i, M - 1) inv[i] = 1LL * inv[i] * x % MOD, x = 1LL * x * i % MOD;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("a.in", "r", stdin);
freopen("a.out", "w", stdout);
#endif
int i, j, k;
int l, r, ans = 1;
uint x;
init();
read(n, q);
B = sqrt(n);
Fo(i, n) {
read(x);
k = 0;
if(~x & 1) {
k++;
a[i][k] = 1;
b[i][k] = 1;
for(x >>= 1; ~x & 1; x >>= 1)
b[i][k]++;
}
For(j, 2, pn) {
if(pc[j] * x <= pd[j]) {
k++;
a[i][k] = j;
b[i][k] = 1;
x *= pc[j];
while(pc[j] * x <= pd[j]) {
x *= pc[j];
b[i][k]++;
}
}
}
if(x > 1) {
k++;
a[i][k] = idmap[x] ? idmap[x] : (idmap[x] = ++idcnt);
b[i][k] = 1;
}
an[i] = k;
}
Fo(i, q) {
qs[i].id = i;
read(qs[i].l, qs[i].r);
}
std::sort(qs+1, qs+1+q);
l = 1;
r = 0;
#define inc(p) Fo(i, an[p]) { \
ans = 1LL * inv[c[a[p][i]] + 1] * ans % MOD; \
c[a[p][i]] += b[p][i]; \
ans = 1LL * (c[a[p][i]] + 1) * ans % MOD; \
}
#define dec(p) Fo(i, an[p]) { \
ans = 1LL * inv[c[a[p][i]] + 1] * ans % MOD; \
c[a[p][i]] -= b[p][i]; \
ans = 1LL * (c[a[p][i]] + 1) * ans % MOD; \
}
Fo(k, q) {
while(r < qs[k].r) { r++; inc(r); }
while(l > qs[k].l) { l--; inc(l); }
while(r > qs[k].r) { dec(r); r--; }
while(l < qs[k].l) { dec(l); l++; }
qans[qs[k].id] = ans;
}
Fo(i, q) writeln(qans[i]);
return 0;
}
愿我们的真主,宝可梦世界之唯一神,阿尔宙斯,保佑你。