【Codeforces #1322 C】Instant Noodles

传送门

考虑到有 g c d ( a , b + a ) = g c d ( a , b ) gcd(a,b+a)=gcd(a,b) gcd(a,b+a)=gcd(a,b)
所以实际上对于所有可能的 N ( S ) N(S) N(S)
只需要统计所有 S S S满足不存在子集 S ′ S' S使得, S − S ′ 和 N ( S ) − N ( S ′ ) S-S'和N(S)-N(S') SSN(S)N(S)非空
即对于右边每个点将左边相连的点集合相同的加在一起,所有取 g c d gcd gcd即可
可以用哈希表或者 m a p map map做到 O ( n ) O(n) O(n) O ( n l o g n ) O(nlogn) O(nlogn)

#include<bits/stdc++.h>
using namespace std;
#define cs const
#define re register
#define pb push_back
#define pii pair<int,int>
#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;
}
template<typename tp>inline void chemx(tp &a,tp b){a<b?a=b:0;}
template<typename tp>inline void chemn(tp &a,tp b){a>b?a=b:0;}
cs int N=500005;
vector<int> e[N];
map<vector<int>,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<int>,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();
	}
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值