题意:求如题目所给的式子的值。
思路:由性质可得
f(i+j∗k,j)和f(i,j)
相同,此式子易得。而这两个式子中的c也相同。
(i+(k+d∗c)∗j)∗jc∗g∗g,其中0<=k<c
对于一组(i,j),有一个特定c然后可以求这个c的循环节,然后在凑上多余的数随便搞搞。
具体看代码
#include<bits/stdc++.h>
using namespace std;
long long n,m,p;
long long a[711][711];
int v[711][711];
long long gcd[711][711];
long long tmp[30];
void f(long long x,long long y) {
long long xx=x,yy=y;
int c=0;
long long tt;
while(y>0) {
c++;
tt=x%y;
x=y;
y=tt;
}
a[xx][yy]=c*x*x;
v[xx][yy]=c;
gcd[xx][yy]=x;
}
void init(void) {
for(int j=1;j<=670;j++) {
for(int i=1;i<=670;i++) {
f(i,j);
}
}
}
int main()
{
init();
int t;
scanf("%d",&t);
while(t--) {
scanf("%lld%lld%lld",&n,&m,&p);
long long ans=0;
for(long long j=1;j<=m;j++) {
for(long long i=1;i<=j;i++) {
long long tot=0;
int c=v[i][j];
long long g=gcd[i][j];
long long tt0=n/j;
if(i<=(n%j)) tt0++;
long long tt=tt0/c;
long long sum=0;
for(int k=0;k<c;k++) {
tmp[k]=(i+j*k)*j/a[i][j];
tmp[k]%=p;
sum+=tmp[k];
sum%=p;
}
tot+=sum*tt%p;
tot%=p;
long long ss=j*j/g/g;
tot+=tt*(tt-1)/2%p*c%p*ss%p;
tot%=p;
for(int k=0;k<tt0-tt*c;k++) {
tot+=tmp[k];
tot%=p;
tot+=tt*ss%p;
tot%=p;
}
ans+=tot;
ans%=p;
}
}
printf("%lld\n",ans);
}
return 0;
}