题解:AT_abc414_e [ABC414E] Count A%B=C
_Lerche_
·
·
题解
感觉十分顺理成章的题目。
首先有个结论:a>b>c。
- $a<b$:此时 $a\bmod b=a$,即 $c=a$,不符合 $a,b,c$ 两两不同的条件。
- $a=b$:此时 $a\bmod b=0$,即 $c=0$,不符合 $1\le a,b,c\le n$ 的条件。
综上,有 $a>b$,即 $a>b>c$。
我们可以枚举 $a$,再枚举 $b\in [1,a-1]$,最后判断 $c$ 是否合法,因为 $a>b>c$,所以 $c$ 不可能等于 $a,b$,显然其不可能超过 $n$,因此唯一需要判断的就是 $c=0$。
若 $c=0$,则 $b$ 为 $a$ 的因数,我们枚举 $a$,则 $a$ 为答案的贡献就是 $a$ 减去她本身的因数个数。但 $n\le 10^{12}$,暴力枚举会 TLE,所以推一下式子。
$$\sum_{a=1}^n\left(a-\sum_{b=1}^a[b|a]\right)$$
$$\sum_{a=1}^n a-\sum_{a=1}^n\sum_{b=1}^a[b|a]$$
$$\dfrac{n(n+1)}{2} a-\sum_{b=1}^n\sum_{a=1}^n[b|a]$$
$$\dfrac{n(n+1)}{2} a-\sum_{b=1}^n\left\lfloor \dfrac{n}{b}\right\rfloor$$
前半部分是等差数列求和公式,后半部分可以整除分块,时间复杂度 $O(\sqrt{n})$,因为 $n$ 太大了,所以代码全部开了 __int128。
```cpp
#include <iostream>
#include <cstdio>
using namespace std;
const int mod=998244353;
typedef long long ll;
__int128 ans,n,l,r;
ll res,N;
int main(){
cin>>N;n=N;ans=n*(n+1)/2;
for(l=1;l<=n;l=r+1){
r=min(n,n/(n/l));
ans-=(r-l+1)*(n/l);
}
res=(ans%mod+mod)%mod;
cout<<res<<endl;
return 0;
}
```