zoukankan      html  css  js  c++  java
  • hdu 4790 Just Random (2013成都J题) 数学思路题 容斥

    题意:在[a,b]  [c,d] 之间,和模p等于m的对数

    详见代码

     1 #include <stdio.h>
     2 #include <algorithm>
     3 #include <string.h>
     4 #include<cmath>
     5 #define LL long long
     6 using namespace std;
     7 int T;
     8 LL a,b,c,d,p,m;
     9 
    10 LL gcd(LL a, LL b) {
    11     return b ? gcd(b, a % b) : a;
    12 }
    13 
    14 LL fun(LL x,LL y) {//表示0到x区间,0到y区间的组合对数
    15     LL ret;
    16     LL ra,rb;
    17     ra=x%p,rb=y%p;
    18     ret=(x/p)*(y/p)*p;
    19     ret+=(ra+1)*(y/p)+(rb+1)*(x/p);
    20     if(ra>m) {
    21         ret+=min(m+1,rb+1);
    22         LL tmp=(m+p-ra)%p;
    23         if(tmp<=rb) {
    24             ret+=rb-tmp+1;
    25         }
    26     } else {
    27         LL tmp=(m+p-ra)%p;
    28         if(tmp<=rb) {
    29             ret+=min(m-tmp+1,rb-tmp+1);
    30         }
    31     }
    32     return ret;
    33 }
    34 int main() {
    35     scanf("%d",&T);
    36     int cas=0;
    37     while(T--) {
    38         cas++;
    39         scanf("%I64d%I64d%I64d%I64d%I64d%I64d",&a,&b,&c,&d,&p,&m);
    40         LL ans=fun(b,d)-fun(a-1,d)-fun(b,c-1)+fun(a-1,c-1);//容斥原理求和
    41         LL tot=(b-a+1)*(d-c+1);
    42         if(ans<0)
    43             ans=0;
    44 //        printf("ans:%I64d tot:%I64d
    ",ans,tot);
    45         LL l=gcd(ans,tot);
    46         ans/=l,tot/=l;
    47         printf("Case #%d: %I64d/%I64d
    ", cas, ans, tot);
    48     }
    49     return 0;
    50 }
    View Code
  • 相关阅读:
    小知识!
    命令级的python静态资源服务。
    自定义滚动条样式-transition无效
    css:a:visited限制
    react16 渲染流程
    virtual-dom
    用video标签流式加载
    golang 代码笔记
    position:fixed not work?
    go/node/python 多进程与多核cpu
  • 原文地址:https://www.cnblogs.com/ITUPC/p/5298901.html
Copyright © 2011-2022 走看看