Description
给定 (n,a,c le 10^9),求 (sumlimits_{i=0}^nlfloorfrac{a imes i}c floor),保证 (c|n)。
Solution
化简原式,得
[egin{aligned}
&sumlimits_{i=0}^nlfloorfrac{a imes i}c
floor\
=&sumlimits_{i=0}^nfrac{a imes n}c-lceilfrac{a imes(n-i)}c
ceil\
=&sumlimits_{i=0}^nfrac{a imes n}c-lceilfrac{a imes i}c
ceil\
=&sumlimits_{i=0}^nfrac{a imes n}c-(lfloorfrac{a imes i}c
floor+1-[c|(a imes i)])\
=&(n+1)(frac{a imes n}c-1)+sumlimits_{i=0}^n[c|(a imes i)]-sumlimits_{i=0}^nlfloorfrac{a imes i}c
floor\
=&(n+1)(frac{a imes n}c-1)+frac n{frac c{gcd(a,c)}}+1-sumlimits_{i=0}^nlfloorfrac{a imes i}c
floor\
=&frac nc imes(a imes(n+1)-c+gcd(a,c))-sumlimits_{i=0}^nlfloorfrac{a imes i}c
floor\
=&frac{frac nc imes(a imes(n+1)-c+gcd(a,c))}2
end{aligned}
]
#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 1000005;
const int mod = 1e9+7;
int qpow(int p,int q)
{
return (q&1?p:1)*(q?qpow(p*p%mod,q/2):1)%mod;
}
int inv(int p)
{
return qpow(p,mod-2);
}
signed main()
{
int n,a,c;
cin>>n>>a>>c;
int ans=a*(n+1)%mod-c+__gcd(a,c);
ans%=mod;
ans+=mod;
ans%=mod;
ans*=n/c;
ans%=mod;
ans*=inv(2);
ans%=mod;
cout<<ans<<endl;
//system("pause");
}