其他分享
首页 > 其他分享> > nowcoder19934 [CQOI2014]数三角形

nowcoder19934 [CQOI2014]数三角形

作者:互联网

链接

点击跳转

题解

答案等于C(n+1)(m+1)3C_{(n+1)(m+1)}^3C(n+1)(m+1)3​减去三点共线的情况数

fi,jf_{i,j}fi,j​表示以格点(i,j)(i,j)(i,j)为右下角的点时,选三点共线的方案数

fi,jf_{i,j}fi,j​先赋值为fi1,j+fi,j1fi1,j1f_{i-1,j}+f_{i,j-1}-f_{i-1,j-1}fi−1,j​+fi,j−1​−fi−1,j−1​,这样还剩下一种没算,就是以(0,0)(0,0)(0,0)为左上角的点,以(i,j)(i,j)(i,j)为右下角的点的情况,这种情况的个数为gcd(i,j)1gcd(i,j)-1gcd(i,j)−1

综上,fi,j=fi1,j+fi,j1fi1,j1+gcd(i,j)1f_{i,j} = f_{i-1,j}+f_{i,j-1}-f_{i-1,j-1} + gcd(i,j)-1fi,j​=fi−1,j​+fi,j−1​−fi−1,j−1​+gcd(i,j)−1

根据对称性,答案等于C(n+1)(m+1)32×fij+(n+1)Cm+13+(m+1)Cn+13C_{(n+1)(m+1)}^3 - 2 \times \sum f_{ij} + (n+1)C_{m+1}^3 + (m+1)C_{n+1}^3C(n+1)(m+1)3​−2×∑fij​+(n+1)Cm+13​+(m+1)Cn+13​

代码

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define iinf 0x3f3f3f3f
#define linf (1ll<<60)
#define eps 1e-8
#define maxn 1010
#define maxe 1000010
#define cl(x) memset(x,0,sizeof(x))
#define rep(_,__) for(_=1;_<=(__);_++)
#define em(x) emplace(x)
#define emb(x) emplace_back(x)
#define emf(x) emplace_front(x)
#define fi first
#define se second
#define de(x) cerr<<#x<<" = "<<x<<endl
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
ll read(ll x=0)
{
    ll c, f(1);
    for(c=getchar();!isdigit(c);c=getchar())if(c=='-')f=-f;
    for(;isdigit(c);c=getchar())x=x*10+c-0x30;
    return f*x;
}
ll calc(ll n){return n*(n-1)*(n-2)/6;}
ll f[maxn][maxn];
int main()
{
    ll i, j, n, m, ans;
    n=read(), m=read();
    ans = calc((n+1)*(m+1));
    for(i=0;i<=n;i++)for(j=0;j<=m;j++)
    {
        if(i)f[i][j] += f[i-1][j];
        if(j)f[i][j] += f[i][j-1];
        if(i and j)f[i][j] -= f[i-1][j-1];
        f[i][j] += max(0ll,__gcd(i,j)-1);
        ans -= 2*f[i][j];
    }
    ans += (n+1)*calc(m+1) + (m+1)*calc(n+1);
    printf("%lld",ans);
    return 0;
}
*ACoder* 发布了862 篇原创文章 · 获赞 72 · 访问量 19万+ 他的留言板 关注

标签:gcd,ll,nowcoder19934,fi,3C,CQOI2014,三角形,include,getchar
来源: https://blog.csdn.net/FSAHFGSADHSAKNDAS/article/details/104086263