「NOI2016」循环之美

$a\equiv a \times k^x\pmod b$

$k^x\equiv 1\pmod b$

$$(k,b)=1$$，那么由欧拉定理，有解$$x=\varphi(x)$$

$\sum_{i=1}^m\sum_{j=1}^n[(i,k)=1][(i,j)=1]$

\begin{aligned} &\sum_{i=1}^m\sum_{j=1}^n[(i,k)=1][(i,j)=1]\\ =&\sum_{i=1}^m[(i,k)=1]\sum_{j=1}^n\sum_{d=1}^{\min(i,j)}\mu(d)[d|i\land d|j]\\ =&\sum_{d=1}^{\min(n,m)}\mu(d)\lfloor\frac{n}{d}\rfloor\sum_{i=1}^m[(i,k)=1\land d|i]\\ =&\sum_{d=1}^{\min(n,m)}\mu(d)\lfloor\frac{n}{d}\rfloor\sum_{i=1}^{\lfloor\frac{m}{d}\rfloor}[(di,k)=1]\\ =&\sum_{d=1}^{\min(n,m)}\mu(d)[(d,k)=1]\lfloor\frac{n}{d}\rfloor\sum_{i=1}^{\lfloor\frac{m}{d}\rfloor}[(i,k)=1]\\ \end{aligned}

$is(i)=[(i,k)=1]\\ f(i)=\sum_{j=1}^iis(j)$

$\sum_{d=1}^{\min(n,m)}\mu(d)is(d\bmod k)\lfloor\frac{n}{d}\rfloor(\lfloor\frac{m}{dk}\rfloor f(k)+f(\lfloor\frac{m}{d}\rfloor \bmod k))$

$F(n)=\lfloor\frac{n}{k}\rfloor f(k)+f(n\bmod k)$

$\sum_{d=1}^{\min(n,m)}\mu(d)[(d,k)=1]\lfloor\frac{n}{d}\rfloor F(\lfloor\frac{m}{d}\rfloor)$

$g(n,k)=\sum_{d=1}^n\mu(d)[(d,k)=1]$

\begin{aligned} g(n,k)=&\sum_{d=1}^n\mu(d)[(d,k)=1]\\ =&\sum_{d=1}^n\mu(d)\sum_{p=1}^{\min(d,k)}\mu(p)[p|d\land p|k]\\ =&\sum_{p|k}\mu(p)\sum_{p|d}^n\mu(d)\\ =&\sum_{p|k}\mu(p)\sum_{d=1}^{\lfloor\frac{n}{p}\rfloor}\mu(dp)\\ \mathbb{because \ of}& \ [\mu(dp)\not=0]=[(d,p)=1],\mathbb{so}\\ g(n,k)=&\sum_{p|k}\mu(p)\sum_{d=1}^{\lfloor\frac{n}{p}\rfloor}\mu(dp)[(d,p)=1]\\ =&\sum_{p|k}\mu^2(p)\sum_{d=1}^{\lfloor\frac{n}{p}\rfloor}\mu(d)[(d,p)=1]\\ =&\sum_{p|k}\mu^2(p)g(\lfloor\frac{n}{p}\rfloor,p) \end{aligned}

$g(n,1)=\sum_{d=1}^n\mu(d),g(0,k)=0$

Code:

#include <cstdio>
#include <cctype>
#include <map>
#include <algorithm>
#define ll long long
using std::min;
const int SIZE=1<<21;
char ibuf[SIZE],*iS,*iT;
#define gc() getchar()
template <class T>
{
x=0;char c=gc();
while(!isdigit(c)) c=gc();
while(isdigit(c)) x=x*10+c-'0',c=gc();
}
const int N=5e6+1;
int mu[N],pri[N],ispri[N],fmu[N],cnt,toki[2020],aya[2020];
void init()
{
fmu[1]=mu[1]=1;
for(int i=2;i<N;i++)
{
if(!ispri[i])
{
pri[++cnt]=i;
mu[i]=-1;
}
for(int j=1;j<=cnt&&i*pri[j]<N;j++)
{
ispri[i*pri[j]]=1;
if(i%pri[j]) mu[i*pri[j]]=-mu[i];
else break;
}
fmu[i]=fmu[i-1]+mu[i];
}
}
int gcd(int a,int b){return b?gcd(b,a%b):a;}
std::map <std::pair<int,int>,int> saki;
int g(int x,int k)
{
//if(x<=1) return n;
if((k==1&&x<N)||(!x)) return fmu[x];
std::pair<int,int> now=std::make_pair(x,k);
if(saki[now]) return saki[now];
int ret=0;
if(k==1)
{
ret=1;
for(int l=2,r;l<=x;l=r+1)
{
r=x/(x/l);
ret-=g(x/l,k)*(r+1-l);
}
}
else
{
for(int i=1;i*i<=k;i++)
{
if(k%i) continue;
if(mu[i]) ret+=g(x/i,i);
if(i*i!=k&&mu[k/i]) ret+=g(x/(k/i),k/i);
}
}
return saki[now]=ret;
}
int n,m,k;
int F(int x){return (x/k)*toki[k]+toki[x%k];}
int main()
{
init();
for(int i=1;i<=k;i++)
toki[i]=toki[i-1]+(gcd(k,i)==1);
ll las=0,now,ans=0;
for(int l=1,r;l<=min(n,m);l=r+1)
{
r=min(n/(n/l),m/(m/l));
ans+=1ll*((now=g(r,k))-las)*(n/l)*F(m/l);
las=now;
}
printf("%lld\n",ans);
return 0;
}


2019.5.30

posted @ 2019-05-30 08:41  露迭月  阅读(391)  评论(0编辑  收藏  举报