zoukankan      html  css  js  c++  java
  • CF1323E Instant Noodles

    题目

    分析

    首先如果我们对于左部每个点的贡献进行考虑,其实是不太好做的,所以这样的题目一般可以直接按照每一个贡献点的贡献次数来思考。

    然后我们发现其实很多个数的和的 \(\gcd\) 与其本身也有关系,也就是一定可以从小的关系推出大的关系。

    于是可以考虑两个不同的右端点,其拥有左端点集合的关系来判断这一对点对于答案的贡献。

    设第一个点其左侧点集为 \(A\) ,右侧点集为 \(B\) :(默认 \(|A|\le |B|\)

    \(1.\) \(A=B\) ,那么两者对于答案的贡献一定时时刻刻都是一个整体,贡献一定是 \(w_1+w_2\)

    \(2.\) \(A\subseteq B\) ,那么两者答案是 \(w_1+w_2\)\(w_2\) ,那么因为 \(\gcd(a,b)=\gcd(a+b,b)\) 得到其贡献等价于 \(w_1,w_2\)

    \(3.\) \(A\bigcap B \not = \emptyset\)\(A\bigcap B\not =A\),那么其贡献是 \(w_1,w_2,w_1+w_2\) ,同上,等价于 \(w_1,w_2\)

    \(4.\) \(A\bigcap B = \emptyset\) ,那么贡献是 \(w_1,w_2\)

    多元的情况可以由二元推广。

    于是我们发现除了第一个,其他的情况都是一样的贡献,那么我们只需要按照集合的相等分一下组就知道哪些集合相等了,使用哈希即可。

    代码

    #include<bits/stdc++.h>
    using namespace std;
    //#ifdef ONLINE_JUDGE
    //	#define getchar() (p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++)
    //	char buf[1<<21],*p1=buf,*p2=buf;
    //#endif
    template<typename T>
    inline void read(T &x){
    	x=0;bool f=false;char ch=getchar();
    	while(!isdigit(ch)){f|=ch=='-';ch=getchar();}
    	while(isdigit(ch)){x=x*10+(ch^48);ch=getchar();}
    	x=f?-x:x;
    	return ;
    }
    template<typename T>
    inline void write(T x){
    	if(x<0) x=-x,putchar('-');
    	if(x>9) write(x/10);
    	putchar(x%10^48);
    	return ;
    }
    #define ll long long
    #define ull unsigned long long
    #define ld long double
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define pc putchar
    #define PII pair<ll,ll>
    #define rep(i,x,y) for(register int i=(x);i<=(y);i++)
    #define dep(i,y,x) for(register int i=(y);i>=(x);i--)
    #define repg(i,x) for(int i=head[x];i;i=nex[i])
    #define filp(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout)
    #define infilp(s) freopen(s".in","r",stdin)
    #define outfilp(s) freopen(s".out","w",stdout)
    const int MOD=1e9+7;
    inline int inc(int x,int y){x+=y;return x>=MOD?x-MOD:x;}
    inline int dec(int x,int y){x-=y;return x<0?x+MOD:x;}
    inline void incc(int &x,int y){x+=y;if(x>=MOD) x-=MOD;}
    inline void decc(int &x,int y){x-=y;if(x<0) x+=MOD;}
    inline void chkmin(int &x,int y){if(y<x) x=y;}
    inline void chkmax(int &x,int y){if(y>x) x=y;}
    const int N=5e5+5,M=2e5+5,INF=1e9+7,P=19260817,Q=1e9+7;
    int n,m;
    ll c[N],sum[N],Gcd,has1[N],has2[N]; 
    ll gcd(ll x,ll y){return !y?x:gcd(y,x%y);}
    map<pair<ll,ll>,ll>Map;
    vector<int>vec[N];
    signed main(){
    //	double ST=clock();
    	// ios::sync_with_stdio(false);
    //#ifndef ONLINE_JUDGE
    //	filp("my");
    //#endif
    	int t;
    	read(t);
    	while(t--){
    		read(n),read(m); 
    		rep(i,1,n) read(c[i]),sum[i]=0,has1[i]=has2[i]=0,vec[i].clear();
    		Map.clear();Gcd=0;
    		rep(i,1,m){
    			int u,v;
    			read(u),read(v);
    			vec[v].pb(u);
    		}
    		rep(i,1,n){
    		    sort(vec[i].begin(),vec[i].end());
    		    ll has1=0,has2=0,tmp=0;
    		    for(int j:vec[i]){
    		    	tmp=1;
    		        has1=(has1*1331+1ll*j*j*131)%P;
    		        has2=(has2*131+1ll*j*j*j)%Q;
    		    }
    		    if(tmp) Map[mp(has1,has2)]+=c[i];
    		}
    		for(auto p:Map){
    			if(p.se==0) continue;
    			if(!Gcd) Gcd=p.se;
    			else Gcd=gcd(Gcd,p.se);
    		} 
    		write(Gcd);pc('\n');
    	}
    //	cerr<<"\nTime:"<<(clock()-ST)/CLOCKS_PER_SEC<<"s\n";
    	return 0;
    }
    /*
    
    */
    
  • 相关阅读:
    【转】Pandas学习笔记(七)plot画图
    【转】Pandas学习笔记(六)合并 merge
    【转】Pandas学习笔记(五)合并 concat
    【转】Pandas学习笔记(四)处理丢失值
    【转】Pandas学习笔记(三)修改&添加值
    【转】Pandas学习笔记(二)选择数据
    17秋 SDN课程 第二次上机作业
    17秋 SDN课程 第三次上机作业
    17秋 SDN课程 第一次上机作业
    Ubuntu 14.04 安装sublime
  • 原文地址:https://www.cnblogs.com/Akmaey/p/15624554.html
Copyright © 2011-2022 走看看