首先咱們能夠發現,對於每個i,後一項的\(\sum_{j=1}^{m}(m \bmod j)\)都相同,合併提出,得:
\(\sum_{i=1}^{n} n \bmod i \sum_{j=1}^{m} m \bmod j (i!=j)\)
而後咱們能夠將其拆成兩部分:\(\sum_{i=1}^{n} n \bmod i\)和\(\sum_{j=1}^{m} m \bmod j\),
答案即爲這兩部分乘積,減去(i=j)的部分:
\((\sum_{i=1}^{n} n \bmod i)*(\sum_{j=1}^{m} m \bmod j)-\sum_{i=1}^{min(n,m)} (n \bmod i)*(m \bmod i)\)
將\(n \bmod i\)拆爲\(n-i[\frac{n}{i}]\),得:
\((\sum_{i=1}^{n}(n-i[\frac{n}{i}]))(\sum_{j=1}^{m}(m-j[\frac{m}{j}]))-\sum_{i=1}^{min(n,m)}(n-i[\frac{n}{i}])(m-i[\frac{m}{i}])\)
其中:
\(\sum_{i=1}^{n}(n-i[\frac{n}{i}])=n^2-\sum_{i=1}^{n} i[\frac{n}{i}]\)
\(\sum_{i=1}^{min(n,m)}(n-i[\frac{n}{i}])(m-i[\frac{m}{i}])=\sum_{i=1}^{min(n,m)}(nm-i(m[\frac{n}{i}]+n[\frac{m}{i}])+[\frac{n}{i}][\frac{m}{i}]i^2)\)
就變成:
\((n^2-\sum_{i=1}^{n} i[\frac{n}{i}])(m^2-\sum_{j=1}^{m} j[\frac{m}{j}])-\sum_{i=1}^{min(n,m)}(nm-i(m[\frac{n}{i}]+n[\frac{m}{i}])+[\frac{n}{i}][\frac{m}{i}]i^2)\)
這三部分分別用數論分塊作,注意:
\(\sum_{i=1}^n i^2=\frac{n(n+1)(2n+1)}{6}\)
\(\sum_{i=l}^{r} i^2=\sum_{i=1}^r i^2 -\sum_{i=1}^{l-1} i^2=\frac{r(r+1)(2r+1)}{6}-\frac{l(l-1)(2l-1)}{6}\)c++
#include<bits/stdc++.h> #define ll long long using namespace std; const ll mod=19940417,inv=3323403; ll n,m,l,r,t,p,q,ans=0,sum=0; ll sum1(ll l,ll r){return (r-l+1)*(r+l)/2%mod;}\\一次之和 ll sum2(ll x){return (x+1)*x%mod*(x+1+x)%mod*inv%mod;}\\平方之和 int main(){ scanf("%lld%lld",&n,&m),ans=n*n%mod,sum=m*m%mod; for(l=1;l<=m;l=r+1) r=m/(m/l),q=m/l,sum=(sum+mod-sum1(l,r)*q%mod)%mod;\\第一項 for(l=1;l<=n;l=r+1) r=n/(n/l),q=n/l,ans=(ans+mod-sum1(l,r)*q%mod)%mod;\\第二項 ans=ans*sum%mod,t=min(n,m),sum=n*m%mod*t%mod; for(l=1;l<=t;l=r+1) r=min(n/(n/l),m/(m/l)),p=m/l,q=n/l,sum=(sum+(sum2(r)-sum2(l-1)+mod)%mod*p%mod*q%mod+mod-sum1(l,r)*(n*p%mod+m*q%mod)%mod)%mod;\\第三項 ans=(ans-sum+mod)%mod,printf("%lld\n",ans); return 0; }