hdu 4790 Just Random(2013成都)

参考:

http://blog.csdn.net/ok_again/article/details/16812897

http://blog.csdn.net/yrleep/article/details/16808827

http://www.cnblogs.com/kuangbin/p/3429062.html

(a, b)与 (c, d)的数对个数 转化成 get(b, d) - get(a - 1, d) - get(b, c - 1) + get(a - 1, c - 1)

get(a, b)表示区间(0,a)与(0,b)的数对个数

类似的一道题目2013南京邀请赛HDU 4588 Count The Carries

也是将区间转化为(0,a)形式的区间


hdu 4790 Just Random(2013成都)代码:

//#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <ctime>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <string>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;

#define FF(i, a, b) for(int i = (a); i < (b); ++i)
#define FE(i, a, b) for(int i = (a); i <= (b); ++i)
#define FD(i, b, a) for(int i = (b); i >= (a); --i)
#define REP(i, N) for(int i = 0; i < (N); ++i)
#define CLR(a, v) memset(a, v, sizeof(a))
#define PB push_back
#define MP make_pair

typedef long long LL;
const int INF = 0x3f3f3f3f;
const int MAXN = 100010;

int p, m;

LL get(int a, int b)
{
    if (a < 0 || b < 0) return 0;
    ++a; ++b;
    int an = a / p, bn = b / p;
    int am = a % p, bm = b % p;
    LL ret = 1LL * an * bn * p + am * bn + bm * an;

    if (am && bm)
    {
        --am; --bm;
        if (am < bm) swap(am, bm);
        int l = (m - bm + p) % p, r = m;

        if (r > am) ret += max(0, bm  + 1 - (r - am));
        else if (r >= bm) ret += bm + 1;
        else ret += r + 1 + max(0, am - l + 1);
    }
    return ret;
}

int main ()
{
    int a, b, c, d;
    int nc = 1;
    int T;
    scanf("%d", &T);
    while (T--)
    {
        scanf("%d%d%d%d", &a, &b, &c, &d);
        scanf("%d%d", &p, &m);
        LL up = get(b, d) - get(b, c - 1) - get(a - 1, d) + get(a - 1, c - 1);
        LL down = 1LL * (b - a + 1) * (d - c + 1);
        LL mid = __gcd(up, down);
        printf("Case #%d: ", nc++);
        printf("%I64d/%I64d\n", up / mid, down / mid);
    }
    return 0;
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值