hdu6229 Wandering Robots

链接

点击跳转

题解

举几个例子,列一个方程组

然后解一解,就发现答案的分母等于所有点的出度之和

分子等于所有“右下角”的点的出度之和

代码

#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 1000010
#define maxe 1000010
#define cl(x) memset(x,0,sizeof(x))
#define rep(i,a,b) for(i=a;i<=b;i++)
#define drep(i,a,b) for(i=a;i>=b;i--)
#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 N;
set<pll> s;
bool check(ll x, ll y)
{
    return 0<=x and x<N and 0<=y and y<N and s.find(pll(x,y))==s.end();
}
int main()
{
    ll T=read(), i, d, kase;
    rep(kase,1,T)
    {
        N=read();
        ll K=read(), tot=5*N*N-4*N, cnt=5*N*(N+1)/2-2*N-2;
        s.clear();
        vector<pll> v;
        while(K--)
        {
            ll x=read(), y=read();
            v.emb(pll(x,y));
        }
        ll dx[]{0,0,1,-1}, dy[]{1,-1,0,0};
        for(auto pr:v)
        {
            ll x=pr.fi, y=pr.se;
            s.em(pr);
            rep(d,0,3)
            {
                ll tx = x+dx[d], ty = y+dy[d];
                if(check(tx,ty))
                {
                    tot--;
                    if(tx+ty>=N-1)cnt--;
                }
            }
            ll t=5; t -= (x==0 or x==N-1) + (y==0 or y==N-1);
            rep(d,0,3)
            {
                ll tx=x+dx[d], ty = y+dy[d];
                if(s.find(pll(tx,ty))!=s.end())
                {
                    t--;
                }
            }
            tot -= t;
            if(x+y>=N-1)cnt-=t;
        }
        ll g = __gcd(tot,cnt);
        printf("Case #%lld: %lld/%lld\n",kase,cnt/g,tot/g);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/FSAHFGSADHSAKNDAS/article/details/106844289