In mathematics you don't understand things, you just get used to them.

标签 number theory 下的文章

link。

设 $\displaystyle f(x) = \# S', s.t. S' \subseteq S, S' \neq \varnothing, \gcd(S') = x$,$g(x) = \# t, s.t. \gcd(t, x) = 1$,则答案为 $\sum f_i \times g_i$。

  • $f$:这个的求解是老套路了,设 $\displaystyle F(x) = \# S', s.t. S' \subseteq S, S' \neq \varnothing, x \mid S'$,则有 $\displaystyle F(x) = 2^{\sum_{x \mid t} \textit{cnt}_t}-1$,$cnt$ 是桶,$\displaystyle f(x) = \sum_{x \mid d} \mu(\frac{d}{x}) \times F(d)$,可以调和级数也可以逆 dirichlet 前后缀和(不可以)。
  • $g$:写出 $\displaystyle g_T = \sum_i [(T, i) = 1] \textit{cnt}_i = \sum_{d \mid T} \mu(d) \sum_{d \mid i} cnt_i = \sum_{d \mid T} h_d$,其中 $h_d = \mu(d) \times w_d$,其中 $\displaystyle w_T = \sum_{T \mid i} \textit{cnt}_i$。都是 dirichlet 前后缀和的形式。
using modint = modint1000000007;
bitset<10000100> tag;
int n, up, tot, prm[10000100], mu[10000100], w[10000100];
modint f[10000100], F[10000100], pw[10000100];
void sieve(int maxn) {
    mu[1] = 1;
    for (int i=2; i<=maxn; ++i) {
        if (!tag[i]) {
            prm[++tot] = i;
            mu[i] = -1;
        }
        for (int j=1; j<=tot && i<=maxn/prm[j]; ++j) {
            tag.set(i*prm[j]);
            if (i%prm[j] == 0) {
                break;
            }
            mu[i*prm[j]] = -mu[i];
        }
    }
}
signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin >> n;
    for (int i=1,x; i<=n; ++i) {
        cin >> x, w[x]++;
        cmax(up, x);
    }
    sieve(up);
    pw[0] = 1;
    for (int i=1; i<=up; ++i) {
        pw[i] = pw[i-1]*2;
    }
    for (int i=1; i<=tot; ++i) {
        for (int j=up/prm[i]; j>=1; --j) {
            w[j] += w[j*prm[i]];
        }
    }
    for (int i=1; i<=up; ++i) {
        F[i] = pw[w[i]]-1;
    }
    for (int i=1; i<=up; ++i) {
        for (int d=i; d<=up; d+=i) {
            f[i] += mu[d/i]*F[d];
        }
    }
#define g F
    for (int i=1; i<=up; ++i) {
        g[i] = mu[i]*w[i];
    }
    for (int i=1; i<=tot; ++i) {
        for (int j=1; j<=up/prm[i]; ++j) {
            g[j*prm[i]] += g[j];
        }
    }
    modint ans = 0;
    for (int i=2; i<=up; ++i) {
        ans += f[i]*g[i];
    }
    cout << ans.val() << "\n";
}

Quack 知道好多东西,把它们都做成 ppt。

inv_gcd 还可以用递推矩阵算。

void exgcd(int a, int b){
  r[0] = a, r[1] = b;
  int i = 2;
  for ( ; r[i - 1]; i++) {
    r[i] = r[i - 2] % r[i - 1];
    q[i - 1] = r[i - 2] / r[i - 1];
  }
  d = r[i -= 2];
  mat res(1, 0, 0, 1);
  for (int j = i - 1; j; --j)
    res.mul(mat(0, 1, 1, -q[j]));
  s = res.get(1, 0); t = res.get(1, 1);
}

还有个 trick 叫 binary gcd。

int gcd(int a, int b){
  if (a == b) return a;
  if ((a & 1) && (b & 1)) return gcd(b, abs(a - b));
  else if ((a & 1) && (!(b & 1))) return gcd(a, b >> 1);
  else if ((!(a & 1)) && (b & 1)) return gcd(a >> 1, b);
  else return gcd(a >> 1, b >> 1) << 1;
}

