zoukankan      html  css  js  c++  java
  • [POI 2007]ZAP-Queries

    Description

    Byteasar the Cryptographer works on breaking the code of BSA (Byteotian Security Agency). He has alreadyfound out that whilst deciphering a message he will have to answer multiple queries of the form"for givenintegers $a$, $b$ and $d$, find the number of integer pairs $(x,y)$ satisfying the following conditions:

    $1le xle a$,$1le yle b$,$gcd(x,y)=d$, where $gcd(x,y)$ is the greatest common divisor of $x$ and $y$".

    Byteasar would like to automate his work, so he has asked for your help.

    TaskWrite a programme which:

    reads from the standard input a list of queries, which the Byteasar has to give answer to, calculates answers to the queries, writes the outcome to the standard output.

    FGD正在破解一段密码,他需要回答很多类似的问题:对于给定的整数a,b和d,有多少正整数对x,y,满足x<=a,y<=b,并且gcd(x,y)=d。作为FGD的同学,FGD希望得到你的帮助。

    Input

    The first line of the standard input contains one integer $n$ ($1le nle 50 000$),denoting the number of queries.

    The following $n$ lines contain three integers each: $a$, $b$ and $d$($1le dle a,ble 50 000$), separated by single spaces.

    Each triplet denotes a single query.

    Output

    Your programme should write $n$ lines to the standard output. The $i$'th line should contain a single integer: theanswer to the $i$'th query from the standard input.

    Sample Input

    2
    4 5 2
    6 4 3

    Sample Output

    3
    2

    题解

    双倍经验。去掉容斥就好了,分析见超链接。

     1 //It is made by Awson on 2018.1.21
     2 #include <set>
     3 #include <map>
     4 #include <cmath>
     5 #include <ctime>
     6 #include <queue>
     7 #include <stack>
     8 #include <cstdio>
     9 #include <string>
    10 #include <vector>
    11 #include <cstdlib>
    12 #include <cstring>
    13 #include <iostream>
    14 #include <algorithm>
    15 #define LL long long
    16 #define Abs(a) ((a) < 0 ? (-(a)) : (a))
    17 #define Max(a, b) ((a) > (b) ? (a) : (b))
    18 #define Min(a, b) ((a) < (b) ? (a) : (b))
    19 #define Swap(a, b) ((a) ^= (b), (b) ^= (a), (a) ^= (b))
    20 #define writeln(x) (write(x), putchar('
    '))
    21 #define lowbit(x) ((x)&(-(x)))
    22 using namespace std;
    23 const int N = 50000;
    24 void read(int &x) {
    25     char ch; bool flag = 0;
    26     for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || 1); ch = getchar());
    27     for (x = 0; isdigit(ch); x = (x<<1)+(x<<3)+ch-48, ch = getchar());
    28     x *= 1-2*flag;
    29 }
    30 void write(LL x) {
    31     if (x > 9) write(x/10);
    32     putchar(x%10+48);
    33 }
    34 
    35 int a, b, k, mu[N+5];
    36 
    37 void get_mu() {
    38     int isprime[N+5], prime[N+5], tot = 0;
    39     memset(isprime, 1, sizeof(isprime)); isprime[1] = 0, mu[1] = 1;
    40     for (int i = 2; i <= N; i++) {
    41     if (isprime[i]) mu[i] = -1, prime[++tot] = i;
    42     for (int j = 1; j <= tot && i*prime[j] <= N; j++) {
    43         isprime[i*prime[j]] = 0;
    44         if (i%prime[j]) mu[i*prime[j]] = -mu[i];
    45         else {mu[i*prime[j]] = 0; break; }
    46     }
    47     mu[i] += mu[i-1];
    48     }
    49 }
    50 LL cal(int a, int b) {
    51     if (a > b) Swap(a, b); LL ans = 0;
    52     for (int i = 1, last; i <= a; i = last+1) {
    53     last = Min(a/(a/i), b/(b/i));
    54     ans += (LL)(mu[last]-mu[i-1])*(a/i)*(b/i);
    55     }
    56     return ans;
    57 }
    58 void work() {
    59     read(a), read(b), read(k); writeln(cal(a/k, b/k));
    60 }
    61 int main() {
    62     int t; read(t); get_mu();
    63     while (t--) work();
    64     return 0;
    65 }
  • 相关阅读:
    flink 读取kafka 数据,partition分配
    Flink 报错 "Could not find a suitable table factory for 'org.apache.flink.table.factories.StreamTableSourceFactory' in the classpath"
    flume接收http请求,并将数据写到kafka
    【翻译】Flume 1.8.0 User Guide(用户指南) Processors
    【翻译】Flume 1.8.0 User Guide(用户指南) Channel
    【翻译】Flume 1.8.0 User Guide(用户指南) Sink
    【翻译】Flume 1.8.0 User Guide(用户指南) source
    【翻译】Flume 1.8.0 User Guide(用户指南)
    Apache Flink 简单安装
    Java之使用IDE
  • 原文地址:https://www.cnblogs.com/NaVi-Awson/p/8325830.html
Copyright © 2011-2022 走看看