HAOI2011 problem b
2024-09-23 08:10:45
2301: [HAOI2011]Problem b
Time Limit: 50 Sec Memory Limit: 256 MB
Submit: 1047 Solved: 434
[Submit][Status]
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
题解:
其实就是容斥原理了
代码:
uses math;
const maxn=;
var i,n,a,b,c,d,w,tot:longint;
ans:int64;
sum,mu,p:array[..maxn] of int64;
procedure get;
var i,j,k:longint;
check:array[..maxn] of boolean;
begin
fillchar(check,sizeof(check),false);
tot:=;mu[]:=;
for i:= to maxn do
begin
if not(check[i]) then begin inc(tot);p[tot]:=i;mu[i]:=-;end;
for j:= to tot do
begin
k:=i*p[j];
if k>maxn then break;
check[k]:=true;
if i mod p[j]<> then mu[k]:=-mu[i]
else begin mu[k]:=;break;end;
end;
end;
for i:= to maxn do sum[i]:=sum[i-]+mu[i];
end;
function f(x,y:longint):int64;
var i,last,t:longint;
begin
x:=x div w;y:=y div w;
f:=;
if x>y then begin t:=x;x:=y;y:=t;end;
i:=;
while i<=x do
begin
last:=min(x div (x div i),y div (y div i));
inc(f,(sum[last]-sum[i-])*(x div i)*(y div i));
i:=last+;
end;
exit(f);
end;
procedure main;
begin
get;
readln(n);
for i:= to n do
begin
readln(a,b,c,d,w);
ans:=;
inc(ans,f(b,d));
dec(ans,f(a-,d));
dec(ans,f(b,c-));
inc(ans,f(a-,c-));
writeln(ans);
end;
end;
begin
main;
end.
不知道为什么上面的数组都要开int64才能过,我觉得不需要啊,他们存储的值都在50000以内啊……????
最新文章
- 【转】linux 设置用户id 设置组id
- iOS开发之邓白氏编码申请流程
- Spring IOC 源码浅析
- Java数据结构——链表-单链表
- Python2安装说明
- [JAVA][RCP]Clean project之后报错:java.lang.RuntimeException: No application id has been found.
- css Spirtes 错位问题解决
- Java 理论与实践: 处理 InterruptedException(转)
- Oracle创建表空间、用户、分配权限语句
- REST API设计指导——译自Microsoft REST API Guidelines(一)
- Javascript循环删除数组中元素的几种方法示例
- python3 在 windows 读取路径多了一个\u202a 是咋回
- Android开发——ListView使用技巧总结(一)
- android greenDao使用
- itertools库 combinations() 和 permutations() 组合 和 排列选项的方法
- phpmyadmin在nginx环境下配置错误
- 百度语音识别vs科大讯飞语音识别
- 《AngularJS即学即用》读书笔记(一)
- iOS_5_汤姆猫
- Maven新建一个Spring MVC项目
热门文章
- Google面试题
- jquery全局加载函数的几种方式;
- eclipse html插件的下载和安装
- 为什么要用Hibernate框架? 把SessionFactory,Session,Transcational封装成包含crud的工具类并且处理了事务,那不是用不着spring了?
- CPU使用率
- ExtJs 4.2.1 复选框数据项动态加载(更新一下)
- uva 11069
- 一分钟明白 VS manifest 原理
- Amzon MWS API开发之订单接口
- Android:控件GridView的使用