• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
mhy12345
博客园    首页    新随笔    联系   管理    订阅  订阅

bzoj 3505: [Cqoi2014]数三角形 组合数学

3505: [Cqoi2014]数三角形

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 478  Solved: 293
[Submit][Status]

Description

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

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

Input

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

Output


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

Sample Input


2 2

Sample Output

76


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

HINT

 

Source

   这道题我居然白痴得卡在了求三点共线组数上,最开始我的想法是枚举每一个斜率的直线,计算他们的条数与交点,但是至今我还是不知道怎么弄,正确的方法应该是枚举三点共线两端点的坐标差,然后可轻而易举求出这样的端点对数,中间点的位置数,即可解此题。

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;
#define MAXN 10000
typedef long long qword;
int gcd(int x,int y)
{
        return (x%y==0)?y:gcd(y,x%y);
}
int main()
{
        freopen("input.txt","r",stdin);
//        freopen("output.txt","w",stdout);
        int i,j,k,x,y,z;
        int n,m;
        scanf("%d%d",&n,&m);
        n++;m++;
        int tot=0;
        qword ans0,ans1=0;
        ans0=(qword)(m*n-2)*(m*n-1)*m*n/6;
        for (i=1;i<n;i++)
        {
                for (j=1;j<m;j++)
                {
                        ans1+=(qword)(gcd(i,j)-1)*(n-i)*(m-j);
                }
        }
        ans1*=2;
        ans1+=(qword)(n-2)*(n-1)*n/6*m;
        ans1+=(qword)(m-2)*(m-1)*m/6*n;
        printf("%lld\n",ans0-ans1);
}

 

 
by mhy12345(http://www.cnblogs.com/mhy12345/) 未经允许请勿转载

本博客已停用,新博客地址:http://mhy12345.xyz

posted @ 2014-10-25 11:50  mhy12345  阅读(345)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3