hdu-4790

来源:互联网 发布:淘宝上的斗神摇杆 编辑:程序博客网 时间:2024/04/29 12:18

题解参见:http://www.cnblogs.com/kuangbin/p/3429062.html

WA了n次

要注意的地方:

1)如果不好判断哪里会发生整数溢出的话,最好全部开long long

2)如果用题解中的方法,边界计算要小心,最好多出几组特例

#include <iostream>#include <cstdio>#include <cstdlib>#include <cstring>#include <vector>#include <queue>#include <stack>#include <algorithm>using namespace std;#define F(i, n) for (int i=0;i<(n);++i)#define LBIT(x) x&-xint T;long long a, b, c, d, p, m;long long gcd(long long a, long long b) {    return b == 0 ? a : gcd(b, a % b);}template<class T>void myswap(T * a, T * b) {    T tmp = *a;    *a = *b;    *b = tmp;}int main(){    freopen("input.in", "r", stdin);    //freopen("output.out", "w", stdout);    int T;    cin >> T;    F(cases,T) {        cin >> a >> b >> c >> d >> p >> m;        if (d - c + 1 < b - a + 1) {            myswap<long long>(&a, &c);            myswap<long long>(&b, &d);        }        long long pos1 = a + c;        long long pos2 = b + c - 1;        long long pos3 = b + c;        long long pos4 = a + d + 1;        long long pos5 = b + d;        long long sum1 = 0;        long long sum2 = 0;        long long sum3 = 0;            int _m = pos1 % p;            if (_m < m)                pos1 += m - _m;            else if (_m > m)                pos1 += p + m - _m;            if (pos1 < b + c) {                long long n = (pos2 - pos1) / p + 1;                long long a0 = pos1 - (a + c) + 1;                sum1 = a0 * n + (n-1) * n / 2 * p;            } else                sum1 = 0;            _m = pos3 % p;            if (_m < m)                pos3 += m - _m;            else if (_m > m)                pos3 += p + m - _m;            if (pos3 <= a + d) {                sum2 = (long long)( (a + d - pos3) / p + 1 ) * (b - a + 1);            } else                sum2 = 0;            _m = pos4 % p;            if (_m < m)                pos4 += m - _m;            else if (_m > m)                pos4 += p + m - _m;            if (pos4 <= pos5) {                long long n = (pos5 - pos4) / p + 1;                long long a0 = b + d -pos4 + 1;                sum3 = a0 * n + (n-1) * n / 2 * (-p);            } else                sum3 = 0;            long long sum = sum1 + sum2 + sum3;            long long tot = (long long)(b - a + 1) * (d - c + 1);            if (sum > 1) {                long long g = gcd(sum, tot);                sum /= g;                tot /= g;            } else if (sum == 0) {                sum = 0;                tot = 1;            }            cout << "Case #" << cases + 1 << ": " << sum << '/' << tot << endl;            //cout << sum * 1.0 / tot << endl;    }    return 0;}


0 0
原创粉丝点击