[HDU - 5116 ] Everlasting L (计数DP,容斥)

[HDU - 5116 ] Everlasting L (计数DP,容斥)

题目链接:HDU - 5116

题目链接:

题意:

给定一个大小为\(\mathit n\)的点集\(\mathit S\),现在让求出有多少个集合对\((A,B)\),满足:

img

一个集合被称为Good,当且仅当满足:

点集\(\mathit P\) 中存在一个点\((x,y)\),满足:

P = {(x, y), (x + 1, y), . . . , (x + a, y), (x, y + 1), . . . , (x, y + b)}(a, b ≥ 1)

并且 gcd(a, b) = 1.

思路:

考虑容斥:

设Good集合个数=a。

设有交集Good集合对数=b。

则答案\(ans=a^2-b\)

问题转化为求\(a,b\)

先利用DP求出数组:

\(f[i][j]\)代表\(\mathit i\)\([1,j]\)中多少个数互质。

\(g[i][j]\)代表\([1,i]\)\([1,j]\)中多少对数互质。

\(right[i][j]\)代表集合中点\((i,j)\)右边有多少个连续的点。

\(up[i][j]\)代表集合中点\((i,j)\)上边有多少个连续的点。

那么我们对每一个点集中纯在的点\((i,j)\),枚举\(k\in[1,up[i][j]]\),该点对\(\mathit a\)的贡献即为\(\sum f[k][right[i][j]]\)

利用同样的方式可以获得出dp数组\(cnt[i][j]\) 代表 点\((i,j)\)在多少个Good集合的上升部分。

数组\(num[i][j]\) 代表 点\((i,j)\)在多少个Good集合的右拓部分。

那么每一个点\((i,j)\)\(\mathit b\)的贡献即为\(2\times num[i][j]*cnt[i][j]-g[up[i][j][right[i][j]]]^2\)

代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <bits/stdc++.h>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define chu(x)  if(DEBUG_Switch) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) { if (a == 0ll) {return 0ll;} a %= MOD; ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
ll poww(ll a, ll b) { if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a ;} a = a * a ; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
inline long long readll() {long long tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') { fh = -1; } c = getchar();} while (c >= '0' && c <= '9') { tmp = tmp * 10 + c - 48, c = getchar(); } return tmp * fh;}
inline int readint() {int tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') { fh = -1; } c = getchar();} while (c >= '0' && c <= '9') { tmp = tmp * 10 + c - 48, c = getchar(); } return tmp * fh;}
void pvarr_int(int *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%d%c", arr[i], i == n ? '\n' : ' ');}}
void pvarr_LL(ll *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%lld%c", arr[i], i == n ? '\n' : ' ');}}
const int maxn = 1000010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
#define DEBUG_Switch 0
int n;
ll ans;
ll vis[205][205];
ll _right[205][205];
ll up[205][205];
ll num[205][205];
ll cnt[205][205];
int len = 200;
ll f[205][205];
ll g[205][205];
int main()
{
#if DEBUG_Switch
    freopen("C:\\code\\input.txt", "r", stdin);
#endif
    //freopen("C:\\code\\output.txt","w",stdout);
    int t;
    t = readint();
    repd(i, 1, len) {
        repd(j, 1, len) {
            f[i][j] = f[i][j - 1] + (gcd(i, j) == 1);
            g[i][j] = g[i - 1][j] + f[i][j];
        }
    }
    for (int icase = 1; icase <= t; ++icase) {
        MS0(vis);
        MS0(_right);
        MS0(up);
        MS0(cnt);
        MS0(num);
        n = readint();
        repd(i, 1, n) {
            int x = readint(), y = readint();
            vis[x][y] = 1;
        }
        for (int i = len; i >= 1; --i) {
            for (int j = len; j >= 1; --j) {
                if (vis[i + 1][j] > 0) {
                    _right[i][j] = _right[i + 1][j] + 1;
                }
                if (vis[i][j + 1] > 0) {
                    up[i][j] = up[i][j + 1] + 1;
                }
            }
        }
        ll a = 0ll;
        repd(i, 1, len) {
            repd(j, 1, len) {
                if (vis[i][j] == 0) {
                    continue;
                }
                ll temp = 0ll;
                for (int k = up[i][j]; k >= 0; --k) {
                    temp += f[k][_right[i][j]];
                    cnt[i][j + k] += temp;
                }
                a += temp;
            }
        }
        repd(i, 1, len) {
            repd(j, 1, len) {
                if (vis[i][j] == 0) {
                    continue;
                }
                ll temp = 0ll;
                for (int k = _right[i][j]; k >= 0; --k) {
                    temp += f[k][up[i][j]];
                    num[i + k][j] += temp;
                }
            }
        }
        ll b = 0ll;
        repd(i, 1, len) {
            repd(j, 1, len) {
                if (vis[i][j] > 0) {
                    b += num[i][j] * cnt[i][j] * 2;
                    b -= g[up[i][j]][_right[i][j]] * g[up[i][j]][_right[i][j]];
                }
            }
        }
        ll ans = a * a - b;
        printf("Case #%d: %lld\n", icase, ans );
    }
    return 0;
}


posted @ 2020-10-23 15:03  茄子Min  阅读(108)  评论(0编辑  收藏  举报