UVALive 3295 Counting Triangles

题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1296

UVALive 3295  Counting Triangles

UVALive 3295  Counting Triangles

题意:给出一个a*b的网格,在网格上取不共线的三点构成三角形,求三角形总数。
分析:就是一一道简单的组合数计算题目,设总结点数为n,则取三个节点的个数为C(n,3),

然后减去横向、竖向、斜向的三点共线的个数即可,斜线三点共线等价于所枚举的矩形的长宽成倍数关系,即gcd不为1

 #include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
#include <cstdlib>
#include <iomanip>
#include <cmath>
#include <ctime>
#include <map>
#include <set>
#include <queue>
using namespace std;
#define lowbit(x) (x&(-x))
#define max(x,y) (x>y?x:y)
#define min(x,y) (x<y?x:y)
#define MAX 100000000000000000
#define MOD 1000000007
#define pi acos(-1.0)
#define ei exp(1)
#define PI 3.141592653589793238462
#define INF 0x3f3f3f3f3f
#define mem(a) (memset(a,0,sizeof(a)))
typedef long long ll;
ll gcd(ll a,ll b){
return b?gcd(b,a%b):a;
}
bool cmp(int x,int y)
{
return x>y;
}
const int N=;
const int mod=1e9+;
int main(){
ll a, b;
int cas = ;
while(scanf("%lld%lld", &a, &b)!=EOF && (a||b)){
ll n = (a+)*(b+);
ll sum1 = n*(n-)*(n-)/; //C(n,3)
ll sum2 = (b+)*(a+)*a*(a-)/ + (a+)*(b+)*b*(b-)/; //横向或竖向三点共线的个数
ll sum3 = ; //斜线上三点共线的个数的一半
int i, j;
for(i=; i<=a; i++)
for(j=; j<=b; j++)
sum3 += (gcd(i,j)-) * (a-i+) * (b-j+);
ll ans = sum1 - *sum3 - sum2;
printf("Case %d: %lld\n", cas++, ans);
}
return ;
}
上一篇:React Native 项目整合 CodePush 全然指南


下一篇:Python的特殊成员