原文链接https://www.cnblogs.com/zhouzhendong/p/CF1016G.html

题目传送门 - CF1016G

题意

  给定 $n,x,y$ ,以及一个含有 $n$ 个元素的数组 $a$ 。

  我们称一个数对 $(i,j)$ 是合法的,当且仅当存在一个 $v$ ,使得 $\gcd(a_i,v)=x$ 且 ${\rm lcm} (a_j,v)=y$ 。

  请你统计有多少合法的 $(i,j)$ 数对。

  $n\leq 2\times 10^5,1\leq x,y,a_i\leq 10^{18}$

题解

  先用 Pollard_Rho 将 $y$ 分解一下。由于最小的 $16$ 个不同的质数的乘积大于 $10^{18}$ ,所以, $y$ 最多只有 $15$ 个不同的质因子。

  然后,我们罗列以下简单性质:

  性质1 : $x|y$ 。 证明: 因为 $\gcd(a_i,v)=x$ ,所以 $x|v$ ; 因为 ${\rm lcm}(a_j,v)=y$ ,所以 $v|y$ 。因为 $x|v,v|y$ ,所以 $x|y$ 。

  性质2 : $x|a_i,\ \ \ \ a_j|y$ 。

  性质3 : 令集合 $P$ 为素数集。定义一个数 $\alpha$ 分解质因数后,得到的结果中,含有质因子 $p(p\in P)$ 的个数为 $c_{a_i,p}$ 。则因为 $x|y$ ,所以 $\forall p\in P , c_{x,p}\leq c_{y,p}$ 。

  性质4 : 数对 $(i,j)$ 是合法的,对应的数是 $v$ 。那么 $\forall p\in P, c_{x,p}\leq \min(c_{a_i,p},c_{v,p}),\max(c_{a_j,p},c_{v,p})\leq c_{y,p}$ 。

  于是,我们就可以得到快速判定两个 $(i,j)$ 是否合法了。

  性质5 : $X=\{a_i\} (x|a_i),Y=\{a_i\} (a_i|y)$ ,如果一个数对 $(i,j)$ 合法,则根据性质2和性质4,必然有: $a_i\in X,a_j\in Y;\ \ \forall p\in P, c_{a_i,p}=c_{x,p} 或 c_{a_j,p}=c_{y,p}$ 。

  由于一开始说到的, $y$ 最多只有 $15$ 个不同的质因子,所以对于 $p\in P$ 且 $c_{y,p}=0$ ,由于 $a_j|y$ ,所以 $c_{a_j,p}=c_{y,p}=0$ ,所以这个不需要考虑。

  考虑枚举 $a_i\in X$ ,对于一个数 $i$ ,对应的合法的 $j$ 有多少?我们考虑写出合法的 $a_j$ 的范围。

  对于每一个 $p\in P$ ,那么我们需要询问的是一下两种情况下满足条件的数的个数:如果 $c_{a_i,p}=c_{x,p}$ , $c_{a_j,p}=c_{y,p}\ {\rm OR}\ c_{a_j,p}<c_{y,p}$ ;如果 $c_{a_i,p}>c_{x,p}$ ,那么 $c_{a_j,p}=c_{y,p}$ 。

  由于我们最多只需要涉及 $15$ 个质因子,我们可以把集合 $y$ 中的数的信息压缩一下,得到一个二进制表示。令 $y$ 的第 $i$ 个质数为 $p_i$ ,则对于一个数 $a_j$ ,如果 $c_{a_j,p_i}=c_{y,p_i}$ 那么对应二进制位为 $1$ ,否则为 $0$ 。于是,每一次询问就是要求一下对应的数的个数。不难发现,我们只需要统计一下每一个二进制表示对应的在集合 $y$ 中有多少数字,然后做一个 and 卷积的FWT,就可以得到需要询问的东西了。

  令 $d=15$ ,所以时间复杂度为 $O ((n+2^d)\log d)$ 。

