hdu 6286(数学)(容斥)

2018

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 251    Accepted Submission(s): 129


Problem Description
Given  a,b,c,d, find out the number of pairs of integers  (x,y) where  axb,cyd and  xy is a multiple of  2018.
 

Input
The input consists of several test cases and is terminated by end-of-file.

Each test case contains four integers  a,b,c,d.
 

Output
For each test case, print an integer which denotes the result.

## Constraint

1ab109,1cd109
* The number of tests cases does not exceed  104.
 

Sample Input
 
  
1 2 1 2018 1 2018 1 2018 1 1000000000 1 1000000000
 

Sample Output
 
  
3 6051 1485883320325200

思路: 2018的因子只有1 2018 2 1009

代码:

#include<stdio.h>
#include<string.h>
#include<iostream>
#include<algorithm>

using namespace std;
typedef long long ll;
ll x;
ll l1,r1,l2,r2;

ll c[2020];
ll cc[2020];

int main()
{
    while(scanf("%lld %lld %lld %lld",&l1,&r1,&l2,&r2)!=EOF)
    {
        l1--; l2--;
        for(int i=0;i<=2018;i++) c[i]=cc[i]=0;
        x=l1/2018;
        for(int i=0;i<=2017;i++){
            c[i]-=x;
        }
        x=l1%2018;
        for(int i=1;i<=x;i++) c[i]--;

        x=r1/2018;
        for(int i=0;i<=2017;i++){
            c[i]+=x;
        }
        x=r1%2018;
        for(int i=1;i<=x;i++) c[i]++;

        x=l2/2018;
        for(int i=0;i<=2017;i++){
            cc[i]-=x;
        }
        x=l2%2018;
        for(int i=1;i<=x;i++) cc[i]--;

        x=r2/2018;
        for(int i=0;i<=2017;i++){
            cc[i]+=x;
        }
        x=r2%2018;
        for(int i=1;i<=x;i++) cc[i]++;

        ll ans=0;
        ll totc=0; ll totcc=0;
        for(int i=0;i<=2017;i++) totc+=c[i];
        for(int i=0;i<=2017;i++) totcc+=cc[i];
        ans+=totc*cc[0];
        ans+=totcc*c[0];
        totc=0; totcc=0;
        for(int i=2;i<=2017;i+=2) totc+=c[i];
        for(int i=2;i<=2017;i+=2) totcc+=cc[i];
        ans+=totc*(cc[1009]);
        ans+=totcc*(c[1009]);
        ans-=c[0]*cc[0];

        printf("%lld\n",ans);

    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/yjt9299/article/details/80541678