We define a function gay(i)gay(i):
displaystyle gay(i)=left{ egin{array}{rcl} & 0, quad& if quad i = k*x*x, x > 1, k geq 1 \ & i * i ,quad& else end{array} ight.gay(i)={0,i∗i,ifi=k∗x∗x,x>1,k≥1else
Now your task is to calculate
displaystyle sum_{num=1}^n (sum_{i=1}^{num} gay(i)) mod pnum=1∑n(i=1∑numgay(i))modp
Input
Multiple test cases. Please use EOF.
In each test case, there are two integers nn, pp, which are described above.
1 leq n leq 10^{10},1 leq p leq 10^{11}1≤n≤1010,1≤p≤1011.
The number of test cases is no more than 100100.
Output
For each test case print the answer in one line.
样例输入
1 10
8 19230817
样例输出
1
396
SOLUTION:
看到完全平方因子可以想到 莫比乌斯函数 ,当完全平方因子的时候,mu为0
这个题让求所有的不含完全平方因子的数,容斥一下,
对于 关于完全平方因子的容斥,枚举平方因子,其容斥系数为mu
这篇写的不错: https://blog.csdn.net/qkoqhh/article/details/82532516#commentsedit
CODE:
#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ll;
const ll maxn=1e5+10;
pair<__int128,__int128> p[maxn];
ll mod;
bool vis[maxn];
int prime[maxn];
int mu[maxn];
void init(){
mu[1]=1;
int tot=0;
for(int i=2;i<maxn;i++){
if(!vis[i]){
prime[tot++]=i;
mu[i]=-1;
}
for(int j=0;j<tot;j++){
if(i*prime[j]>maxn) break;
vis[i*prime[j]]=true;
if(i%prime[j]==0){
mu[i*prime[j]]=0;
break;
}else{
mu[i*prime[j]]=-mu[i];
}
}
}
}
ll pingfang(ll n){
__int128 x=n,y=n+1,z=2*n+1;
if(x%2==0) x/=2;
else if(y%2==0) y/=2;
else z/=2;
if(x%3==0) x/=3;
else if(y%3==0) y/=3;
else z/=3;
return (x%mod*y%mod*z%mod);
}
ll lifang(ll n){
__int128 x=n,y=n+1;
if(x%2==0) x/=2;
else y/=2;
return x%mod*x%mod*y%mod*y%mod;
}
int main(){
ll n;
init();
while(scanf("%lld%lld",&n,&mod)!=EOF){
for(ll i=0;i*i<=n;i++){
p[i].first=i%mod*i%mod*i%mod*i%mod;
p[i].second=i%mod*i%mod*i%mod*i%mod*i%mod*i%mod;
}
ll ans=0;
for(ll i=1;i*i<=n;i++){
ans+=mod+(n+1)%mod*p[i].first%mod*pingfang(n/(i*i))%mod*mu[i]-p[i].second%mod*lifang(n/(i*i))%mod*mu[i];
ans=(ans+mod)%mod;
}
printf("%lld
",ans);
}
}