码迷,mamicode.com
首页 > 其他好文 > 详细

[POI 2007]ZAP-Queries

时间:2018-01-22 00:02:18      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:max   algo   fine   tar   common   char   mat   .com   html   

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:

$1\le x\le a$,$1\le y\le 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$ ($1\le n\le 50\ 000$),denoting the number of queries.

The following $n$ lines contain three integers each: $a$, $b$ and $d$($1\le d\le a,b\le 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(‘\n‘))
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 }

 

[POI 2007]ZAP-Queries

标签:max   algo   fine   tar   common   char   mat   .com   html   

原文地址:https://www.cnblogs.com/NaVi-Awson/p/8325830.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!