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

    2301 [HAOI2011]Problem b

    Time Limit: 50 Sec Memory Limit: 256 MB

    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


    就是顺手写了一道**题。。。。 滚去开车去了~
    ```c++

    include<bits/stdc++.h>

    using namespace std;
    const int maxn = 5e4 + 5;
    int prime[maxn], mu[maxn];
    bool not_prime[maxn];
    int a, b, c, d, k, tot;

    inline void prepare()
    {
    mu[1] = 1;
    for(int i = 2; i < maxn; ++i){
    if(!not_prime[i]){
    prime[++tot] = i; mu[i] = -1;
    }
    for(int j = 1; prime[j] * i < maxn; ++j){
    not_prime[prime[j] * i] = true;
    if(i % prime[j] == 0){
    mu[i * prime[j]] = 0;
    break;
    }
    mu[i * prime[j]] = -mu[i];
    }
    }
    for(int i = 1; i < maxn; ++i) mu[i] += mu[i - 1];
    }

    inline int workk(int n, int m)
    {
    if(n > m) swap(n, m);
    int next, ret = 0;
    for(int d = k; d <= n; d = next + k){
    next = min(n / (n / d), m / (m / d));
    next = (next / k) * k;
    ret += (mu[next / k] - mu[d / k - 1]) * ((n / d) * (m / d));
    }
    return ret;
    }

    int main()
    {
    prepare();
    int T; scanf("%d", &T);
    while(T--){
    scanf("%d%d%d%d%d", &a, &b, &c, &d, &k);
    printf("%d ", workk(b, d) - workk(b, c - 1) - workk(a - 1, d) + workk(a - 1, c - 1));
    }
    return 0;
    }

    心如花木,向阳而生。
  • 相关阅读:
    Node.js v0.10.1 稳定版发布
    PHP 5.5.0 Alpha6 发布
    DataNucleus Access Platform 3.2 正式版发布
    Swipe 2.0 发布,移动端滑动 JS 库
    Kamailio 4.0 发布,开源的SIP服务器
    PeerJS 0.1.7:一个用于浏览器内P2P的WebRTC封装器
    Apache Libcloud 0.12.3 发布
    IE 10将加强对Flash的支持
    JBoss Portlet Bridge 3.2.0.Beta2 发布
    如何在遗留代码基础上开发
  • 原文地址:https://www.cnblogs.com/LLppdd/p/9157280.html
Copyright © 2011-2022 走看看