http://www.lydsy.com/JudgeOnline/problem.php?id=2154
题意:求$sum_{i=1}^{n} sum_{j=1}^{m} lcm(i, j)$, $n,m<=1e7$
#include <bits/stdc++.h> using namespace std; typedef long long ll; const int N=1e7+10, MD=20101009; int p[N], pcnt, n, m; bool np[N]; ll g[N]; void init() { g[1]=1; int i, j, t; for(i=2; i<=n; ++i) { if(!np[i]) p[++pcnt]=i, g[i]=1-i; for(j=1; j<=pcnt; ++j) { t=p[j]*i; if(t>n) break; np[t]=1; if(i%p[j]==0) { g[t]=g[i]; break; } g[t]=g[i]*(1-p[j]); } } for(i=2; i<=n; ++i) g[i]*=i; for(i=1; i<=n; ++i) g[i]+=g[i-1], g[i]%=MD; } int main() { scanf("%d%d", &n, &m); if(n>m) swap(n, m); init(); ll ans=0, t1, t2; for(int i=1, pos=0; i<=n; i=pos+1) { pos=min(n/(n/i), m/(m/i)); t1=((ll)(n/i)*(n/i+1)/2)%MD; t2=((ll)(m/i)*(m/i+1)/2)%MD; ans+=((g[pos]-g[i-1])*((t1*t2)%MD))%MD; ans%=MD; } printf("%lld ", ((ans%MD)+MD)%MD); return 0; }
吐槽:
妈妈我再也不相信科学了QAQ..............这种题各种不知道哪里爆+各种不知道哪里爆让我十分蛋疼QAQ
sb错1:忘记最后除法不一定是整数QAQ,那么一定要在中途能保证是整数的就算出来啊= =后边算不出来的啊喂。。
sb错2:各种爆longlong让我十分无语,取太多mod又会很慢= =(看来为了保险起见...以后哪里要mod就mod...不要乱估计啦。。。
然后不明觉厉...为何我查询已经做到$O(n^{0.5} + m^{0.5})$了,还是那么慢?那些200多ms的是什么神做法QAQ难道是分块打表了吗QAQ
本题很恶心,为了推出$O(n^{0.5} + m^{0.5})$的查询我推了十分钟,然后各种wa以为是推错了QAQ原来是精度,妈妈压。。。
(我真的有不想写推导公式的过程的冲动QAQ
下边均设$n<=m$
$$sum_{i=1}^{n} sum_{j=1}^{m} lcm(i, j) = sum_{i=1}^{n} sum_{j=1}^{m} frac{ij}{(i,j)} $$
然后我们要想枚举$d=(i,j)$,那么就要确定$ij$怎么取,显然我们只需要先除去$i$和$j$的$d$,也就是$(i/d,j/d)=1$就行了,那么设
$$F(x, y) = sum_{i=1}^{x} sum_{j=1}^{y} ij[(i,j)=1]$$
那么原式变成
$$
sum_{d=1}^{n} frac{d^2 F(lfloor frac{n}{d}
floor, lfloor frac{m}{d}
floor)}{d} = sum_{d=1}^{n} d F(lfloor frac{n}{d}
floor, lfloor frac{m}{d}
floor)
$$
考虑求$F(x,y)$
$$
egin{align}
F(x, y) & = sum_{i=1}^{x} sum_{j=1}^{y} ij[(i,j)=1] \
& = sum_{i=1}^{x} sum_{j=1}^{y} ij sum_{d|(i,j)} mu (d) \
& = sum_{d=1}^{x} mu (d) sum_{d|i}^{x} i sum_{d|j}^{y} j \
& = sum_{d=1}^{x} mu (d) d^2 sum_{i=1}^{lfloor frac{x}{d}
floor} i sum_{j=1}^{lfloor frac{y}{d}
floor} j \
& = sum_{d=1}^{x} mu (d) d^2 frac{lfloor frac{x}{d}
floor (lfloor frac{x}{d}
floor +1)}{2} frac{lfloor frac{y}{d}
floor (lfloor frac{y}{d}
floor +1)}{2} \
end{align}
$$
带回原式得
$$
egin{align}
& sum_{d=1}^{n} d F(lfloor frac{n}{d}
floor, lfloor frac{m}{d}
floor) \
= & sum_{d=1}^{n} d sum_{i=1}^{lfloor frac{n}{d}
floor} mu (i) i^2 frac{lfloor frac{lfloor frac{n}{d}
floor}{i}
floor (lfloor frac{lfloor frac{n}{d}
floor}{i}
floor +1)}{2} frac{lfloor frac{lfloor frac{m}{d}
floor}{i}
floor (lfloor frac{lfloor frac{m}{d}
floor}{i}
floor +1)}{2} \
= & sum_{d=1}^{n} d sum_{i=1}^{lfloor frac{n}{d}
floor} mu (i) i^2 frac{lfloor frac{n}{di}
floor (lfloor frac{n}{di}
floor +1)}{2} frac{lfloor frac{m}{di}
floor (lfloor frac{m}{di}
floor +1)}{2} \
end{align}
$$
现在已经可以$O(sqrt n sqrt n) = O(n)$单次查询了,但是不够理想,我们继续化简
令$T=di$,则$i|T, d=T/i$,换掉指标,得
$$
egin{align}
& sum_{d=1}^{n} d sum_{i=1}^{lfloor frac{n}{d}
floor} mu (i) i^2 frac{lfloor frac{n}{di}
floor (lfloor frac{n}{di}
floor +1)}{2} frac{lfloor frac{m}{di}
floor (lfloor frac{m}{di}
floor +1)}{2} \
= &
sum_{T=1}^{n} frac{lfloor frac{n}{T}
floor (lfloor frac{n}{T}
floor +1)}{2} frac{lfloor frac{m}{T}
floor (lfloor frac{m}{T}
floor +1)}{2} sum_{i|T} frac{T}{i} mu (i) i^2 \
= & sum_{T=1}^{n} frac{lfloor frac{n}{T}
floor (lfloor frac{n}{T}
floor +1)}{2} frac{lfloor frac{m}{T}
floor (lfloor frac{m}{T}
floor +1)}{2} T sum_{i|T} mu (i) i \
end{align}
$$
设$g(T)=T sum_{i|T} mu (i) i$,我们再设$f(T)=sum_{i|T} mu (i) i$那么$g(T)=Tf(T)$,考虑求$f(T)$
在线性筛中,外层为$k$,内层为$p_y$,所以求$f(kp_y)=sum_{i|kp_y} mu(i) i$
当$p_y|k$时
当$i$取的数的因子中不包含新加入的$p_y$时,答案就是$f(k)$
当$i$取包含新加入的因子$p_y$时,由于此时$p_y$指数已经$>=2$,所以$mu (i)=0$,因此贡献为0
综上,当$p_y|k$时,答案为$f(k)$
当$p_y
mid k$时
当$i$取的数的因子中不包含新加入的$p_y$时,同上,答案是$f(k)$
当$i$取的数的因子包含新加入的$p_y$时,由于指数为$1$,所以我们考虑$i=ap_y$,原式变为
$$
egin{align}
& sum_{i|T} mu(i) i \
= & sum_{ap_y|kp_y} mu (ap_y) ap_y \
= & p_y sum_{a|k} mu (a) mu(p_y) a \
= & -p_y sum_{a|k} mu(a) a \
= & -p_y f(k) \
end{align}
$$
综上,当$p_y mid k$时,答案为$(1-p_y)f(k)$
然后线性筛随便搞搞即可,最后答案就是
$$sum_{T=1}^{n} frac{lfloor frac{n}{T} floor (lfloor frac{n}{T} floor +1)}{2} frac{lfloor frac{m}{T} floor (lfloor frac{m}{T} floor +1)}{2} g(T) $$
分块搞搞就行了