代码

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef long double LD;
namespace Pollard_Rho{
int prime[9]={2,3,5,7,11,13,17,19,23};
ULL RR;
int Pcnt;
LL p[70];
vector <LL> res;
LL R(LL mod){
return (RR+=4179340454199820289LL)%mod;
}
LL Mul(LL x,LL y,LL mod){
LL d=(LL)floor((LD)x*y/mod+0.5);
LL res=x*y-d*mod;
if (res<0)
res+=mod;
return res;
}
LL Pow(LL x,LL y,LL mod){
LL ans=1%mod;
for (;y;y>>=1,x=Mul(x,x,mod))
if (y&1)
ans=Mul(ans,x,mod);
return ans;
}
bool Miller_Rabin(LL n){
if (n<=1)
return 0;
for (int i=0;i<9;i++)
if (n==prime[i])
return 1;
LL d=n-1;
int tmp=0;
while (!(d&1))
d>>=1,tmp++;
for (int i=0;i<9;i++){
LL x=Pow(prime[i],d,n),p=x;
for (int j=1;j<=tmp;j++){
x=Mul(x,x,n);
if (x==1&&p!=1&&p!=n-1)
return 0;
p=x;
}
if (x!=1)
return 0;
}
return 1;
}
LL f(LL x,LL c,LL mod){
return (Mul(x,x,mod)+c)%mod;
}
LL gcd(LL x,LL y){
return y?gcd(y,x%y):x;
}
LL Get_Factor(LL c,LL n){
LL x=R(n),y=f(x,c,n),p=n;
while (x!=y&&(p==n||p==1)){
p=gcd(n,max(x-y,y-x));
x=f(x,c,n);
y=f(f(y,c,n),c,n);
}
return p;
}
void Pollard_Rho(LL n){
if (n<=1)
return;
if (Miller_Rabin(n)){
res.push_back(n);
return;
}
while (1){
LL v=Get_Factor(R(n-1)+1,n);
if (v!=n&&v!=1){
Pollard_Rho(v);
Pollard_Rho(n/v);
return;
}
}
}
void work(LL n){
res.clear();
Pollard_Rho(n);
}
}
LL read(){
LL x=0,f=1;
char ch=getchar();
while (!isdigit(ch)&&ch!='-')
ch=getchar();
if (ch=='-')
f=-1,ch=getchar();
while (isdigit(ch))
x=(x<<1)+(x<<3)+ch-48,ch=getchar();
return x;
}
const int N=200005;
int n;
LL x,y,a[N];
LL v[16],vc=0,xc[16],yc[16],c[16];
vector <LL> Mul_x;
LL A[1<<15];
void Get_V(){
memset(xc,0,sizeof xc);
memset(yc,0,sizeof yc);
Pollard_Rho :: work(y);
vector <LL> xx=Pollard_Rho :: res;
sort(xx.begin(),xx.end());
v[vc=1]=xx[0],yc[vc]=1;
for (int i=1;i<xx.size();i++)
if (xx[i]==xx[i-1])
yc[vc]++;
else
v[++vc]=xx[i],yc[vc]=1;
LL xv=x;
for (int i=1;i<=vc;i++)
while (xv%v[i]==0)
xv/=v[i],xc[i]++;
}
void FWT(LL a[],int n,int flag){
for (int d=1;d<n;d<<=1)
for (int i=0;i<n;i+=(d<<1))
for (int j=0;j<d;j++)
a[i+j]+=a[i+j+d]*flag;
}
int main(){
n=read(),x=read(),y=read();
for (int i=1;i<=n;i++)
a[i]=read();
if (y%x){
putchar('0');
return 0;
}
if (y==1){
int ans=0;
for (int i=1;i<=n;i++)
if (a[i]==1)
ans++;
printf("%I64d",1LL*ans*ans);
return 0;
}
Get_V();
Mul_x.clear();
memset(A,0,sizeof A);
for (int i=1;i<=n;i++){
if (a[i]%x==0)
Mul_x.push_back(a[i]);
if (y%a[i]==0){
memset(c,0,sizeof c);
LL yv=a[i];
for (int i=1;i<=vc;i++)
while (yv%v[i]==0)
yv/=v[i],c[i]++;
int s=0;
for (int i=1;i<=vc;i++)
if (c[i]==yc[i])
s|=1<<(i-1);
A[s]++;
}
}
FWT(A,1<<vc,1);
LL ans=0;
for (int i=0;i<Mul_x.size();i++){
LL now=Mul_x[i];
memset(c,0,sizeof c);
LL yv=now;
for (int i=1;i<=vc;i++)
while (yv%v[i]==0)
yv/=v[i],c[i]++;
int s=(1<<vc)-1;
for (int i=1;i<=vc;i++)
if (c[i]==xc[i]||xc[i]==yc[i])
s^=1<<(i-1);
ans+=A[s];
}
printf("%I64d",ans);
return 0;
}

  

