zoj 3647 智商题

此题就是求格点中三角形的个数。

就是找出三点不共线的个数。

n*m的矩形中有(n+1)*(m+1)个格点。

选出三个点的总个数为:C((n+1)*(m+1),3).

减掉共线的情况就是答案了。

首先是水平和垂直共线的情况:C(n+1,3)*(m+1)+C(m+1,3)*(n+1);

然后斜的共线的情况就是枚举矩形。

斜着共线的三点用枚举法n*m的矩形,对角两个点中间共有gcd(m,n)-1个点,两条对角线,所以数量*2,大矩形里共有(N-n+1)*(M-m+1)个的矩形,一并去除

 1 #include<stdio.h>
 2 #include<iostream>
 3 #include<string.h>
 4 #include<algorithm>
 5 using namespace std;
 6
 7 int gcd(int a,int b)
 8 {
 9     if(b==0)return a;
10     return gcd(b,a%b);
11 }
12 long long Com(int n,int r)
13 {
14     if(n<r)return 0;//这个一定要
15     if(n-r<r)r=n-r;
16     int i,j;
17     long long ret=1;
18     for(i=0,j=1;i<r;i++)
19     {
20         ret*=(n-i);
21         for(;j<=r&&ret%j==0;j++)ret/=j;
22     }
23     return ret;
24 }
25 int main()
26 {
27     int n,m;
28     while(scanf("%d%d",&n,&m)!=EOF)
29     {
30         long long  ans=Com((n+1)*(m+1),3);//选三个点的所有组合数
31         for(int i=2;i<=n;i++)
32           for(int j=2;j<=m;j++)
33           {
34               ans-=(long long)(gcd(i,j)-1)*(n-i+1)*(m-j+1)*2;
35           }
36         ans-=Com(n+1,3)*(m+1);
37         ans-=Com(m+1,3)*(n+1);
38         printf("%lld\n",ans);//ZOJ用lld,不能用I64d
39     }
40     return 0;
41 }
时间: 2024-08-26 20:46:07

zoj 3647 智商题的相关文章

考考你、智商题 小明借爸爸500元 又借妈妈500元 买双鞋 970元 还剩30元 还给爸爸10元

考考你.智商题小明借爸爸500元又借妈妈500元买双鞋 970元还剩30元还给爸爸10元又还妈妈10元 自己留10元欠妈妈490 欠爸爸490490+490=980 加上自己的10元=990请问那10元去哪了 解答:还了10元,实际借款就不是500了,是490元.总借款现在已经不是1000元,而是980元.买鞋970元,还剩下10元.

ZOJ 3647 Gao the Grid dp,思路,格中取同一行的三点,经典 难度:3

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4837 三角形的总数=格子中任取3个点的组合数-同一横行任取3个点数目-同一纵行任取3个点数目-同一斜直线上任取3个点数目 同一横行和同一纵行都好求 同一斜行的距离最远的点必然是一个矩形的两个端点,设矩形的长宽为l,w,中间可以取的点数则是(gcd(l,w)-1),左上角或左下角的起点反而不重要. 能够取到该矩形的可能是 (n-l+1)*(m-w+1),因为左上角作为起点或左下

zoj 3657 策略题 容易

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4880 因为是要去牡丹江,是浙大出题,所以找了份浙大的题,第一道水题做的就不顺啊,题看不明白,然后枚举3个数的组合,循环条件居然写错,二逼啊,这到现场肯定悲剧啊 题意: 一共有5座山,有人拿5个篮子去采蘑菇,现在他已经采了几座山上的蘑菇,之后几座山的蘑菇数量你可以自己确定.但是他要交出3个篮子,且它们的和必须是1024的倍数.否则,剩余两个篮子也要交出.之后,如果剩余数量大于1

ZOJ 3647(格点上的三角形数目)

这道题目说起来真的是忧伤啊,比赛一开始的时候就开始做做了一场也没有做出来-. 题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3647 题意: 就是在格点网络上数三角形. 思路:  思路就是先做一个C(3, n*m) 然再把三点共线的情况全部减掉. 关键就是如果找三点共线的情况, 最常规的思路是枚举矩形(也就是枚举三点中的两个点),然后找去对角线上有几个点(枚举第三个点),再来判这个小矩形在整个大矩形中最多出现

poj1426--Find The Multiple(广搜,智商题)

Find The Multiple Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18527   Accepted: 7490   Special Judge Description Given a positive integer n, write a program to find out a nonzero multiple m of n whose decimal representation contains

zoj 3672 思维题

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4915 真是老了,脑子不会动了,但是其实就算现在搜了题解A了,还是没总结出思维方式 三点: 1.segma(a[i]-b[i])必须是偶数,,因为其实每次操作都是相当于从segma(a[i]-b[i])里面减去2*delta 2.a[i]>=b[i] 题目说的很清楚,只能减去,所以这点必须满足 前两点都想到了,但是自己能举出反例,后来队友A掉了 3.max(a[i]-b[i])

ZOJ 3647 Gao the Grid(居然是暴力)

A n * m grid as follow: Count the number of triangles, three of whose vertice must be grid-points. Note that the three vertice of the triangle must not be in a line(the right picture is not a triangle). Input The input consists of several cases. Each

zoj 3726 水题+二分

Alice's Print Service Time Limit: 2 Seconds      Memory Limit: 65536 KB Alice is providing print service, while the pricing doesn't seem to be reasonable, so people using her print service found some tricks to save money. For example, the price when

CF #355div2 D 宝藏与钥匙 dp 二维数组智商题

D. Vanya and Treasure time limit per test 1.5 seconds memory limit per test 256 megabytes input standard input output standard output Vanya is in the palace that can be represented as a grid n × m. Each room contains a single chest, an the room locat