当前位置:网站首页>2017 USP Try-outs C. Coprimes
2017 USP Try-outs C. Coprimes
2022-07-05 05:31:00 【solemntee】
Portal :Coprimes
The question : Give you a length of n n n Array of , ask l l l To r r r Whether there are mutual prime pairs between positions ( a [ i ] , a [ j ] ) = 1 ( i ≠ j ) (a[i],a[j])=1(i\not=j) (a[i],a[j])=1(i=j)
For each location l l l, We can find the smallest r r r, bring [ l , r ] [l,r] [l,r] There are coprime pairs between .
With l l l The increase of , r r r It must increase , So we can consider taking a ruler . When the right endpoint enters and the left endpoint exits , We try to maintain the number of non coprime pairs in the interval p a i r pair pair.
about μ ( x ) ≠ 0 μ(x)≠0 μ(x)=0 Of x x x, We use it c n t [ x ] cnt[x] cnt[x] maintain , Among the factors of how many numbers are in the interval x x x, then , Did not enter a right endpoint r r r when , Then each d ∣ a [ r ] d|a[r] d∣a[r] And μ ( d ) ≠ 0 μ(d)≠0 μ(d)=0 And d ≠ 1 d≠1 d=1 Of d d d Yes p a i r pair pair The contribution of − μ ( d ) c n t [ d ] −μ(d)cnt[d] −μ(d)cnt[d], Add this contribution to p a i r pair pair In go to , Empathy , Exit one endpoint after another , Take this contribution from p a i r pair pair Subtract from .
In short , Build a bucket , c n t [ x ] cnt[x] cnt[x] To maintain x x x Number of multiples , Then in the set and a [ x ] a[x] a[x] The number of non reciprocal primes is equal to
∑ d ∣ a [ i ] , d ≠ 1 − μ ( d ) c n t [ d ] \sum_{d|a[i],d\not=1} -μ(d)cnt[d] d∣a[i],d=1∑−μ(d)cnt[d]
because 1 It cannot be used as a common factor, so it can be excluded c n t [ 1 ] cnt[1] cnt[1] It makes no sense , We skip 1, All the following numbers should be reversed
#include<bits/stdc++.h>
using namespace std;
int nxt[500005];
bool vis[10000005];
int pri[1000005];
int mo[1000005];
int tot=0;
void init(int n)
{
memset(vis,0,sizeof(vis));
mo[1]=1;
for(int i=2;i<=n;i++)
{
if(!vis[i])pri[++tot]=i,mo[i]=-1;
for(int j=1;j<=tot&&pri[j]*i<=n;j++)
{
vis[i*pri[j]]=1;
if(i%pri[j]==0)
{
mo[i*pri[j]]=0;
break;
}
else mo[i*pri[j]]=mo[i]*-1;
}
}
}
int nowans=0;
int CNT[500005];
int a[500005];
void addcnt(int id,int w)
{
for(int i=1;i*i<=a[id];i++)
{
if(a[id]%i==0)
{
if(i*i==a[id])CNT[i]+=w;
else
{
CNT[i]+=w;
CNT[a[id]/i]+=w;
}
}
}
}
void add(int id,int w)
{
for(int i=1;i*i<=a[id];i++)
{
if(a[id]%i==0)
{
if(i*i==a[id])
{
nowans+=mo[i]*w*CNT[i];
}
else
{
nowans+=mo[i]*w*CNT[i];
nowans+=mo[a[id]/i]*w*CNT[a[id]/i];
}
}
}
}
int main()
{
init(500000);
mo[1]=0;
int N,M;
scanf("%d%d",&N,&M);
for(int i=1;i<=N;i++)scanf("%d",&a[i]);
int R=0;
for(int l=1;l<=N;l++)
{
while(nowans<=0&&R<N)
{
add(++R,1);
addcnt(R,1);
nowans+=R-l;
}
if(nowans>0)nxt[l]=R;
else nxt[l]=-1;
nowans-=R-l;
addcnt(l,-1);
add(l,-1);
}
for(int i=1;i<=M;i++)
{
int l,r;
scanf("%d%d",&l,&r);
if(nxt[l]!=-1&&nxt[l]<=r)printf("S\n");
else printf("N\n");
}
return 0;
}
边栏推荐
- National teacher qualification examination in the first half of 2022
- lxml. etree. XMLSyntaxError: Opening and ending tag mismatch: meta line 6 and head, line 8, column 8
- Romance of programmers on Valentine's Day
- Sword finger offer 06 Print linked list from beginning to end
- [binary search] 69 Square root of X
- 常见的最优化方法
- 对象的序列化
- A problem and solution of recording QT memory leakage
- sync. Interpretation of mutex source code
- room数据库的使用
猜你喜欢
sync. Interpretation of mutex source code
Palindrome (csp-s-2021-palin) solution
[turn]: OSGi specification in simple terms
Pointnet++的改进
利用HashMap实现简单缓存
Romance of programmers on Valentine's Day
TF-A中的工具介绍
质量体系建设之路的分分合合
Sword finger offer 05 Replace spaces
[to be continued] [depth first search] 547 Number of provinces
随机推荐
Acwing 4301. Truncated sequence
Reader writer model
Yolov5 adds attention mechanism
Haut OJ 1401: praise energy
[merge array] 88 merge two ordered arrays
Warning using room database: schema export directory is not provided to the annotation processor so we cannot export
Alu logic operation unit
A preliminary study of sdei - see the essence through transactions
一个新的微型ORM开源框架
High precision subtraction
Romance of programmers on Valentine's Day
Zzulioj 1673: b: clever characters???
剑指 Offer 58 - II. 左旋转字符串
[turn]: OSGi specification in simple terms
kubeadm系列-02-kubelet的配置和启动
sync. Interpretation of mutex source code
lxml. etree. XMLSyntaxError: Opening and ending tag mismatch: meta line 6 and head, line 8, column 8
每日一题-搜索二维矩阵ps二维数组的查找
Palindrome (csp-s-2021-palin) solution
剑指 Offer 35.复杂链表的复制