[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:
$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
题解
双倍经验。去掉容斥就好了,分析见超链接。
//It is made by Awson on 2018.1.21
#include <set>
#include <map>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <cstdio>
#include <string>
#include <vector>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define LL long long
#define Abs(a) ((a) < 0 ? (-(a)) : (a))
#define Max(a, b) ((a) > (b) ? (a) : (b))
#define Min(a, b) ((a) < (b) ? (a) : (b))
#define Swap(a, b) ((a) ^= (b), (b) ^= (a), (a) ^= (b))
#define writeln(x) (write(x), putchar('\n'))
#define lowbit(x) ((x)&(-(x)))
using namespace std;
const int N = ;
void read(int &x) {
char ch; bool flag = ;
for (ch = getchar(); !isdigit(ch) && ((flag |= (ch == '-')) || ); ch = getchar());
for (x = ; isdigit(ch); x = (x<<)+(x<<)+ch-, ch = getchar());
x *= -*flag;
}
void write(LL x) {
if (x > ) write(x/);
putchar(x%+);
} int a, b, k, mu[N+]; void get_mu() {
int isprime[N+], prime[N+], tot = ;
memset(isprime, , sizeof(isprime)); isprime[] = , mu[] = ;
for (int i = ; i <= N; i++) {
if (isprime[i]) mu[i] = -, prime[++tot] = i;
for (int j = ; j <= tot && i*prime[j] <= N; j++) {
isprime[i*prime[j]] = ;
if (i%prime[j]) mu[i*prime[j]] = -mu[i];
else {mu[i*prime[j]] = ; break; }
}
mu[i] += mu[i-];
}
}
LL cal(int a, int b) {
if (a > b) Swap(a, b); LL ans = ;
for (int i = , last; i <= a; i = last+) {
last = Min(a/(a/i), b/(b/i));
ans += (LL)(mu[last]-mu[i-])*(a/i)*(b/i);
}
return ans;
}
void work() {
read(a), read(b), read(k); writeln(cal(a/k, b/k));
}
int main() {
int t; read(t); get_mu();
while (t--) work();
return ;
}
[POI 2007]ZAP-Queries的更多相关文章
- [POI 2007] Zap
		[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1101 [算法] 首先 , 问题可以转化为求GCD(x,y) = 1,x <= ... 
- BZOJ 1101 Luogu P3455 POI 2007 Zap (莫比乌斯反演+数论分块)
		手动博客搬家: 本文发表于20171216 13:34:20, 原地址https://blog.csdn.net/suncongbo/article/details/78819470 URL: (Lu ... 
- 解题:POI 2007 Tourist Attractions
		题面 事实上这份代码在洛谷过不去,因为好像要用到一些压缩空间的技巧,我并不想(hui)写(捂脸) 先预处理$1$到$k+1$这些点之间相互的最短路和它们到终点的最短路,并记录下每个点能够转移到时的状态 ... 
- 解题:POI 2007 Driving Exam
		题面 有点意思的题 从一个位置$i$出发可以到达每一个位置即是从$1,n$出发可以到达$i$.然后有了一个做法:把图上下反转后建反图,这样就可以求从一个点$i$到达左右两侧的花费$dp[i][0/1] ... 
- 解题:POI 2007 Weights
		题面 这是个$O(nlog^2$ $n)$的解法,因为蒟蒻博主没有看懂$O(nlog$ $n)$的更优秀的解法 显然从小到大装砝码是最优的方法,又显然从大到小装容器不会使得答案变劣,还显然砝码数具有单 ... 
- [POI 2007] 办公楼
		[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1098 [算法] 显然 , 答案为补图的连通分量个数 用链表优化BFS , 时间复杂度 ... 
- [POI 2007] 堆积木
		[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1109 [算法] DP [代码] #include<bits/stdc++.h& ... 
- 【POI 2007】 山峰和山谷
		[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1102 [算法] 广度优先搜索 [代码] #include<bits/stdc+ ... 
- [POI 2007] 旅游景点
		[题目链接] https://www.lydsy.com/JudgeOnline/problem.php?id=1097 [算法] 首先,用Dijkstra算法求出2-k+1到每个点的最短路 然后,我 ... 
随机推荐
- 四则运算程序(java基于控制台)
			四则运算题目生成程序(基于控制台) 一.题目描述: 1. 使用 -n 参数控制生成题目的个数,例如 Myapp.exe -n 10 -o Exercise.txt 将生成10个题目. 2. 使用 -r ... 
- 20162330 第十二周 蓝墨云班课 hash
			题目要求 利用除留余数法为下列关键字集合的存储设计hash函数,并画出分别用开放寻址法和拉链法解决冲突得到的空间存储状态(散列因子取0.75) 关键字集合:85,75,57,60,65,(你的8位学号 ... 
- 每日冲刺报告-Day4
			敏捷冲刺报告--Day4 情况简介 今天完成前端后端任务对接, GUI主体编写 任务进度 赵坤: 完成后端爬虫 李世钰: 前后端对接, GUI编写 黄亦薇:召集小组成员开会,帮助查找资料,寻找BUG ... 
- 学号:201621123032 《Java程序设计》第5周学习总结
			1:本周学习总结 1.1: 写出你认为本周学习中比较重要的知识点关键词 接口interface,comparator接口和comparable接口. 1.2:尝试使用思维导图将这些关键词组织起来. 2 ... 
- Unix下zfs文件系统重组RAID-5后可以这样恢复
			存储做的RAID-5, SCSI硬盘,操作系统是FreeBSD,文件系统是zfs.本案例共有12块硬盘,11块硬盘里有数据,1块硬盘是热备盘.其中第6块数据硬盘出现故障,重组时需要将其剔除. 物理盘: ... 
- 自己动手写CPU(基于FPGA与Verilog)
			大三上学期开展了数字系统设计的课程,下学期便要求自己写一个单周期CPU和一个多周期CPU,既然要学,就记录一下学习的过程. CPU--中央处理器,顾名思义,是计算机中最重要的一部分,功能就是周而复始地 ... 
- OpenGL中怎么把世界坐标系变成屏幕坐标系
			对这个3D坐标手动进行OpenGL的四个变换,得到的结果就是屏幕上的像素坐标.前三个变换(Model, View, Projection)都是4x4矩阵,操作对象是四维向量,所以需要把(100, 10 ... 
- js 时间戳  vue 时间戳的转换 ?
			在没有用vue做项目之前 也遇到过戳转换的问题 直接函数 调用 方法 这个也可以写成vue的 把function去掉 formatDate后面加冒号 就可以了 当然这个不是原创 但是是谁的我忘记了 ... 
- 剑指offer-第一个只出现一次的字符
			题目描述 在一个字符串(1<=字符串长度<=10000,全部由字母组成)中找到第一个只出现一次的字符,并返回它的位置 解题思路 由于char类型一共有256种可能,所以开辟一个数组ha ... 
- 使用Putty实现windows向阿里云的Linux云服务器上传文件
			1.首先获取PSCP工具 PuTTY小巧方便.但若需要向网络中的Linux系统上传文件,则可以使用PuTTY官方提供的PSCP工具来实现上传.PSCP是基于ssh协议实现. 可以点击这里下载 2.启动 ... 
