CCPC2018-湖南全國邀請賽 K 2018


K.2018

題目描述

Given a, b, c, d , find out the number of pairs of integers ( x, y ) where a ≤ x ≤ b, c ≤ y ≤ d and x · y is a multiple of 2018.

輸入

The input consists of several test cases and is terminated by end-of-file.
Each test case contains four integers a, b, c, d 

輸出

For each test case, print an integer which denotes the result.
• 1 ≤ a ≤ b ≤ 10 9 , 1 ≤ c ≤ d ≤ 10 9
• The number of tests cases does not exceed 10 4 .

樣例輸入

1 2 1 2018
1 2018 1 2018
1 1000000000 1 1000000000

樣例輸出

3
6051
1485883320325200
 
題意:給定區間[a,b]、[c,d],問有多少對有序數組(x,y)(x∈[a,b],y∈[c,d])使得x*y是2018的倍數
思路:2018=2*1009(分解質因數),則對x分類討論:1)僅為2的倍數;2)僅為1009的倍數;3)即為2又為1009的倍數;4)既不為2又不為1009的倍數
等價於如下分類討論:
1.若x是偶數:1)若x是1009的倍數,則y可為[c,d]中任意數; 2)若x不是1009的倍數,則y必定為[c,d]中1009的倍數
2.若x是奇數:1)若x是1009的倍數,則y必定為[c,d]中2的倍數; 2)若x不是1009的倍數,則y必定為[c,d]中2018的倍數

#include <iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
#define ll long long
using namespace std;
int main()
{
    ll a,b,c,d;
    while(cin>>a>>b>>c>>d)
    {
        ll s1=b-a+1;
        ll s2=d-c+1;
        ll s1_o=b/2-(a-1)/2;
        ll s1_1009=b/1009-(a-1)/1009;

        /*int x=b/2018-(a-1)/2018;
        ans+=x*s2;

        x=s1_o-x;
        ans+=x*(d/1009-(c-1)/1009);

        x=s1_1009-(b/2018-(a-1)/2018);
        ans+=x*(d/2-(c-1)/2);

        x=s1-s1_o-x;
        ans+=x*(d/2018-(c-1)/2018);*/
        ll x1,x2,x3,x4;

        x1=(b/2018-(a-1)/2018)*s2;
        x2=(s1_o-(b/2018-(a-1)/2018))*(d/1009-(c-1)/1009);
        x3=(s1_1009-(b/2018-(a-1)/2018))*(d/2-(c-1)/2);
        x4=((s1-s1_o)-(s1_1009-(b/2018-(a-1)/2018)))*(d/2018-(c-1)/2018);
        printf("%lld\n",x1+x2+x3+x4);
    }
    return 0;
}

 




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM