求助卡常,80TLE
查看原帖
求助卡常,80TLE
326797
Suffix_Sum楼主2023/5/27 17:35

本地跑过了,你谷上卡不过

#include<bits/stdc++.h>
#include<unordered_map>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/hash_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define int long long
int n,N;
int qzh[10000005];
signed mob[10000005];
signed zs[10000005];
bool flag[10000005];
gp_hash_table<long long,long long>m;
int ny6;
signed dfs(const int x)
{
	const int NN=N;
	if(x<=10000000)
	{
		return qzh[x]; 
	}
	if(m.find(x)!=m.end())
	{
		return m[x];
	}
	int ans=1;
	for(int i=2,j;i<=x;i=j+1)
	{
		j=(x/(x/i));
		const int modi=(i-1)%NN,modj=j%NN;
		//i^2+...+j^2;
		ans-=((modj*(modj+1)%NN*(2*modj+1)%NN-modi*(modi+1)%NN*(2*modi+1)%NN)*ny6%NN)*dfs(x/i)%NN;
	}
	ans%=NN;
	if(ans<0)
	{
		ans+=NN;
	}
	m[x]=ans;
	return ans;
 } 
int ksm(int x,int c)
{
	if(c==0)
	{
		return 1;
	}
	if(c%2==0)
	{
		int tmp=ksm(x,c/2);
		tmp*=tmp;
		tmp%=N;
		return tmp;
	}
	else
	{
		int tmp=ksm(x,c/2);
		tmp*=tmp;
		tmp%=N;
		tmp*=x;
		tmp%=N;
		return tmp; 
	}
}
int cnt=0;
signed main()
{
	cin>>N>>n;
	const int NN=N;
	const int nn=n;
	ny6=ksm(6,NN-2);
	const int ny4=ksm(4,NN-2);
	mob[1]=1;
	qzh[1]=1;
	for(int i=2;i<=10000000;i++)
	{
		if(flag[i]==0)
		{
			zs[++cnt]=i;
			mob[i]=-1;
		}
		qzh[i]=(qzh[i-1]+i*i*mob[i])%NN;
		if(qzh[i]<0)
		{
			qzh[i]+=NN;
		} 
		for(int j=1,tt;j<=cnt&&(tt=i*zs[j])<=10000000;j++)
		{
			flag[tt]=1;
			if(i%zs[j]==0)
			{
				break;
			}
			mob[tt]=-mob[i];
		}
	}
	int ans=0;
	for(int i1=1,j1;i1<=nn;i1=j1+1)
	{
		const int x1=nn/i1;
		j1=nn/x1;
		cnt=0;
		int last=0;
		for(int i2=1,j2;i2<=x1;i2=j2+1)
		{
			const int tmp=x1/i2;
			j2=x1/tmp;
			const int tt=(tmp+1)*tmp/2%NN;
			//cout<<":"<<i2<<" "<<j2<<endl;
			const int pp=dfs(j2);
			cnt+=(pp-last+NN)*(tt*tt%NN)%NN;
			last=pp;
		}
		cnt%=NN;
		//cout<<"::"<<i1<<" "<<cnt<<" "<<tt<<endl;
		const int modj1=j1%NN,modi1=i1%NN;
		const int ppj=modj1*(modj1+1)%NN;
		const int ppi=(modi1-1)*modi1%NN;
		const int tt=((ppj-ppi)*(ppj+ppi)%NN)*ny4%NN;
		ans+=tt*cnt%NN;
	}
	ans%=NN;
	cout<<ans<<endl;
	return 0;
}
2023/5/27 17:35
加载中...