传送门
考虑到有gcd(a,b+a)=gcd(a,b)gcd(a,b+a)=gcd(a,b)gcd(a,b+a)=gcd(a,b)
所以实际上对于所有可能的N(S)N(S)N(S)
只需要统计所有SSS满足不存在子集S′S'S′使得,S−S′和N(S)−N(S′)S-S'和N(S)-N(S')S−S′和N(S)−N(S′)非空
即对于右边每个点将左边相连的点集合相同的加在一起,所有取gcdgcdgcd即可
可以用哈希表或者mapmapmap做到O(n)O(n)O(n)或O(nlogn)O(nlogn)O(nlogn)
#include
using namespace std;
#define cs const
#define re register
#define pb push_back
#define pii pair
#define ll long long
#define fi first
#define se second
#define bg begin
cs int RLEN=1<<20|1;
inline char gc(){
static char ibuf[RLEN],*ib,*ob;
(ib==ob)&&(ob=(ib=ibuf)+fread(ibuf,1,RLEN,stdin));
return (ib==ob)?EOF:*ib++;
}
inline int read(){
char ch=gc();
int res=0;bool f=1;
while(!isdigit(ch))f^=ch=='-',ch=gc();
while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
return f?res:-res;
}
inline ll readll(){
char ch=gc();
ll res=0;bool f=1;
while(!isdigit(ch))f^=ch=='-',ch=gc();
while(isdigit(ch))res=(res+(res<<2)<<1)+(ch^48),ch=gc();
return f?res:-res;
}
inline int readstring(char *s){
int top=0;char ch=gc();
while(isspace(ch))ch=gc();
while(!isspace(ch)&&ch!=EOF)s[++top]=ch,ch=gc();
return top;
}
templateinline void chemx(tp &a,tp b){a<b?a=b:0;}
templateinline void chemn(tp &a,tp b){a>b?a=b:0;}
cs int N=500005;
vector e[N];
map<vector,ll>vl;
int n,m;
ll val[N];
inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
int main(){
#ifdef Stargazer
freopen("lx.in","r",stdin);
#endif
int T=read();
while(T--){
n=read(),m=read();
for(int i=1;i<=n;i++)val[i]=readll();
for(int i=1;i<=m;i++){
int u=read(),v=read();
e[v].pb(u);
}
for(int i=1;i<=n;i++)sort(e[i].bg(),e[i].end());
for(int i=1;i<=n;i++)if(e[i].size())vl[e[i]]+=val[i];
ll res=0;
for(map<vector,ll>::iterator it=vl.bg();it!=vl.end();++it)
res=gcd(res,it->se);
cout<<res<<'\n';
vl.clear();
for(int i=1;i<=n;i++)e[i].clear();
}
}