# 【BZOJ】2154: Crash的数字表格

http://www.lydsy.com/JudgeOnline/problem.php?id=2154

#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\n", ((ans%MD)+MD)%MD);
return 0;
}


sb错1：忘记最后除法不一定是整数QAQ，那么一定要在中途能保证是整数的就算出来啊= =后边算不出来的啊喂。。

sb错2：各种爆longlong让我十分无语，取太多mod又会很慢= =（看来为了保险起见...以后哪里要mod就mod...不要乱估计啦。。。

（我真的有不想写推导公式的过程的冲动QAQ

$$\sum_{i=1}^{n} \sum_{j=1}^{m} lcm(i, j) = \sum_{i=1}^{n} \sum_{j=1}^{m} \frac{ij}{(i,j)}$$

$$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} \rfloor, \lfloor \frac{m}{d} \rfloor)}{d} = \sum_{d=1}^{n} d F(\lfloor \frac{n}{d} \rfloor, \lfloor \frac{m}{d} \rfloor)$$

\begin{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} \rfloor} i \sum_{j=1}^{\lfloor \frac{y}{d} \rfloor} j \\ & = \sum_{d=1}^{x} \mu (d) d^2 \frac{\lfloor \frac{x}{d} \rfloor (\lfloor \frac{x}{d} \rfloor +1)}{2} \frac{\lfloor \frac{y}{d} \rfloor (\lfloor \frac{y}{d} \rfloor +1)}{2} \\ \end{align}

\begin{align} & \sum_{d=1}^{n} d F(\lfloor \frac{n}{d} \rfloor, \lfloor \frac{m}{d} \rfloor) \\ = & \sum_{d=1}^{n} d \sum_{i=1}^{\lfloor \frac{n}{d} \rfloor} \mu (i) i^2 \frac{\lfloor \frac{\lfloor \frac{n}{d} \rfloor}{i} \rfloor (\lfloor \frac{\lfloor \frac{n}{d} \rfloor}{i} \rfloor +1)}{2} \frac{\lfloor \frac{\lfloor \frac{m}{d} \rfloor}{i} \rfloor (\lfloor \frac{\lfloor \frac{m}{d} \rfloor}{i} \rfloor +1)}{2} \\ = & \sum_{d=1}^{n} d \sum_{i=1}^{\lfloor \frac{n}{d} \rfloor} \mu (i) i^2 \frac{\lfloor \frac{n}{di} \rfloor (\lfloor \frac{n}{di} \rfloor +1)}{2} \frac{\lfloor \frac{m}{di} \rfloor (\lfloor \frac{m}{di} \rfloor +1)}{2} \\ \end{align}

\begin{align} & \sum_{d=1}^{n} d \sum_{i=1}^{\lfloor \frac{n}{d} \rfloor} \mu (i) i^2 \frac{\lfloor \frac{n}{di} \rfloor (\lfloor \frac{n}{di} \rfloor +1)}{2} \frac{\lfloor \frac{m}{di} \rfloor (\lfloor \frac{m}{di} \rfloor +1)}{2} \\ = & \sum_{T=1}^{n} \frac{\lfloor \frac{n}{T} \rfloor (\lfloor \frac{n}{T} \rfloor +1)}{2} \frac{\lfloor \frac{m}{T} \rfloor (\lfloor \frac{m}{T} \rfloor +1)}{2} \sum_{i|T} \frac{T}{i} \mu (i) i^2 \\ = & \sum_{T=1}^{n} \frac{\lfloor \frac{n}{T} \rfloor (\lfloor \frac{n}{T} \rfloor +1)}{2} \frac{\lfloor \frac{m}{T} \rfloor (\lfloor \frac{m}{T} \rfloor +1)}{2} T \sum_{i|T} \mu (i) i \\ \end{align}

\begin{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}

$$\sum_{T=1}^{n} \frac{\lfloor \frac{n}{T} \rfloor (\lfloor \frac{n}{T} \rfloor +1)}{2} \frac{\lfloor \frac{m}{T} \rfloor (\lfloor \frac{m}{T} \rfloor +1)}{2} g(T)$$

posted @ 2015-02-02 22:59  iwtwiioi  阅读(1306)  评论(6编辑  收藏