• BZOJ 3505: [Cqoi2014]数三角形 数学


    3505: [Cqoi2014]数三角形

    Time Limit: 1 Sec  

    Memory Limit: 256 MB

    题目连接

    http://www.lydsy.com/JudgeOnline/problem.php?id=3505

    Description

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

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

    Input

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

    Output

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

    Sample Input

    2 2

    Sample Output

    76

    HINT

    1<=m,n<=1000

    题意

    题解:

    任意选择三个不在同一条直线上的三个点即是满足题意的点

    考虑补集,随意选择3个点,然后删除同一直线,同一斜线的就好了

    直线上的比较简单,只用想斜线的就好了

    枚举矩形,矩形的长宽分别为i,j,那么这个矩形的对角线就会经过gcd(i,j)-1个点

    然后这个图里面可以放(n-i+1)*(m-j+1)个矩形

    然后搞一搞就好了

    代码:

    //qscqesze
    #include <cstdio>
    #include <cmath>
    #include <cstring>
    #include <ctime>
    #include <iostream>
    #include <algorithm>
    #include <set>
    #include <bitset>
    #include <vector>
    #include <sstream>
    #include <queue>
    #include <typeinfo>
    #include <fstream>
    #include <map>
    #include <stack>
    typedef long long ll;
    using namespace std;
    //freopen("D.in","r",stdin);
    //freopen("D.out","w",stdout);
    #define sspeed ios_base::sync_with_stdio(0);cin.tie(0)
    #define maxn 510000
    #define mod 10007
    #define eps 1e-9
    int Num;
    //const int inf=0x7fffffff;   //§ß§é§à§é¨f§³
    const int inf=0x3f3f3f3f;
    inline ll read()
    {
        ll x=0,f=1;char ch=getchar();
        while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
        while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
        return x*f;
    }
    //**************************************************************************************
    
    ll gcd(ll a,ll b)
    {
        return b==0?a:gcd(b,a%b);
    }
    int main()
    {
        ll n=read(),m=read();
        n++,m++;
        ll ans = (n*m)*(n*m-1)*(n*m-2LL)/6LL;
        ans -= n*(m*(m-1LL)*(m-2LL)/6LL);
        ans -= m*(n*(n-1LL)*(n-2LL)/6LL);
        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=m;j++)
            {
                ll tmp = gcd(i,j)+1LL;
                if(tmp>2LL)
                    ans-=(tmp-2LL)*2LL*(n-i*1LL)*(m-j*1LL);
            }
        }
        printf("%lld
    ",ans);
    }
  • 相关阅读:
    springboot2.X动态修改log4j2日志级别
    iframe嵌套PMM2.0
    grafana配置告警
    prometheus+grafana配置流程
    kubernetes拉取私有镜像仓库的镜像
    Windows Server 2016离线安装.NET Framework 3.5
    Office批量授权(VL)版本和激活方法
    华为USG防火墙配置NAT映射回流解决内网通过公网映射访问内部服务器
    IRF配置
    CENTOS7安装各种桌面系统 CENTOS安装桌面图形化GUI GNOME/KDE/Cinnamon/MATE/Xfce
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4776095.html
Copyright © 2020-2023  润新知