P - 区间与其他数互质数的个数 HDU - 4777
Rabbit Kingdom
Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3360 Accepted Submission(s): 1135
题目链接
http://acm.hdu.edu.cn/showproblem.php?pid=4777
Problem Description
long ago, there was an ancient rabbit kingdom in the forest. Every
rabbit in this kingdom was not cute but totally pugnacious, so the
kingdom was in chaos in season and out of season.
n rabbits were
numbered form 1 to n. All rabbits' weight is an integer. For some
unknown reason, two rabbits would fight each other if and only if their
weight is NOT co-prime.
Now the king had arranged the n rabbits in a
line ordered by their numbers. The king planned to send some rabbits
into prison. He wanted to know that, if he sent all rabbits between the
i-th one and the j-th one(including the i-th one and the j-th one) into
prison, how many rabbits in the prison would not fight with others.
Please note that a rabbit would not fight with himself.
Input
The first line of each test case contains two integer n, m, indicating the number of rabbits and the queries.
The following line contains n integers, and the i-th integer Wi indicates the weight of the i-th rabbit.
Then
m lines follow. Each line represents a query. It contains two integers L
and R, meaning the king wanted to ask about the situation that if he
sent all rabbits from the L-th one to the R-th one into prison.
(1 <= n, m, Wi <= 200000, 1 <= L <= R <= n)
The input ends with n = 0 and m = 0.
Output
Sample Input
Sample Output
Hint
In the second case, the answer of the 4-th query is 2, because only 1 and 5 is co-prime with other numbers in the interval [2,6] .
Source
题意
题解
代码:
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define N 200050
const int maxn=;
int n,m,P_num;
int prime[N],f[N],last[N],ans[N],c[N];
int L[N],R[N];
vector<int> V[N];
struct Query
{
int l,r,id;
bool operator <(const Query&b)const
{return r<b.r;}
}que[N];
template<typename T>void read(T&x)
{
ll k=; char c=getchar();
x=;
while(!isdigit(c)&&c!=EOF)k^=c=='-',c=getchar();
if (c==EOF)exit();
while(isdigit(c))x=x*+c-'',c=getchar();
x=k?-x:x;
}
void read_char(char &c)
{while(!isalpha(c=getchar())&&c!=EOF);}
void update(int x,int tt){while(x<=maxn){c[x]+=tt;x+=x&-x;}}
int query(int x){int ans=;while(x){ans+=c[x];x-=x&-x;}return ans;}
void init()
{
for(int i=;i<=maxn;i++)
{
if (f[i]==)
{
prime[++P_num]=i;
int k=i;
while(k+i<=maxn)f[k+i]=,k+=i;
}
}
}
void add(int i,int zs)
{
R[last[zs]]=min(R[last[zs]],i);
L[i]=max(L[i],last[zs]);
last[zs]=i;
}
void work()
{
read(n); read(m);
if (n==)exit();
for(int i=;i<=n;i++)
{
int x;
L[i]=; R[i]=n+;
read(x);
for(int j=;j<=P_num&&x>&&f[x]==;j++)
if (x%prime[j]==)
{
add(i,prime[j]);
while(x%prime[j]==)x/=prime[j];
}
if (x>)add(i,x);
}
for(int i=;i<=n;i++) V[R[i]].push_back(i);
for(int i=;i<=m;i++)
{
read(que[i].l); read(que[i].r);
que[i].id=i;
}
sort(que+,que+m+);
int r=;
for(int i=;i<=m;i++)
{
for(int j=r+;j<=que[i].r;j++)
{
if (L[j])update(L[j],);
for(int k=;k<V[j].size();k++)
{
if (L[V[j][k]])update(L[V[j][k]],-);
update(V[j][k],);
}
}
r=que[i].r;
ans[que[i].id]=que[i].r-que[i].l+;
ans[que[i].id]-=query(que[i].r)-query(que[i].l-);
}
for(int i=;i<=m;i++)printf("%d\n",ans[i]);
}
void clear()
{
for(int i=;i<=maxn;i++)V[i].clear();
memset(last,,sizeof(last));
memset(c,,sizeof(c));
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("aa.in","r",stdin);
#endif
init();
while()
{
clear();
work();
}
}
P - 区间与其他数互质数的个数 HDU - 4777的更多相关文章
- 区间求小于等于k的数字个数 hdu4177
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4417 题目意思给出一个序列,叫我们求一个区间里面小于等于k的数字个数. 这里面我用分块和主席树两种方法 ...
- hdu4106 区间k覆盖问题(连续m个数,最多选k个数) 最小费用最大流 建图巧妙
/** 题目:hdu4106 区间k覆盖问题(连续m个数,最多选k个数) 最小费用最大流 建图巧妙 链接:http://acm.hdu.edu.cn/showproblem.php?pid=4106 ...
- AC日记——数1的个数 openjudge 1.5 40
40:数1的个数 总时间限制: 1000ms 内存限制: 65536kB 描述 给定一个十进制正整数n,写下从1到n的所有整数,然后数一下其中出现的数字“1”的个数. 例如当n=2时,写下1,2. ...
- 数对的个数(cogs610)
Description出题是一件痛苦的事情!题目看多了也有审美疲劳,于是我舍弃了大家所熟悉的A+B Problem,改用A-B了哈哈! 好吧,题目是这样的:给出一串数以及一个数字C,要求计算出所有A- ...
- sum_series() 求一列数的指定个数的数和(5个数字的和)
#include <stdio.h> #include <stdarg.h> /*用sum_series() 求一列数的指定个数的数和(5个数字的和)*/ double sum ...
- SELECT INTO和INSERT INTO SELECT的区别 类似aaa?a=1&b=2&c=3&d=4,如何将问号以后的数据变为键值对 C# 获取一定区间的随即数 0、1两个值除随机数以外的取值方法(0、1两个值被取值的概率相等) C# MD5 加密,解密 C#中DataTable删除多条数据
SELECT INTO和INSERT INTO SELECT的区别 数据库中的数据复制备份 SELECT INTO: 形式: SELECT value1,value2,value3 INTO Ta ...
- hdu 5062 单峰数(12321)的个数
http://acm.hdu.edu.cn/showproblem.php?pid=5062 模拟筛出对称单峰数(12321)的个数,水题 #include <cstdio> #inclu ...
- POJ3180(有向图强连通分量结点数>=2的个数)
The Cow Prom Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 1451 Accepted: 922 Descr ...
- cogs 610. 数对的个数
610. 数对的个数 ★★ 输入文件:dec.in 输出文件:dec.out 简单对比时间限制:1 s 内存限制:128 MB Description出题是一件痛苦的事情!题目看多了也 ...
随机推荐
- Java-Runoob:Java 循环结构
ylbtech-Java-Runoob:Java 循环结构 - for, while 及 do...while 1.返回顶部 1. Java 循环结构 - for, while 及 do...whil ...
- zabbix 在linux上安装以及一些配置
本文章将演示zabbix 3.2版本的安装,供有需要的伙伴们参考: 网络也有很多关于zabbix的安装文档,甚至每一步的配置都有详细的截图,我这里就不演示截图了,多配置几次自然就熟练了.多折腾. 楼主 ...
- 机器视觉学习笔记(5)——基于OpenCV的单目摄像机标定
本文CameraCalibrator类源代码来自于OpenCV2 计算机视觉编程手册(Robert Laganiere 著 张静 译) 强烈建议阅读机器视觉学习笔记(4)--单目摄像机标定参数说明之后 ...
- cocoa 线程操作
在Cocoa 中创建线程使用NSThread类的detachNewThreadSelector: toTarget:withObject:方法 NSPort *port1 = [NSPort port ...
- Three.js黑暗中的萤火虫
效果图 demo import './index.css'; // stats var stats; (function(){ stats = new Stats(); document.body.a ...
- 「小程序JAVA实战」 小程序wxss样式文件的使用(七)
转自:https://idig8.com/2018/08/09/xiaochengxu-chuji-07/ 细说下微信小程序的wxss样式文件.源码:https://github.com/liming ...
- mac配置git mergetool为p4merge(2013笔记整理)
既有环境: 1)下载安装p4merge 2)安装git 下面是配置p4merge为git mergetool的步骤 1. /usr/local/bin下创建extMerge文件: $ cat > ...
- PHP 使用memcached简单示例分享
1.添加扩展包 代码如下: php_memcache.dll 2.在PHP.INI添加 代码如下: extension=php_memcache.dll 3.程序 代码如下: < ?php / ...
- rdlc设置指定列隐藏
此用户帐户对提案名称列不可见
- js,javascript生成 UUID的四种方法
全局唯一标识符(GUID,Globally Unique Identifier)也称作 UUID(Universally Unique IDentifier) . GUID是一种由算法生成的二进制长度 ...