hdu 4790 ()

链接: http://acm.hdu.edu.cn/showproblem.php?pid=4790

思路: 对于(a b)(c d) 我们可以考虑(b d)-(b c-1)-(a-1 ,d)+(a-1,c-1)

那么剩下的就是一个(x y)的符合情况的对数。

cnt1=x/p;  cnt2=y/p; res1=x%p;  res2=y%p ;  

那么答案就是  cnt1*cnt2*p+ (res1+1)*cnt2+(res2+1)*cnt1+  余数部分。

余数部分我们分情况考虑。 分为4种 res1<=m&&res2<=m  res1<=m&&res>m  res>m&&res1,<=m res1>m&&res2>m

代码:

#include<bits/stdc++.h>

using namespace std;
typedef long long ll;

ll a,b,c,d,m,p;


ll get(ll A, ll B)
{
    if(A<0||B<0) return 0;
    ll cnt1=A/p;
    ll cnt2=B/p;
    ll res1=A%p;
    ll res2=B%p;
    ll ans=0;
    ans+=cnt1*cnt2*p;
    ans+=(res1+1)*cnt2;
    ans+=(res2+1)*cnt1;
    if(res1>m&&res2>m){
        ans+=(m+1);
        ll x=(m-res1+p)%p;
        if(x<=res2) ans+=(res2-x+1);
    }
    else if(res1>m&&res2<=m){
        ans+=(res2+1);
    }
    else if(res1<=m&&res2<=m){
        ll x=(m-res1+p)%p;
        if(x<=res2) ans+=(res2-x+1);
    }
    else if(res1<=m&&res2>m){
        ll x=(m-res1+p)%p;
        if(x<=res2) ans+=(m-x+1);
    }
    return ans;
}

int main()
{
    int T;
    scanf("%d",&T);
    int kk=0;
    while(T--)
    {
        scanf("%lld %lld %lld %lld %lld %lld",&a,&b,&c,&d,&p,&m);
        ll ans=0;
        ans+=get(b,d);
        ans-=get(c-1,b);
        ans-=get(a-1,d);
        ans+=get(a-1,c-1);
        //cout<<"ans "<<ans<<endl;
        ll mu=(b-a+1)*(d-c+1);
        //cout<<"ans "<<ans<<" mu "<<mu<<endl;
        ll gg=__gcd(mu,ans);
        ans/=gg;
        mu/=gg;
        printf("Case #%d: %lld/%lld\n",++kk,ans,mu);
    }
    return 0;
}

/*

4
0 7 0 6 9 4
0 7 0 3 9 4
0 4 0 6 9 4
0 4 0 3 9 4

*/

猜你喜欢

转载自blog.csdn.net/yjt9299/article/details/82459703
hdu