Triangles
Problem Description
How many triple of points A(xA,yA),B(xB,yB),C(xC,yC) A(xA,yA),B(xB,yB),C(xC,yC) which:
-
xA,yA,xB,yB,xC,yC∈Z xA,yA,xB,yB,xC,yC∈Z
-
0≤xA,xB,xC<n,0≤yA,yB,yC<m 0≤xA,xB,xC<n,0≤yA,yB,yC<m
-
S△ABC∉Z S△ABC∉Z? ( S△ S△ denotes the area of triangle)
Input
Two integers n n and m m.
( 1≤n,m≤109 1≤n,m≤109)
Output
The only integer denotes the number possible triples, modulo 109+7 109+7.
Sample Input
2 2
Sample Output
24
Hint
There are 4 4 triangles. Each of them is counted 6 6 times.
假设三角形的3的顶点分别为P1(x1,y1),P2(x2,y2),P3(x3,y3)
则向量a 就可以表示为(x2-x1,y2-y1)
向量b 就可以表示为(x3-x1,y3-y1)
根据二维向量叉积的运算
∣a×b|=(x2-x1)(y3-y1) - (y2-y1)(x3-x1)
则这个三角形的面积
S = |((x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1) ) / 2|
所以,算出每一个点四种情况的个数,然后深搜各个点的各种情况。
代码:
#pragma warning(disable:4996)
#include <iostream>
#include <functional>
#include <algorithm>
#include <cstring>
#include <vector>
#include <string>
#include <cstdio>
#include <cmath>
#include <queue>
#include <stack>
#include <deque>
#include <ctime>;
#include <set>
#include <map>
using namespace std;
typedef long long ll;
#define INF 0x3fffffffffffffff
const ll mod = 1e9 + 7;
const int maxn = 1e4 + 5;
ll n, m, res;
ll c[2][2];//c[0][0]代表都是偶数的情况数
void dfs(int num, int x1, int y1, int x2, int y2, int x3, int y3)
{
if (num == 4)
{
if (((x2 - x1) * (y3 - y1) - (y2 - y1) * (x3 - x1)) % 2 != 0)
{
res += (((c[x1 % 2][y1 % 2] * c[x2 % 2][y2 % 2])%mod * c[x3 % 2][y3 % 2]))%mod;
res %= mod;
}
}
else
{
int i, j;
for (i = 1; i <= 2; i++)
{
for (j = 1; j <= 2; j++)
{
if (num == 1)
{
dfs(num + 1, i, j, 3, 3, 3, 3);
}
else if (num == 2)
{
dfs(num + 1, x1, y1, i, j, 3, 3);
}
else if (num == 3)
{
dfs(num + 1, x1, y1, x2, y2, i, j);
}
}
}
}
}
void solve()
{
ll i, j, k, x, y;
scanf("%lld%lld", &n, &m);
for (i = 0; i < 2; i++)
{
for (j = 0; j < 2; j++)
{
if (i == 0)
x = (n - 1) / 2 + 1;
else
x = n / 2 ;
if (j == 0)
y = (m - 1) / 2 + 1;
else
y = m / 2;
c[i][j] = (x*y) % mod;
}
}
res = 0;
dfs(1, 3, 3, 3, 3, 3, 3);
printf("%lld\n", res);
}
int main()
{
//#ifndef ONLINE_JUDGE
// freopen("i.txt", "r", stdin);
// freopen("o.txt", "w", stdout);
//#endif
solve();
return 0;
}