复杂度和正确性平凡吧,在写高精度 gcd 的时候可以考虑。接下来看一个典中典问题:

证明:$(a^n-1, a^m-1) = a^{(n, m)}-1$。

P.f.:不妨设 $n \leqslant m$,首先 $(a^n-1, a^m-1) = (a^n(a^{m-n}-1), a^n-1) = (a^{m-n}-1, a^n-1)$,一直迭代可以得到 $(a^n-1, a^m-1) = (a^{(n, m)}-1, a^{(n, m)}-1) = a^{(n, m)}-1$。

然后这个有个 more general 的版本,就是对于任意整数 $a, b$,满足 $(a, b) = 1$ 则有 $(a^n-b^n, a^m-b^m) = a^{(n, m)}-b^{(n, m)}$,证明是类似的。

但是如果说我们还有一个 more more general 的版本呢?

Theorem:若 $f_n$ 是一个整数序列,并满足 $f_0 = 0$,$f_{n} \equiv f_{n-m} \pmod {f_m},n > m$,有 $(f_n, f_m) = f_{(n, m)}$。

P.f.:考虑 induction,当 $n = m$,$n = 0$,$m = 0$ 时,上述命题为一个,一个一个一个,一个一个一个一个一个一个一个

什么栈什么溢什么出上还有一些 q-analog 的高论,不过我连上面的证明都还没看懂。引一下原 writer:

Mimic in expts a subtractive Euclidean algorithm $\rm\,(n,m) = (\color{#0a0}{n\!-\!m},m)$

$$\begin{align} \rm{e.g.}\ \ &\rm (f_5,f_2) = (f_3,f_2) = (f_1,f_2) = (f_1,f_1) = (f_1,\color{darkorange}{f_0})= f_1 = f_{\:\!(5,\,2)}\[.3em]
{\rm like}\ \ \ &(5,\ 2)\, =\:\! (3,\ 2)\, =\:\! (1,\ 2)\:\! =\:\! (1,\ 1)\:\! =\:\! (1,\ \color{darkorange}0)\:\! = 1,\ \ {\rm since}\end{align}\qquad$$

$\rm\ f_{\,n}\: :=\ a^n\!-\!1\ =\ a^{n-m} \: \color{#c00}{(a^m\!-\!1)} + \color{#0a0}{a^{n-m}\!-\!1},\,\ $ hence $\rm\:\ {f_{\,n}\! = \color{#0a0}{f_{\,n-m}}\! + k\ \color{#c00}{f_{\,m}}},\,\ k\in\mathbb Z,\:$ thus

Theorem $\: $ If $\rm\ f_{\, n}\: $ is an integer sequence with $\rm\ \color{darkorange}{f_{0} =\, 0},\: $ $\rm \:{ f_{\,n}\!\equiv \color{#0a0}{f_{\,n-m}}\ (mod\ \color{#c00}{f_{\,m})}}\ $ for all $\rm\: n > m,\ $ then $\rm\: (f_{\,n},f_{\,m})\ =\ f_{\,(n,\:m)}, \: $ where $\rm\ (i,\:j)\ $ denotes $\rm\ gcd(i,\:j).\:$

Proof $\ $ By induction on $\rm\:n + m\:$. The theorem is trivially true if $\rm\ n = m\ $ or $\rm\ n = \color{darkorange}0\ $ or $\rm\: m = \color{darkorange}0.\:$
So we may assume $\rm\:n > m > 0\:$.$\ $ Note $\rm\ (f_{\,n},f_{\,m}) = (\color{#0a0}{f_{\,n-m}},\color{#c00}{f_{\,m}})\ $ follows by $\rm\color{#90f}{Euclid}$ & hypothesis.
Since $\rm\ (n-m)+m \ <\ n+m,\ $ induction yields $\rm\, \ (f_{\,n-m},f_{\,m})\, =\, f_{\,(n-m,\:m)} =\, f_{\,(n,\:m)}.$

$\rm\color{#90f}{Euclid}\!:\ A\equiv a\pmod{\! m}\,\Rightarrow\ (A,m) = (a,m)\,$ is the reduction (descent) step used both above and in the Euclidean algorithm $\rm\: (A,m) = (A\bmod m,\,m),\, $ the special case $\,\rm f_{\:\!n} = n\,$ above.

This is a prototypical strong divisibility sequence. Same for Fibonacci numbers.


Alternatively it has a natural proof via the Order Theorem $\ a^k\equiv 1\iff {\rm ord}(a)\mid k,\,$ viz.

$$\begin{eqnarray}\ {\rm mod}\:\ d\!:\ a^M\!\equiv 1\equiv a^N&\!\iff\!& {\rm ord}(a)\ |\ M,N\!\color{#c00}\iff\! {\rm ord}(a)\ |\ (M,N)\!\iff\! \color{#0a0}{a^{(M,N)}\!\equiv 1}\[.2em]
{\rm i.e.}\ \ \ d\ |\ a^M\!-\!1,\:a^N\!-\!1\! &\!\iff\!\!&\ d\ |\ \color{#0a0}{a^{(M,N)}\!-\!1},\qquad\,\ {\rm where} \quad\! (M,N)\, :=\, \gcd(M,N)
\end{eqnarray}\ \ \ \ \ $$

Thus, by above $\, a^M\!-\!1,\:a^N\!-\!1\ $ and $\, a^{(M,N)}\!-\!1\ $ have the same set $\,S\,$ of common divisors $\,d,\, $ therefore they have the same greatest common divisor $\ (= \max\ S).$

Note We used the GCD universal property $\ a\mid b,c \color{#c00}\iff a\mid (b,c)\ $ [which is the definition of a gcd in more general rings]. Compare that with $\ a<b,c \!\iff\! a< \min(b,c),\, $ and, analogously, $\,\ a\subset b,c\iff a\subset b\cap c.\ $ Such universal "iff" characterizations enable quick and easy simultaneous proof of both directions.

The conceptual structure that lies at the heart of this simple proof is the ubiquitous order ideal. $\ $ See this answer for more on this and the more familiar additive form of a denominator ideal.

link。

容易发现,如果将 $x$ 写作 $\displaystyle \prod_{i = 1}^k p_i^{\alpha_i}$ 的形式,$\displaystyle J(x) = 1+\sum p_i^{\alpha_i}+\sum\sum p_i^{\alpha_i}p_j^{\alpha_j}+\dots = \sum_{T \in 2^S} \sum_{i \in T} p_i^{\alpha_i}$,其中 $S = \{1, \dots, k\}$。写到这里可以发现 Joker function 是个 multiplicative function,所以 Joker function 又可以写作 $\displaystyle J(x) = \prod_{i = 1}^k J(p_i^{\alpha_i}) = \prod_{i = 1}^k \left(p_i^{\alpha_i}+1\right)$。

考虑 dp,设 $f[i][j]$ 表示用前 $i$ 个因数通过上述形式凑出 $j$ 的方案数,转移很平凡,具体看代码。这个 dp 可以用数据结构来存。这个 dp 复杂度的保证在于 $\displaystyle \max_{i \in [1, 10^{12}]} {\sigma_0(i)} = 6720$,这就叫人比较无语。至于判断一个数是否是 $p^k+1$ 的形式,可以根号分治来做,记阈值为 $T = 10^6$,对于 $\leqslant T$ 的元素,可以线性筛出所有质数,标记所有质数的次幂,数量级粗略是 $O(T\log T)$,完全跑不满;对于 $> T$ 的元素,因为 $(T+1)^2 > 10^{12}$,所以只需要判断是否为质数即可,Miller Rabin 或者其他 nb 的判素数方法即可。写不来 MR 所以直接蒯了个 mrsrz 的。

namespace Miller_Rabin{
    const int P[]={2,3,7,97,19260817};
    inline LL mul(LL a,LL b,const LL&md){
        LL tmp=a*b-(LL)((long double)a*b/md+.5)*md;
        return tmp+(tmp>>63&md);
    } 
    inline LL pow(LL a,LL b,const LL&md){
        LL ret=1;
        for(;b;b>>=1,a=mul(a,a,md))if(b&1)ret=mul(ret,a,md);
        return ret;
    }
    bool test(LL a,LL p){
        LL x=p-1;
        int d=0;
        while(!(x&1))++d,x>>=1;
        LL t=pow(a,x,p);
        while(d--){
            const LL ls=t;
            t=mul(t,t,p);
            if(t==1&&ls!=1&&ls!=p-1)return 0;
        }
        return t==1;
    }
    bool check(LL n){
        if(n<2)return 0;
        if(n==2||n==3||n==7||n==97||n==P[4])return 1;
        for(int i=0;i<5;++i)if(n%P[i]==0)return 0;
        for(int i=0;i<5;++i)
        if(!test(P[i]%n,n))return 0;
        return 1;
    }
}
using Miller_Rabin::check;
LL A;
bitset<1000100> tag;
int prm[1000100], cnt;
map<LL, int> mp; // a in it iff a = p^k
bastr<LL> offset[2000100];
void sieve(int n) {
    for (int i=2; i<=n; ++i) {
        if (!tag.test(i)) {
            prm[++cnt] = i;
        }
        for (int j=1; j<=cnt && i<=n/prm[j]; ++j) {
            tag.set(i*prm[j]);
            if (i%prm[j] == 0) {
                break;
            }
        }
    }
    for (int i=1; i<=cnt; ++i) {
        for (LL j=prm[i]; j<=1e12; j*=prm[i]) {
            mp[j] = i;
        }
    }
}
void executer() {
    cin >> A;
    map<LL, LL> dp[2];
    bastr<int> eff;
    for (LL i=1; i<=A/i; ++i) {
        if (A%i == 0) {
            if (mp.count(i-1)) {
                offset[mp[i-1]] += i;
                eff += mp[i-1];
            }
            if (i*i != A) {
                if (mp.count(A/i-1)) {
                    offset[mp[A/i-1]] += A/i;
                    eff += mp[A/i-1];
                }
                else if (check(A/i-1)) {
                    offset[++cnt] += A/i;
                    eff += cnt;
                }
            }
        }
    }
    sort(eff.begin(), eff.end());
    eff.erase(unique(eff.begin(), eff.end()), eff.end());
    dp[0][1] = 1;
    int cur = 1;
    for (auto u : eff) {
        dp[cur] = dp[cur^1];
        for (auto v : dp[cur^1]) {
            for (auto p : offset[u]) {
                if (A/p >= v.first && A%(v.first*p) == 0) {
                    dp[cur][v.first*p] += v.second;
                }
            }
        }
        cur ^= 1;
    }
    cout << dp[cs(eff)&1][A] << "\n";
}

link。

简单提一下做法,注意到 $k^{2^a}\equiv k^{2^b}\equiv-1\equiv (-1)^{2^{b-a}}=1\pmod{(k^{2^a}+1,k^{2^{b}}+1)}$,所以 $\gcd$ 只会是 $1$ 或 $2^{r-l}$,取决于 $k$ 的就行,于是求一个 product 即可。

接下来是这篇题解的重点,聚焦 $\displaystyle \prod\left(k^{2^i}+1\right)=\sum_{i=0}^{2^{r-l+1}-1}\left(k^{2^l}\right)^i$ 做补充。

不妨从二项式乘法的角度来看,举例 $\left(k^{2^i}+1\right)\left(k^{2^j}+1\right)\left(k^{2^w}+1\right)$,不妨将 $k^{2^x}$ 看作是选择,将 $1$ 看作是不选择,那么一个长度为 $r-l+1$ 的二进制数即可表示出这个乘积所有的状态,不妨设这个状态为 $S$,注意到 $S$ 对答案的贡献是在做加法,并且因为类似 $2^i+2^j+2^w$ 的指数相加正好可以凑出 $S$,所以 $S$ 的贡献就是 $\left(k^{2^l}\right)^S$,得证。

void executer() {
    int k, p;
    LL l, r;
    cin >> k >> l >> r >> p;
    SetMod(p-1);
    int e = qpow(2, l), e2 = qpow(2, r-l+1);
    SetMod(p);
    int K = k%p ? qpow(k, e) : 0, ans;
    if (K == 0) {
        ans = 1;
    }
    else {
        if (K == 1) {
            ans = qpow(2, r-l+1);
        }
        else {
            ans = mul(sub(qpow(K, e2), 1), inv(sub(K, 1)));
        }
    }
    if (k%2) {
        mul_eq(ans, inv(qpow(2, r-l)));
    }
    cout << ans << "\n";
}

前置容斥原理,最精简的形式应当如此写成:

有 $n$ 个集合 $A_1, \dots, A_n$,记 $S = \{A_1, \dots, A_n\}$。

则有 $\displaystyle\left|\bigcup_{i = 1}^n A_i\right|=\sum_{T\in2^{S}}(-1)^{|T|-1}\left|\bigcap_{X\in T}X\right|$。

考察欧拉函数通项公式,不妨从容斥的角度来理解:

欲证明 $\displaystyle\varphi(n) = n\prod_{i=1}^{k}\left(1-\frac{1}{p_i}\right)$。

P.f.:令 $A_i$ 表示 $p_i$ 的倍数集合(共 $k$ 个这样的集合),并且这些倍数 $\leqslant n$,$S = \{A_1, \dots, A_k\}$,可知 $\left|A_i\right| = \lfloor\frac{n}{p_i}\rfloor$。则 $\displaystyle\varphi(n) = n-\left|\bigcup_{i=1}^k A_i\right|=n-\sum_{T\in2^{S}}(-1)^{|T|-1}\left|\bigcap_{X\in T}X\right|=n-\sum_{T\in2^{S}}(-1)^{|T|-1}\frac{n}{\prod p}=n-\left(\sum_{i=1}^k\frac{n}{p_i}\right)+\left(\sum_{i=1}^k\sum_{j=i+1}^{k}\frac{n}{p_ip_j}\right)-\cdots=n
\left(1-\left(\sum_{i=1}^k\frac{1}{p_i}\right)+\left(\sum_{i=1}^k\sum_{j=i+1}^{k}\frac{1}{p_ip_j}\right)-\cdots\right) = n\prod_{i=1}^k\left(1-\frac{1}{p_i}\right)$。

解释一下推导的最后一步,考虑组合意义,r.h.s. 相当于在多项式凑系数,$1$ 表示不选,$\frac{1}{p}$ 表示选,选奇数个符号为负,选偶数个符号为正恰好对应 l.h.s.(我觉得能从左边推到右边还是比较困难的)。

从积性的角度:

P.f.:易证 $\varphi(p^\alpha)=p^\alpha-p^{\alpha-1}$(考虑按 $[1,\dots p], [p+1, \dots, 2p], \dots, [p^{\alpha-1}+1, p^\alpha]$ 分组,发现只有每组的最后一个数 $\not\perp p^\alpha$,而这样的组一共有 $p^{\alpha-1}$),那么 $\displaystyle\varphi(n) = \prod_{i=1}^k\varphi(p_i^{\alpha_{i}}) = \prod_{i=1}^k\left(p_i^{\alpha_{i}}-p_i^{\alpha_i-1}\right)=n\prod_{i=1}^k\left(1-\frac{1}{p_i}\right)$。

结合以上两种证明可以顺便证明欧拉函数的积性。


写出 CRT 所解决的线性同余方程组问题:

求解方程 $x\equiv a_i\pmod{p_i}$。

Sol.:考虑构造

咕咕咕


类欧几里得。

给定非负整数 $n,a,b,c$,求 $f(n, a, b, c) = \displaystyle\sum_{i=1}^n\lfloor\frac{ai+b}{c}\rfloor$

Sol. pt 1:首先若 $a\geqslant c$,或 $b\geqslant c$ 我们可以通过对取模并提前计算贡献的方式将 $a<c$,$b<c$ 的假设一般化。于是不妨设 $a<c$,$b<c$,设 $m=\lfloor\frac{an+b}{c}\rfloor$,也就是求和项中的最大值(非负整数),由于 $\displaystyle\forall x\in\mathbb{N},x=\sum_{i=1}^{+\infty}[i \leqslant x]$,所以原式等于 $\displaystyle\sum_{i=1}^n\sum_{j=1}^{m}[j \leqslant \lfloor \frac{ai+b}{c} \rfloor]=\sum_{j=1}^m \sum_{i=1}^n [ai \geqslant cj-b]=nm-\sum_{j=1}^m \sum_{i=1}^n [ai \leqslant cj-b-1]=nm-\sum_{j=1}^m \sum_{i=1}^n [i \leqslant \lfloor\frac{cj-b-1}{a}\rfloor] = nm-f(m, c, -b-1, a)$,到这里你会发现有点不对头,我应该要保证 $-b-1$ 是非负整数,但是这不太可能,考虑到 $b$ 这个位置贡献的方式,我们也许需要考虑更改 $f$ 的定义。

Sol. pt 2:更改定义 $f(n, a, b, c) = \displaystyle\sum_{i=\color{red}0}^n\lfloor\frac{ai+b}{c}\rfloor$,推导过程大体相同(有些细节可以参看 $g$,$h$ 的推导),最终的结论是 $f(n, a, b, c) = nm-f(m-1, c, c-b-1, a)$,这保证了非负整数的前提(注意边界条件的计算有所改变)。

让我们定义 $\displaystyle s_k(n) = \sum_{i=1}^n i^k$。

大致代码长这样:

int floor_sum(LL n, int a, int b, int c) {
    // @returns: \sum_{i=0}^n floor((ai+b)/c)
    if (a == 0) {
        return mul(safe_mod(n+1), safe_mod(b/c));
    }
    if (a >= c || b >= c) {
        return add(floor_sum(n, a%c, b%c, c), mul(s(n), safe_mod(a/c)), mul(safe_mod(n+1), safe_mod(b/c)));
    }
    LL m = (a*n+b)/c;
    return sub(mul(safe_mod(n), safe_mod(m)), floor_sum(m-1, c, c-b-1, a));
}

给定非负整数 $n, a, b, c$,求 $g(n, a, b, c) = \sum_{i=0}^n \lfloor\frac{ai+b}{c}\rfloor^2$,$h(n, a, b, c) = \sum_{i=0}^n i\lfloor \frac{ai+b}{c} \rfloor$

__Sol.__:汲取先前的教训,我们将 $0$ 囊括进了和式。同时在看了题解推导的过程后,我们可以更加成熟地对问题进行分类讨论。

  • $g$

    • $c \leqslant \max\{a, b\}$:$\displaystyle g(n, a, b, c) = \sum_{i=0}^n \left( \lfloor\frac{(a\bmod c)i+b\bmod c}{c}+i \lfloor \frac{a}{c} \rfloor+\lfloor \frac{b}{c} \rfloor \rfloor \right)^2 = g(n, a \bmod c, b \bmod c, c)+2\lfloor \frac{a}{c} \rfloor h(n, a\bmod c, b \bmod c, c) + 2 \lfloor \frac{b}{c} \rfloor f(n, a \bmod c, b \bmod c, c) + s_2(n)\lfloor \frac{a}{c} \rfloor ^ 2 + n(n+1)\lfloor \frac{a}{c} \rfloor \lfloor \frac{b}{c} \rfloor + (n+1)\lfloor \frac{b}{c} \rfloor ^ 2$。
    • $c > \min\{a, b\}$:首先注意到 $x^2 = x(x+1)-x = 2s_1(x) - x$,令 $m=\lfloor \frac{an
      +b}{c} \rfloor$,因此 $\displaystyle g(n, a, b, c) = \sum_{i=0}^n \lfloor \frac{ai+b}{c}^2 \rfloor = 2\left(\sum_{i=0}^n \sum_{j=1}^{\lfloor \frac{ai+b}{c} \rfloor}j\right)-\left(\sum_{i=0}^n\lfloor \frac{ai+b}{c} \rfloor\right) = 2\left(\sum_{i=0}^n\sum_{j=0}^{m-1
      } (j+1)[j < \lceil \frac{ai+b-c+1}{c} \rceil]\right) - f(n, a, b, c) = 2\left(\sum_{j=0}^{m-1
      } (j+1) \sum_{i=0}^n [i > \lfloor \frac{cj-b+c-1}{a} \rfloor]\right) - f(n, a, b, c) = 2\left(\sum_{j=0}^{m-1
      } (j+1) \left(n - \lfloor \frac{cj-b+c-1}{a} \rfloor\right)\right) - f(n, a, b, c) = 2n\times s_1(m) - 2h(m-1, c, c-b-1, a) - 2f(m-1, c, c-b-1, a) - f(n, a, b, c)$。
  • $h$

    • $c \leqslant \max\{a, b\}$:$h(n, a, b, c) = h(n, a \bmod c, b \bmod c, c) + s_1(n) \lfloor \frac{b}{c} \rfloor + s_2(n) \lfloor \frac{a}{c} \rfloor$。
    • $c > \min\{a, b\}$:$\displaystyle h(n, a, b, c) = \sum_{i=0}^n i\lfloor \frac{ai+b}{c} \rfloor = \sum_{i=0}^ni\sum_{j=0}^{m-1}[cj < ai+b-c+1] = \sum_{j=0}^{m-1}\sum_{i=0}^ni[i > \lfloor \frac{cj+c-b-1}{a} \rfloor] = \sum_{j=0}^{m-1}\left(s_1(n)-\sum_{i=0}^n i[i \leqslant \lfloor \frac{cj+c-b-1}{a} \rfloor]\right) = \sum_{j=0}^{m-1} s_1(n)-s_1(\lfloor \frac{cj+c-b-1}{a} \rfloor) = m\times s_1(n)-\frac{\sum_{j=0}^{m-1}\left(\lfloor \frac{cj+c-b-1}{a} \rfloor^2+\lfloor \frac{cj+c-b-1}{a} \rfloor\right)}{2} = s_1(n)\times m - f(m-1, c, c-b-1, a) - g(m-1, c, c-b-1, a)$。

考试给👴遇到了就下地狱吧。

最后的代码还是有点说头,很诡秘的是,用函数式取模会 T 飞,三个函数一起写会 Wa,反正最后的结局就是 modint+抄代码。

tpl floor_sum(LL n, int a, int b, int c) {
    if (a == 0) {
        return {mi(n+1)*(b/c), mi(n+1)*sqr(b/c), s(n)*(b/c)};
    }
    if (a >= c || b >= c) {
        tpl ret = floor_sum(n, a%c, b%c, c);
        return {
            ret.f+s(n)*(a/c)+mi(n+1)*(b/c),
            ret.g+mi(2)*(a/c)*ret.h+mi(2)*(b/c)*ret.f+s2(n)*sqr(a/c)+mi(2)*s(n)*(a/c)*(b/c)+mi(n+1)*sqr(b/c),
            ret.h+s(n)*(b/c)+s2(n)*(a/c)
        };
    }
    LL m = (a*n+b)/c;
    tpl ret = floor_sum(m-1, c, c-b-1, a);
    return {
        mi(n)*m-ret.f,
        mi(2)*n*s(m)-2*ret.h-2*ret.f-(mi(n)*m-ret.f),
        s(n)*m-ret.f*iv2-ret.g*iv2
    };
}

阶,原根,还有,,?

学数论,就好比吃shit。吃shit啊吃shit

阶 multiplicative order:在 $(a, m) = 1$ 的情况下,极小的满足 $a ^ n \equiv 1 \pmod m$ 的正整数 $n$ 称作 $a$ 模 $m$ 的阶,记作 $\delta_m(a)$。

由于欧拉定理,所以阶一定存在(至少有一个 $\varphi(m)$)。接下来扯几个性质:

  • $\{1, a^2, \dots, a^{\delta_m(a)}-1\}$ 是模 $m$ 的缩系。