bzoj3505: [Cqoi2014]数三角形 [数论][gcd]

Description

给定一个nxm的网格,请计算三点都在格点上的三角形共有多少个。下图为4x4的网格上的一个三角形。

注意三角形的三点不能共线。

Input

输入一行,包含两个空格分隔的正整数m和n。

Output

输出一个正整数,为所求三角形数量。

Sample Input

2 2

Sample Output

76

数据范围
1<=m,n<=1000


太伤心了。。不能abs(int)???

首先格点个数是(n+1)*(m+1)的,所以我们先把n和m都+1。 先选出三个不同点,方案数是C(n*m,3)。 接下来扣掉三点共线的情况。 枚举两个点,计算以它们为端点的线段上的整点个数。 不难发现是gcd(x1-x2,y1-y2)-1。 线段是可以平移的,那么我们把其中一个点固定在(0,0),只枚举另一个点的坐标,然后乘上方案数就行了。 时间复杂度O(nm)

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 using namespace std;
 5 #define dbg(x) cout<<#x<<" = "<<x<<endl
 6
 7 typedef long long ll;
 8
 9 const int maxn=1005;
10
11 int n,m,f[maxn][maxn],ens;
12 ll ans;
13
14 ll C(int a,int b){
15     ll res=1;
16     for(int i=a;i>a-b;i--)  res*=i;
17     for(int i=b;i>1;i--)  res/=i;
18     return res;
19 }
20
21 int gcd(int a,int b){
22     if(f[a][b])  return f[a][b];
23     return b?(f[a][b]=gcd(b,a%b)):a;
24 }
25
26 int main(){
27     scanf("%d%d",&n,&m);
28     n++;  m++;
29     ans=C(n*m,3);
30     for(int i=-n+1;i<=n-1;i++)
31         for(int j=0;j<=m-1;j++){
32             //避免重复计算
33             if(!j&&i<=0)  continue;
34             ens=gcd(i<0?-i:i,j)-1;
35 //            dbg(abs(i));  dbg(j);  dbg(ens);
36             ans-=1ll*ens*(n-(i<0?-i:i))*(m-j);
37         }
38     printf("%lld\n",ans);
39     return 0;
40 }
时间: 2024-08-01 16:39:37

bzoj3505: [Cqoi2014]数三角形 [数论][gcd]的相关文章

Bzoj 3505: [Cqoi2014]数三角形 数论

3505: [Cqoi2014]数三角形 Time Limits: 1000 ms  Memory Limits: 524288 KB  Detailed Limits Description Input 输入一行,包含两个空格分隔的正整数m和n. Output 输出一个正整数,为所求三角形数量. Sample Input 输入1: 1 1 输入2: 2 2 Sample Output 输出1: 4 输出2: 76 Data Constraint 对于30%的数据 1<=m,n<=10 对于1

[bzoj3505 Cqoi2014] 数三角形 (容斥+数学)

传送门 Description 给定一个nxm的网格,请计算三点都在格点上的三角形共有多少个.下图为4x4的网格上的一个三角形. 注意三角形的三点不能共线. Input 输入一行,包含两个空格分隔的正整数m和n. Output 输出一个正整数,为所求三角形数量. Sample Input 2 2 Sample Output 76 HINT 1<=m,n<=1000 Solution 首先思路肯定是随意三个点方案-三点共线方案 随意三个点方案随意求 主要求三点共线: 有个神奇的结论:节点坐标gc

bzoj3505 [Cqoi2014]数三角形

题目链接 先算在n*m个点中任选3个的方案数,再减去三点共线的方案数 我为什么要做这种水题?因为我很弱 1 #include<algorithm> 2 #include<iostream> 3 #include<cstdlib> 4 #include<cstring> 5 #include<cstdio> 6 #include<string> 7 #include<cmath> 8 #include<ctime>

【排列组合】bzoj3505 [Cqoi2014]数三角形

http://blog.csdn.net/zhb1997/article/details/38474795 #include<cstdio> #include<algorithm> #include<iostream> using namespace std; typedef long long ll; int n,m; ll ans; int main() { // freopen("bzoj3505.in","r",stdin

【BZOJ3505】[Cqoi2014]数三角形 组合数

[BZOJ3505][Cqoi2014]数三角形 Description 给定一个nxm的网格,请计算三点都在格点上的三角形共有多少个.下图为4x4的网格上的一个三角形. 注意三角形的三点不能共线. Input 输入一行,包含两个空格分隔的正整数m和n. Output 输出一个正整数,为所求三角形数量. Sample Input 2 2 Sample Output 76 数据范围 1<=m,n<=1000 题解:显然要用补集法,我们只需要求出三点共线的方案数即可.方法是先枚举两端的点所形成的向

bzoj3505 / P3166 [CQOI2014]数三角形

P3166 [CQOI2014]数三角形 前置知识:某两个点$(x_{1},,y_{1}),(x_{2},y_{2})\quad (x_{1}<x_{2},y_{1}<y_{2})$所连成的线段穿过整点的个数为$gcd(x_{2}-x_{1},y_{2}-y_{1})-1$ “注意三角形的三点不能共线.” 暗示你可以处理出总方案再减去三点共线的方案. 显然,总方案就是在$(n+1)*(m+1)$个点中任选$3$个.于是$tot=C((n+1)*(m+1),3)$ 现在我们要算出三点共线的方案

BZOJ 3505: [Cqoi2014]数三角形( 组合数 )

先n++, m++ 显然答案就是C(3, n*m) - m*C(3, n) - n*C(3, m) - cnt. 表示在全部点中选出3个的方案减去不合法的, 同一行/列的不合法方案很好求, 对角线的不合法方案cnt比较麻烦. 枚举对角线(左下-右上), 即(0, 0)-(x, y), 我们发现这种情况有(n-y)*(m-x)*2(算上左上-右下的)种, 然后中间有gcd(x, y)-1个点(不合法), 乘起来就好了. ---------------------------------------

【BZOJ 3505】 [Cqoi2014]数三角形

3505: [Cqoi2014]数三角形 Time Limit: 10 Sec Memory Limit: 128 MB Submit: 664 Solved: 403 [Submit][Status][Discuss] Description 给定一个nxm的网格,请计算三点都在格点上的三角形共有多少个.下图为4x4的网格上的一个三角形. 注意三角形的三点不能共线. Input 输入一行,包含两个空格分隔的正整数m和n. Output 输出一个正整数,为所求三角形数量. Sample Inpu

3505: [Cqoi2014]数三角形

3505: [Cqoi2014]数三角形 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 1324  Solved: 807[Submit][Status][Discuss] Description 给定一个nxm的网格,请计算三点都在格点上的三角形共有多少个.下图为4x4的网格上的一个三角形. 注意三角形的三点不能共线. Input 输入一行,包含两个空格分隔的正整数m和n. Output 输出一个正整数,为所求三角形数量. Sample Inp