Codeforces 1016G Appropriate Team 数论 FWT的更多相关文章

  1. [CodeForces - 1225C]p-binary 【数论】【二进制】

    [CodeForces - 1225C]p-binary [数论][二进制] 标签: 题解 codeforces题解 数论 题目描述 Time limit 2000 ms Memory limit 5 ...

  2. Codeforces 1045B Space Isaac - 数论 - Hash

    题目传送门 传送门I 传送门II 传送门III 题目大意 给定将$\left \{ 0, 1, \dots, m - 1\right \}$分成了不相交的两个非空集合$A$和$B$,给定$A$,问存在 ...

  3. CodeForces - 615D Multipliers(数论)

    http://codeforces.com/problemset/problem/615/D 题意 给出m个质因子,组成一个数n.问n的约数的乘积是多少,输出mod 1e+7的结果. 分析 从输入我们 ...

  4. Codeforces Round #425 (Div. 2) Problem C Strange Radiation (Codeforces 832C) - 二分答案 - 数论

    n people are standing on a coordinate axis in points with positive integer coordinates strictly less ...

  5. Codeforces 932 E. Team Work(组合数学)

    http://codeforces.com/contest/932/problem/E 题意:   可以看做 有n种小球,每种小球有无限个,先从中选出x种,再在这x种小球中任选k个小球的方案数 选出的 ...

  6. Codeforces 932.E Team Work

    E. Team Work time limit per test 2 seconds memory limit per test 256 megabytes input standard input ...

  7. Codeforces 757 F Team Rocket Rises Again

    Discription It's the turn of the year, so Bash wants to send presents to his friends. There are n ci ...

  8. codeforces 615 D. Multipliers (数论 + 小费马定理 + 素数)

    题目链接: codeforces 615 D. Multipliers 题目描述: 给出n个素数,这n个素数的乘积等于s,问p的所有因子相乘等于多少? 解题思路: 需要求出每一个素数的贡献值,设定在这 ...

  9. codeforces 108D Basketball Team(简单组合)

    D. Basketball Team time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

随机推荐

  1. Python2018-字符串中字符个数统计

    1 编写程序,完成以下要求: 统计字符串中,各个字符的个数 比如:"hello world" 字符串统计的结果为: h:1 e:1 l:3 o:2 d:1 r:1 w:1 prin ...

  2. [C]字符串行为

    未事先分配长度的字符串变量声明,占用内存空间是这个字符串长度 + 1,1用于保存\0结束标识 #include <stdio.h> #include <stdlib.h> #i ...

  3. windows 2012执行powershell脚本报错

    使用powershell运行脚本报错:进行数字签名.无法在当前系统上运行该脚本.有关运行脚本和设置执行策略的详细信息 修复方法:powershell "Set-ExecutionPolicy ...

  4. Confluence 6 手动安装语言包和找到更多语言包

    手动安装语言包 希望以手动的方式按照语言包,你需要按照下面描述的方式上传语言包.一旦你安装成功后,语言包插件将会默认启用. 插件通常以 JAR 或者 OBR(OSGi Bundle Repositor ...

  5. android组件之TabHost

    一 介绍 1.1 TAB的容器.这个对象包含两个子元素: TabWidget:管理标签(tabs),用户点击来选择一个特定的标签,是它告诉TabHost去切换界面的 FrameLayout:对象显示该 ...

  6. mysql之视图,触发器,事务等。。。

    一.视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,可以将该结果集当做表来使用. 使用视图我们可以把查询过程中的 ...

  7. Python字符串编码转换

    使用encode()方法编码 str.encode([encoding="utf-8"][,errors="strict"]) str:表示需要转换的字符串 e ...

  8. PHP编译安装时常见错误解决办法

    转载自:http://www.bkjia.com/PHPjc/1008013.html This article is post on https://coderwall.com/p/ggmpfa c ...

  9. The.Glory.of.Innovation 创新之路3放飞好奇

    教育最重要的就是 问题不在于教他各种学问,而在于培养他爱好学问的兴趣,而且在这种兴趣充分增长起来的时候,教他以研究学问的方法. ————  卢梭     如何辨识不同的观点, 老师考查的重点不在于学生 ...

  10. 激活函数的比较,sigmoid,tanh,relu

    1. 什么是激活函数 如下图,在神经元中,输入inputs通过加权.求和后,还被作用了一个函数.这个函数就是激活函数Activation Function 2. 为什么要用激活函数 如果不用激活函数, ...