hdu 4790 Just Random

               计数,直接算的话非常复杂,可以利用简单容斥,就很容易算了。对于区间(a, b),(c, d) 则结果就是,ans = gt(b, d) - gt(a - 1, d) - gt(b, c - 1) + gt(a - 1, c - 1);

gt(a, b)表示的是0-a, 和0-b两个区间中满足题意的解的对数。

#include
#include
#include
#include
#include
#include
#include
#define LL long long
#define CLR(a, b) memset(a, b, sizeof(a))
#define REP(i, n) for(int i = 0; i < n; i ++)
#define FF(i, a, b) for(int i = a; i < b; i ++)
#define FD(i, a, b) for(int i = a; i >= b; i --)
#define swp(a, b) a^=b^=a^=b;
template inline T& RD(T &x){
    char c;while(!isdigit(c=getchar()));x=c-'0';
    while(isdigit(c=getchar()))x=x*10+c-'0';
    return x;
}

using namespace std;

LL p, m;

LL gt(LL a, LL b)
{
    if(a < 0 || b < 0) return 0;
    LL ma = a % p, mb = b % p, ret;
    ret = (a / p) * (b / p) * p;
    ret += (ma + 1) * (b / p) + (mb + 1) * (a / p);
    if(ma > m)
    {
        ret += min(m + 1, mb + 1);
        LL tmp = (m + p - ma) % p;
        if(tmp <= mb) ret += mb - tmp + 1;
    }
    else
    {
        LL tmp = (m + p - ma) % p;
        if(mb >= tmp)ret += min(m - tmp + 1, mb - tmp + 1);
    }
    return ret;
}

LL gcd(LL a, LL b)
{
    return b ? gcd(b, a % b) : a;
}

int main()
{
    //freopen("input.txt", "r", stdin);
    int t, cas = 1;
    LL a, b, c, d, ans, tot;
    RD(t);
    while(t --)
    {
        RD(a), RD(b), RD(c), RD(d), RD(p), RD(m);
        tot = (b - a + 1) * (d - c + 1);
        ans = gt(b, d) - gt(a - 1, d) - gt(b, c - 1) + gt(a - 1, c - 1);
        LL g = gcd(ans, tot);
        ans /= g;tot /= g;
        printf("Case #%d: %I64d/%I64d\n", cas ++, ans, tot);
    }
}


你可能感兴趣的:(乱搞)