BZOJ3505: [Cqoi2014]数三角形 解题报告

32 篇文章 0 订阅

本来不难的一题,发现我不会算两点线段间整点数…..
这道题可以转化一下,求出所有三角形数量,减去三点共线的三角形数
求三点共线的三角形数的话,枚举 n,m 得到一个线段,以 (1,1),(n,m) 为三角形的两个顶点,1*剩余线段上的点就是这个线段的贡献,再乘上共有多少个贡献相同的线段(对于一条线段 (1,1),(n,m) (n,1),(1,m) 和他也是相同的)
怎么求线段间整点数呢?
这里写图片描述
这是一条线段,横着看有x个间隔,竖着看有y个间隔,那么从 (0,0) 沿线段走, x 改变了a y 就改变了ayx,要使得这是个整点, ayx 需要是整数,所以 a 能取的值是kxgcd(x,y)所以整点数是 xxgcd(x,y)+1 ,即 gcd(x,y)+1




code:

#include<set>
#include<map>
#include<deque>
#include<queue>
#include<stack>
#include<ctime>
#include<cmath>
#include<vector>
#include<string>
#include<bitset>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<climits>
#include<complex>
#include<iostream>
#include<algorithm>
#define ll long long
using namespace std;

const int maxn = 1010;

int n,m;
ll ans,N;

int gcd( int a,int b )
{
    return a==0?b:gcd(b%a,a);
}

int main()
{   
    scanf("%d%d",&n,&m); 
    n++; m++; N = n*m;
    ans = N*(N-1)*(N-2)/6;

    ans -= N*(m-1)*(m-2)/6;
    ans -= N*(n-1)*(n-2)/6;

    for( int i=n;i>=2;i-- )
    {
        for( int j=m;j>=2;j-- )
        {
            ll temp = gcd( i-1,j-1 )-1;
            ans -= temp*(n-i+1)*(m-j+1)*2;
        }
    }

    printf("%lld\n",ans);

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值