• Codeforece 990G. GCD Counting(点分治+暴力)


    Codeforece 990G. GCD Counting(点分治+暴力)

    题目链接:G. GCD Counting
    题意:

    给定一个 n n n个节点的带权树,求所有点对 ( x , y ) (x,y) (x,y)间简单路径的 g c d gcd gcd值,输出每种 g c d gcd gcd的数量

    题解:

    考虑点分治,主体部分套板子就行, s o l v e solve solve函数部分我们暴力,因为点分治 s o l v e solve solve的本质是考虑经过该点路径的贡献,显然经过该点的简单路径的 g c d gcd gcd一定为该点权值的因子,因为权值 a ≦ 2 e 5 a\leqq 2e5 a2e5,所以在实际查找时经过该点形成的 g c d gcd gcd的类型是很少的,我们可以直接用 m a p map map储存下暴力求解,对应每个节点我们先记录根节点的权值到 m p mp mp中,每次搜索子树,将形成的 g c d gcd gcd放入到另一个 s m p smp smp中,这样每搜完一个子树后暴力枚举组合情况即可,最后记得每次将当前子树形成的 g c d gcd gcd加入 m p mp mp,这样就保证了形成的路径的端点是在不同子树之间。
    具体细节参考代码和注释(这题卡常www,忘初始化S=n被卡了好久才发现)

    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    	#ifndef local
    	#define endl '\n'
    #endif */
    #define mkp make_pair
    using namespace std;
    using std::bitset;
    typedef long long ll;
    typedef long double ld;
    typedef unsigned long long ull;
    const int inf=0x3f3f3f3f;
    const ll MAXN=2e6+10;
    const ll INF=1e18;
    const ll N=2e5+100;
    const ll mod=1e9+7;
    const ll hash_p1=1610612741;
    const ll hash_p2=805306457;
    const ll hash_p3=402653189;
    //-----------------------------------------------------------------------------------------------------------------*/
    // ll head[MAXN],net[MAXN],to[MAXN],edge[MAXN]/*流量*/,cost[MAXN]//费用;
    /* 
    void add(ll u,ll v,ll w,ll s){
    	to[++cnt]=v;net[cnt]=head[u];edge[cnt]=w;cost[cnt]=s;head[u]=cnt;
    	to[++cnt]=u;net[cnt]=head[v];edge[cnt]=0;cost[cnt]=-s;head[v]=cnt;
    }
    struct elemt{
    	int p,v;
    };
    -----------------------------------
    求[1,MAXN]组合式和逆元 
    ll mi(ll a,ll b){
    	ll res=1;
    	while(b){
    		if(b%2){
    			res=res*a%mod;
    		}	
    		a=a*a%mod;
    		b/=2;
    	}
    	return res;
    }
    ll fac[MAXN+10],inv[MAXN+10];
    void init(){
    	fac[0]=1;inv[0]=1;
    	for(ll i=1;i<=MAXN;i++){
    		fac[i]=(fac[i-1]*i)%mod;
    		inv[i]=mi(fac[i],mod-2);
    	}
    }
    ll C(int m,int n){//组合式C(m,n); 
    	if(!n){
    		return 1;
    	}
    	if(mmp;
    struct comp{
    	public:
    		bool operator()(elemt v1,elemt v2){
    			return v1.v --小顶堆  less --大顶堆  
    priority_queue,comp>q;
    
    	set::iterator it=st.begin();
    */
    //push_back()  等于push_back(),但效率更高,传输pair时push_back(i,j)==push_back({i,j}) 
    // vector>edge; 二维虚拟储存坐标 
    //-----------------------------------------------------------------------------------------------------------------*/
    //mt19937 rnd(time(0));//高质量随机化函数,直接调用rnd()即可
    //mapmp[N]; 
    //map::iterator it;//迭代器
    //push_back() 
    /*
    int dx[4]={0,0,1,-1};
    int dy[4]={1,-1,0,0};
    */
    struct elemt{//结构体存边
    	int v,w;
    };
    vector<elemt>e[N];//存边
    int sz[N];//点i子树大小
    int root;//当前重心
    int MX;//当前重心对应最大子树大小
    int S;//当前树大小
    int mxson[N];//当前节点最大子树大小
    bool vis[N];//当前点是否分治过
    int n,k;
    inline void read(int &hy) //快读
    {
        hy=0;
        char cc=getchar();
        while(cc<'0'||cc>'9')
        cc=getchar();
        while(cc>='0'&&cc<='9')
        {
            hy=(hy<<3)+(hy<<1)+cc-'0';
            cc=getchar();
        }
    }
    inline void add(int u,int v,int w){//建边(双向)
    	elemt tmp;
    	tmp.v=v;
    	tmp.w=w;
    	e[u].push_back(tmp);
    	tmp.v=u;
    	e[v].push_back(tmp);
    }
    inline void get_root(int x,int pre){//获取当前子树重心(存到root中)
    	sz[x]=1;mxson[x]=0;
    	for(int i=0;i<e[x].size();i++){
    		elemt tmp=e[x][i];
    		if(vis[tmp.v]||tmp.v==pre){
    			continue;
    		}
    		get_root(tmp.v,x);
    		sz[x]+=sz[tmp.v];
    		mxson[x]=max(mxson[x],sz[tmp.v]);
    	}
    	mxson[x]=max(mxson[x],S-sz[x]);
    	if(mxson[x]<MX){
    		root=x;
    		MX=mxson[x];
    	}
    }
    //-----------------------------根据不同题目确定
    int a[N];
    ll ans[N];//储存答案
    int gcd(int a,int b){
    	return a%b==0?b:gcd(b,a%b);
    }
    map<int,int>mp,smp;//分别储存当前已经有的gcd组合和子树新产生的集合对应数量
    map<int,int>::iterator itx,ity;//迭代器
    inline void get_dis(int x,int pre,int gd){//更新当前子树中能产生的gcd值
    	smp[gd]++;
    	for(int i=0;i<e[x].size();i++){
    		int tmp=e[x][i].v;
    		if(vis[tmp]||tmp==pre){
    			continue;
    		}
    		get_dis(tmp,x,gcd(a[tmp],gd));
    	}
    }
    inline void solve(int x){
    	mp.clear();
    	mp[a[x]]++;
    	ans[a[x]]++;//pair(x,x)的情况
    	for(int i=0;i<e[x].size();i++){
    		int tmp=e[x][i].v;
    		if(vis[tmp]){
    			continue;
    		}
    		smp.clear();
    		get_dis(tmp,x,gcd(a[x],a[tmp]));
    		//因为经过x的所有路径的gcd一定为x的因子,所以类型会非常少,直接暴力枚举
    		for(itx=mp.begin();itx!=mp.end();itx++){//暴力枚举所有组合情况
    			for(ity=smp.begin();ity!=smp.end();ity++){
    				int gds=gcd((*itx).first,(*ity).first);
    				ans[gds]+=(ll)(*itx).second*(*ity).second;
    			}
    		}
    		for(ity=smp.begin();ity!=smp.end();ity++){//把当前子树的情况更新带总的情况上去
    			mp[(*ity).first]+=(*ity).second;
    		}
    	}
    }
    //--------------------------------
    inline void Divide(int x){//点分治
    	vis[x]=1;//标记当前点
    	//--------------------------
    	//这里的solve是点分治精髓,看具体题目确定
    	solve(x);
    	//------------------------
    	for(int i=0;i<e[x].size();i++){
    		elemt tmp=e[x][i];
    		if(vis[tmp.v]){
    			continue;
    		}
    		S=sz[tmp.v];root=0;MX=inf;//寻找子树重心作为新分治点
    		get_root(tmp.v,0);
    		Divide(root);
    	}
    }
    int main(){
    /*cout<
    /*cout<
    	//ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);//同步流
    		int n;
    		cin>>n;
    		for(int i=1;i<=n;i++){
    			read(a[i]);
    		}
    		for(int i=1;i<n;i++){
    			int u,v,w=0;
    			read(u);
    			read(v);
    			add(u,v,w);
    		}
    		S=n;//q
    		root=0;MX=inf;
    		get_root(1,0);//获取整棵树的重心作为初始点
    		Divide(root);
    		for(int i=1;i<=200000;i++){//输出答案
    			if(ans[i]){
    				cout<<i<<" "<<ans[i]<<endl;
    			}
    		}
    	return 0;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
    • 232
    • 233
    • 234
    • 235
    • 236
    • 237
    • 238
    • 239
    • 240
  • 相关阅读:
    OCR - 关于OCR技术体系的发展的了解
    Python面试题:如何在 Python 中解析 XML 文件?
    Terraform基础入门 (Infrastructure as Code)
    jQuery的选择器与自带函数详解
    信管知识梳理(三)软件工程相关知识
    跑一跑NeuralAnnot
    docker 更换Docker Root Dir
    本周内容整理
    OpenCV 概念、整体架构、各模块主要功能
    达梦SQL优化:如何定位慢的SQL
  • 原文地址:https://blog.csdn.net/m0_56280274/article/details/126445521