zoukankan      html  css  js  c++  java
  • BZOJ 2301: [HAOI2011]Problem b( 数论 )

    和POI某道题是一样的...  http://www.cnblogs.com/JSZX11556/p/4686674.html

    只需要二维差分一下就行了. 时间复杂度O(MAXN + N^1.5) 

    ------------------------------------------------------------------------------

    #include<cstdio>
    #include<algorithm>
    #include<cstring>
     
    using namespace std;
     
    const int maxn = 50009;
     
    bool F[maxn];
    int mu[maxn], p[maxn], pn, D;
     
    void Init() {
    memset(F, 0, sizeof F);
    pn = 0;
    for(int i = 2; i < maxn; i++) {
    if(!F[i]) {
    p[pn++] = i;
    mu[i] = -1;
    }
    for(int j = 0; j < pn && i * p[j] < maxn; j++) {
    F[i * p[j]] = true;
    if(i % p[j] == 0) {
    mu[i * p[j]] = 0;
    break;
    } else
    mu[i * p[j]] = -mu[i];
    }
    }
    mu[0] = 0;
    mu[1] = 1;
    for(int i = 1; i < maxn; i++)
    mu[i] += mu[i - 1];
    }
     
    int f(int x, int y) {
    if((y /= D) > (x /= D)) swap(x, y);
    int ret = 0;
    for(int L = 1, R; L <= y; L = R + 1) {
    R = min(x / (x / L), y / (y / L));
    ret += (mu[R] - mu[L - 1]) * (x / L) * (y / L);
    }
    return ret;
    }
     
    void Work() {
    int T;
    scanf("%d", &T);
    while(T--) {
    int a, b, c, d;
    scanf("%d%d%d%d%d", &a, &b, &c, &d, &D);
    printf("%d ", f(b, d) + f(a - 1, c - 1) - f(a - 1, d) - f(b, c - 1));
    }
    }
     
    int main() {
    Init();
    Work();
    return 0;
    }

    ------------------------------------------------------------------------------

    2301: [HAOI2011]Problem b

    Time Limit: 50 Sec  Memory Limit: 256 MB
    Submit: 2475  Solved: 1054
    [Submit][Status][Discuss]

    Description

    对于给出的n个询问,每次求有多少个数对(x,y),满足axbcyd,且gcd(x,y) = kgcd(x,y)函数为xy的最大公约数。



    Input

    第一行一个整数n,接下来n行每行五个整数,分别表示abcdk

     

    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

    Source

  • 相关阅读:
    POJ 1466 Girls and Boys 黑白染色 + 二分匹配 (最大独立集) 好题
    POJ2226 Muddy Fields 二分匹配 最小顶点覆盖 好题
    POJ 2195 Going Home 最小费用流 裸题
    POJ 3368 Frequent values RMQ 训练指南 好题
    POJ 3187 杨辉三角+枚举排列 好题
    POJ 2393 贪心 简单题
    系统监控
    系统的初始化和服务
    vi与vim
    正文处理命令及tar命令
  • 原文地址:https://www.cnblogs.com/JSZX11556/p/5109779.html
Copyright © 2011-2022 走看看