洛谷 P4616 [COCI2017-2018#5] Pictionary

PS:如果读过题了可以跳过题目描述直接到题解部分
提交链接:洛谷 P4616 [COCI2017-2018#5] Pictionary

题目

题面翻译

题目描述

在宇宙一个不为人知的地方,有一个星球,上面有一个国家,只有数学家居住。
在这个国家有 n n n个数学家,有趣的是,每个数学家都住在自己的城市,且城市间无道路相连,因为他们可以在线交流。当然,城市有从 1 1 1 n n n的编号。

一位数学家决定用手机发论文,而手机将“不言而喻”自动更正成了“猜谜游戏”。
不久之后,这个国家就发现了猜谜游戏。他们想要见面一起玩,于是这个国家就开始了修路工程。
道路修建会持续 m m m天。对于第 i i i天,若 gcd ⁡ ( a , b ) = m − i + 1 \gcd(a,b)=m-i+1 gcd(a,b)=mi+1,则 a a a b b b城市间会修一条路。

由于数学家们忙于建筑工作,请你来确定一对数学家最早什么时候能凑到一起玩。

输入输出格式

输入格式

第一行有三个正整数 n , m , q n,m,q n,m,q,表示城市数量、修路持续天数、询问数量。
接下来 q q q行,每行有两个正整数 a , b a,b a,b,表示询问 a a a b b b两个城市的数学家最早什么时候能在一起玩。

输出格式

输出 q q q行,第 i i i行有一个正整数,表示第 i i i次询问的结果

说明

数据范围:
对于 40 % 40\% 40%的数据:
n ≤ 4000 , q ≤ 1 0 5 n≤4000,q≤10^5 n4000,q105
对于全部数据:
1 ≤ n , q ≤ 1 0 5 1≤n,q≤10^5 1n,q105
1 ≤ m ≤ n 1≤m≤n 1mn

样例1解释:
在第一天, ( 3 , 6 ) (3,6) (3,6)之间修了一条路,因此第二次询问输出1
在第二天, ( 2 , 4 ) , ( 2 , 6 ) , ( 2 , 8 ) , ( 4 , 6 ) , ( 6 , 8 ) (2,4),(2,6),(2,8),(4,6),(6,8) (2,4),(2,6),(2,8),(4,6),(6,8)之间都修了一条路,此时 4 4 4 8 8 8号城市连通,第三次询问输出2
在第三天,所有编号互质的城市之间都修了路, 2 2 2 5 5 5号城市在此时连通,第一次询问输出1

样例2解释:
在第二天, ( 20 , 15 ) (20,15) (20,15)之间修了一条路
第四天, ( 15 , 9 ) (15,9) (15,9)之间修了一条路
所以 20 20 20 9 9 9号城市在第四天连通,输出4

题目描述

There is a planet, in a yet undiscovered part of the universe, with a country inhabited solely by mathematicians. In this country, there are a total of ​N mathematicians, and the interesting fact is that each mathematician lives in their own city. Is it also interesting that no two cities are connected with a road, because mathematicians can communicate online or by reviewing academic papers. Naturally, the cities are labeled with numbers from 1 to ​N.

Life was perfect until one mathematician decided to write an academic paper on their smartphone. The smartphone auto-corrected the word “self-evident” to “Pictionary” and the paper was published as such. Soon after, the entire country discovered pictionary and wanted to meet up and play, so construction work on roads between cities began shortly.
.
The road construction will last a total of ​M days, according to the following schedule: on the first day, construction is done on roads between all pairs of cities that have ​M as their greatest common divisor. On the second day, construction is done on roads between all pairs of cities that have ​M-1 as their greatest common divisor, and so on until the ​ M t h M^{th} Mth day when construction is done on roads between all pairs of cities that are co-prime. More formally, on the i t h i^{th} ith day, construction is done on roads between cities ​a and ​b if ​gcd(a, b) = M − i + 1 M-i+1 Mi+1.

Since the mathematicians are busy with construction work, they’ve asked you to help them determine the minimal number of days before a given pair of mathematicians can play pictionary together.

输入格式

The first line of input contains three positive integers ​N, M and ​Q (1 ≤ ​N , ​Q ≤ 100 000, 1 ≤ ​M ≤ ​N ), the number of cities, the number of days it takes to build the roads, and the number of queries.
Each of the following ​Q lines contains two distinct positive integers ​A and ​B (1 ≤ ​A , ​B ≤ ​N ) that denote the cities of the mathematicians who want to find out the minimal number of days before they can play pictionary together.

输出格式

The i t h i^{th} ith line must contain the minimal number of days before the mathematicians from the i t h i^{th} ith query can play pictionary together.

样例 #1

样例输入 #1

8 3 3
2 5
3 6
4 8

样例输出 #1

3
1
2

样例 #2

样例输入 #2

25 6 1
20 9

样例输出 #2

4

样例 #3

样例输入 #3

9999 2222 2
1025 2405
3154 8949

样例输出 #3

1980
2160

提示

In test cases worth 40% of total points, it will hold ​N ≤ 1000, ​Q ≤ 100 000.

Clarification of the first test case:

On the first day, road (3, 6) is built. Therefore the answer to the second query is 1.

On the second day, roads (2, 4), (2, 6), (2, 8), (4, 6) and (6, 8) are built. Cities 4 and 8 are now
connected (it is possible to get from the first to the second using city 6).

On the third day, roads between relatively prime cities are built, so cities 2 and 5 are connected.

Clarification of the second test case:

On the second day, road (20, 15) is built, whereas on the fourth day, road (15, 9) is built. After the
fourth day, cities 20 and 9 are connected via city 15.

题解

天数从小到大连边,用并查集维护两城市是否已经联通,边权为连接的天数,这样的话,应该会连接形成一棵树,询问的时候,直接查询路径上的最大值的好了,可以用倍增或者树剖(我用的树剖)。

代码实现

100pts

//pictionary
#pragma GCC optimize(3)
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
int n,m,q;
int x,y;
int f[100010];
int head[100010];
int cnt;
int fa[100010];
int siz[100010];
int dep[100010];
int son[100010];
int top[100010];
int dfn[100010];
int rnk[100010];
int val[100010];
int b[400010];

struct tree{
	int v,w,nex;
}a[200010];

int find(int x){
	if(x==f[x]){
		return x;
	}
	return f[x]=find(f[x]);
}

void build(int u,int v,int w){
	a[++cnt].v=v;
	a[cnt].w=w;
	a[cnt].nex=head[u];
	head[u]=cnt;
}

void dfs1(int x){
	siz[x]=1;
	for(int i=head[x];i;i=a[i].nex){
		int v=a[i].v;
		if(v!=fa[x]){
			fa[v]=x;
			val[v]=a[i].w;
			dep[v]=dep[x]+1;
			dfs1(v);
			siz[x]+=siz[v];
			if(siz[son[x]]<siz[v]){
				son[x]=v;
			}
		}
	}
}

void dfs2(int x,int t){
	top[x]=t;
	dfn[x]=++cnt;
	rnk[cnt]=x;
	if(son[x]){
		dfs2(son[x],t);
	}
	else{
		return;
	}
	for(int i=head[x];i;i=a[i].nex){
		int v=a[i].v;
		if(v!=fa[x]&&v!=son[x]){
			dfs2(v,v);
		}
	}
}

void build1(int rt,int l,int r){
	if(l==r){
		b[rt]=val[rnk[l]];
		return;
	}
	int mid=(l+r)>>1;
	build1(rt<<1,l,mid);
	build1(rt<<1|1,mid+1,r);
	b[rt]=max(b[rt<<1],b[rt<<1|1]);
}

int query(int rt,int l,int r,int ll,int rr){
	if(l>=ll&&r<=rr){
		return b[rt];
	}
	int mid=(l+r)>>1;
	int ans=0;
	if(ll<=mid){
		ans=max(ans,query(rt<<1,l,mid,ll,rr));
	}
	if(rr>mid){
		ans=max(ans,query(rt<<1|1,mid+1,r,ll,rr));
	}
	return ans;
}

int qm(int x,int y){
	int ans=0;
	while(top[x]!=top[y]){
		if(dep[top[x]]<dep[top[y]]){
			swap(x,y);
		}
		ans=max(ans,query(1,1,n,dfn[top[x]],dfn[x]));
		x=fa[top[x]];
	}
	if(dep[x]>dep[y]){
		swap(x,y);
	}
	ans=max(ans,query(1,1,n,dfn[x]+1,dfn[y]));
	return ans;
}

int main(){
	register int i,j;
	scanf("%d%d%d",&n,&m,&q);
	for(i=1;i<=n;++i){
		f[i]=i;
	}
	for(i=m;i>=1;--i){
		for(j=2;j*i<=n;++j){
			if(find(i)!=find(i*j)){
				f[find(i)]=f[find(i*j)];
				build(i,i*j,m-i+1);
				build(i*j,i,m-i+1);
			}
		}
	}
	cnt=0;
	dfs1(1);
	dfs2(1,1);
	build1(1,1,n);
	for(i=1;i<=q;++i){
		scanf("%d%d",&x,&y);
		printf("%d\n",qm(x,y));
	}
	return 0;
}
深度学习是机器学习的一个子领域,它基于人工神经网络的研究,特别是利用多层次的神经网络来进行学习和模式识别。深度学习模型能够学习数据的高层次特征,这些特征对于图像和语音识别、自然语言处理、医学图像分析等应用至关重要。以下是深度学习的一些关键概念和组成部分: 1. **神经网络(Neural Networks)**:深度学习的基础是人工神经网络,它是由多个层组成的网络结构,包括输入层、隐藏层和输出层。每个层由多个神经元组成,神经元之间通过权重连接。 2. **前馈神经网络(Feedforward Neural Networks)**:这是最常见的神经网络类型,信息从输入层流向隐藏层,最终到达输出层。 3. **卷积神经网络(Convolutional Neural Networks, CNNs)**:这种网络特别适合处理具有网格结构的数据,如图像。它们使用卷积层来提取图像的特征。 4. **循环神经网络(Recurrent Neural Networks, RNNs)**:这种网络能够处理序列数据,如时间序列或自然语言,因为它们具有记忆功能,能够捕捉数据中的时间依赖性。 5. **长短期记忆网络(Long Short-Term Memory, LSTM)**:LSTM 是一种特殊的 RNN,它能够学习长期依赖关系,非常适合复杂的序列预测任务。 6. **生成对抗网络(Generative Adversarial Networks, GANs)**:由两个网络组成,一个生成器和一个判别器,它们相互竞争,生成器生成数据,判别器评估数据的真实性。 7. **深度学习框架**:如 TensorFlow、Keras、PyTorch 等,这些框架提供了构建、训练和部署深度学习模型的工具和库。 8. **激活函数(Activation Functions)**:如 ReLU、Sigmoid、Tanh 等,它们在神经网络中用于添加非线性,使得网络能够学习复杂的函数。 9. **损失函数(Loss Functions)**:用于评估模型的预测与真实值之间的差异,常见的损失函数包括均方误差(MSE)、交叉熵(Cross-Entropy)等。 10. **优化算法(Optimization Algorithms)**:如梯度下降(Gradient Descent)、随机梯度下降(SGD)、Adam 等,用于更新网络权重,以最小化损失函数。 11. **正则化(Regularization)**:技术如 Dropout、L1/L2 正则化等,用于防止模型过拟合。 12. **迁移学习(Transfer Learning)**:利用在一个任务上训练好的模型来提高另一个相关任务的性能。 深度学习在许多领域都取得了显著的成就,但它也面临着一些挑战,如对大量数据的依赖、模型的解释性差、计算资源消耗大等。研究人员正在不断探索新的方法来解决这些问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

月半流苏

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值