3518: 点组计数
Time Limit: 20 Sec Memory Limit: 128 MBSubmit: 113 Solved: 62
[ Submit][ Status][ Discuss]
Description
平面上摆放着一个n*m的点阵(下图所示是一个3*4的点阵)。Curimit想知道有多少三点组(a,b,c)满足以a,b,c三点共线。这里a,b,c是不同的3个点,其顺序无关紧要。(即(a,b,c)和
(b,c,a)被认为是相同的)。由于答案很大,故你只需要输出答案对1,000,000,007的余数就可以了。
Input
有且仅有一行,两个用空格隔开的整数n和m。
Output
有且仅有一行,一个整数,表示三点组的数目对1,000,000,007的余
数。(1,000。000。007是质数)
Sample Input
3 4
Sample Output
2 0
HINT
对于100%的数据,1< =N.m< =50000
Source
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<bitset>
#define min(a,b) ((a) < (b) ? (a) : (b))
#include<ext/pb_ds/priority_queue.hpp>
using namespace std;
const int maxn = 5E4 + 50;
typedef long long LL;
const LL mo = 1000000007;
int n,m,Ans,tot,mu[maxn],pri[maxn],sum[maxn];
bool not_pri[maxn];
inline int Mul(const LL &x,const LL &y) {return x * y % mo;}
inline int Add(const int &x,const int &y) {return x + y < mo ? x + y : x + y - mo;}
inline int Dec(const int &x,const int &y) {return x - y >= 0 ? x - y : x - y + mo;}
int C(int N)
{
int A = N,B = N - 1,C = N - 2;
if (A % 2 == 0) A /= 2;
else if (B % 2 == 0) B /= 2;
else if (C % 2 == 0) C /= 2;
if (A % 3 == 0) A /= 3;
else if (B % 3 == 0) B /= 3;
else if (C % 3 == 0) C /= 3;
return Mul(A,Mul(B,C));
}
inline int Sum(int tmp,int N,int M)
{
int A = Mul(N,M);
int B = Mul(sum[N],tmp);
return Dec(A,B);
}
inline int Solve(int d,int N,int M)
{
int ret = 0;
for (int t = 1; t <= N; t++)
{
if (!mu[t]) continue;
int A = Sum(Mul(d,t),N / t,n);
int B = Sum(Mul(d,t),M / t,m);
ret = Add(ret,Mul(mu[t],Mul(A,B)));
}
return ret;
}
int Calc()
{
int ret = 0;
for (int d = 2; d <= n; d++)
ret = Add(ret,Mul(d - 1,Solve(d,n / d,m / d)));
return ret;
}
int main()
{
#ifdef DMC
freopen("DMC.txt","r",stdin);
#endif
cin >> n >> m;
if (n > m) swap(n,m); mu[1] = 1;
for (int i = 2; i < maxn; i++)
{
if (!not_pri[i])
pri[++tot] = i,mu[i] = mo - 1;
for (int j = 1; j <= tot; j++)
{
int Nex = i * pri[j];
if (Nex >= maxn) break;
not_pri[Nex] = 1;
if (i % pri[j] == 0) break;
mu[Nex] = Mul(mu[i],mo - 1);
}
}
for (int i = 1; i < maxn; i++) sum[i] = Add(i,sum[i - 1]);
Ans = Add(Mul(C(n),m),Mul(C(m),n));
Ans = Add(Ans,Mul(2,Calc())); cout << Ans << endl;
return 0;
}