zoukankan      html  css  js  c++  java
  • BZOJ 2301 [HAOI2011]Problem b

    2301: [HAOI2011]Problem b

    Description

    对于给出的n个询问,每次求有多少个数对(x,y),满足a≤x≤b,c≤y≤d,且gcd(x,y) = k,gcd(x,y)函数为x和y的最大公约数。

    Input

    第一行一个整数n,接下来n行每行五个整数,分别表示a、b、c、d、k

    Output

    共n行,每行一个整数表示满足要求的数对(x,y)的个数

    Sample Input

    2

    2 5 1 5 1

    1 5 1 5 2

    Sample Output

    14

    3

    HINT

    100%的数据满足:1≤n≤50000,1≤a≤b≤50000,1≤c≤d≤50000,1≤k≤50000


      类似BZOJ 1101 [POI2007]Zap,唯一区别是区间的加加减减。

     1 /**************************************************************
     2     Problem: 2301
     3     User: Doggu
     4     Language: C++
     5     Result: Accepted
     6     Time:10212 ms
     7     Memory:1700 kb
     8 ****************************************************************/
     9  
    10 #include <cstdio>
    11 #include <algorithm>
    12 const int N = 100100;
    13 int mu[N], prime[N], ptot;
    14 bool vis[N];
    15 void EULER(int n) {
    16     mu[1]=1;
    17     for( int i = 2; i <= n; i++ ) {
    18         if(!vis[i]) prime[++ptot]=i, mu[i]=-1;
    19         for( int j = 1; j <= ptot; j++ ) {
    20             if((long long)i*prime[j]>n) break;
    21             vis[i*prime[j]]=1;
    22             mu[i*prime[j]]=mu[i]*(-1);
    23             if(i%prime[j]==0) {
    24                 mu[i*prime[j]]=0;
    25                 break;
    26             }
    27         }
    28         mu[i]+=mu[i-1];
    29     }
    30 }
    31 int cal(int n,int m) {
    32     if(n>m) std::swap(n,m);
    33     int ans=0;
    34     for( int a = 1, ed; a <= n; a=ed+1 ) {
    35         ed=std::min(n/(n/a),m/(m/a));
    36         ans+=(long long)(mu[ed]-mu[a-1])*(n/a)*(m/a);
    37     }
    38     return ans;
    39 }
    40 int main() {
    41     EULER(50000);
    42     int T, a, b, c, d, k;
    43     scanf("%d",&T);
    44     while(T--) {
    45         scanf("%d%d%d%d%d",&a,&b,&c,&d,&k);
    46         a--;c--;
    47         a/=k;b/=k;c/=k;d/=k;
    48         printf("%d
    ",cal(b,d)+cal(a,c)-cal(a,d)-cal(b,c));
    49     }
    50     return 0;
    51 }
    52 
  • 相关阅读:
    (水题)洛谷
    (水题)洛谷
    洛谷
    (水题)洛谷
    POJ
    poj 3061(二分 or 尺取法)
    poj 2456(二分)
    poj 1064(二分答案)
    POJ 2559(单调栈)
    STL
  • 原文地址:https://www.cnblogs.com/Doggu/p/bzoj2301.html
Copyright © 2011-2022 走看看