博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【TP SRM 703 div2 500】 GCDGraph
阅读量:4946 次
发布时间:2019-06-11

本文共 3269 字,大约阅读时间需要 10 分钟。

Problem Statement

You are given four ints: n, k, x, and y. The ints n and k describe a simple undirected graph. The graph has n nodes, numbered 1 through n. Two distinct vertices i and j are connected by an edge if and only if gcd(i, j) > k. Here, gcd(i, j) denotes the greatest common divisor of i and j. The ints x and y are the numbers of two (not necessarily distinct) vertices in our graph. Return “Possible” if it is possible to travel from x to y by following the edges of our graph. Otherwise, return “Impossible”. In other words, return “Possible” if our graph contains a path that connects the nodes x and y, and “Impossible” if there is no such path.

Definition

Class:

GCDGraph
Method:
possible
Parameters:
int, int, int, int
Returns:
string
Method signature:
string possible(int n, int k, int x, int y)
(be sure your method is public)
Limits

Time limit (s):

2.000
Memory limit (MB):
256
Stack limit (MB):
256

Constraints

n will be between 2 and 1,000,000, inclusive.

k will be between 0 and n, inclusive.

x and y will be between 1 and n, inclusive.

Examples
0)

12

2
8
9
Returns: “Possible”
We have a graph with n = 12 nodes. As k = 2, vertices i and j are connected by an edge if and only if gcd(i, j) is strictly greater than 2. In this graph it is possible to travel from node 8 to node 9. One possible path: 8 -> 4 -> 12 -> 9.
1)

12

2
11
12
Returns: “Impossible”
This is the same graph as in Example 0, but now we are interested in another pair of nodes. It is not possible to travel from node 11 to node 12. In particular, in our graph node 11 is an isolated node because for any other node x we have gcd(11, x) = 1.
2)

12

2
11
11
Returns: “Possible”
A node is always reachable from itself.
3)

10

2
8
9
Returns: “Impossible”

4)

1000000

1000
12345
54321
Returns: “Possible”

5)

1000000

2000
12345
54321
Returns: “Impossible”

6)

2

0
1
2
Returns: “Possible”

【题目链接】:

【题解】

大意是说两个节点之间有边当且仅当两个节点的标号的gcd>k;
可以这样.
先枚举比k大的且比n小的数i;
然后它的倍数和它之间连了一条边.
表示这两个数的最大公因数为i;而i大于k;所以满足题意;
而所有i的出度点之间则肯定也有路径可以到达了。
可以这样想?
两个数x,y的gcd为i
则i和y的gcd为i
i和x的gcd也为i
即x和y肯定是i的倍数.
所以如果i大于k
这对关系x,y肯定能找出来;(用并查集判断就可以了);
其他的要通过间接关系找出来的也同理吧!
用并查集描述两个数之间是否联通即可.
【完整代码】

#include 
using namespace std;#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define LL long long#define rep1(i,a,b) for (int i = a;i <= b;i++)#define rep2(i,a,b) for (int i = a;i >= b;i--)#define mp make_pair#define pb push_back#define fi first#define se secondtypedef pair
pii;typedef pair
pll;const int MAXN = 1e6+100;const int dx[5] = {
0,1,-1,0,0};const int dy[5] = {
0,0,0,-1,1};const double pi = acos(-1.0);int f[MAXN];int ff(int x){ if (f[x]!=x) f[x] = ff(f[x]); else return x; return f[x];}class GCDGraph{ public: string possible(int n, int k, int x, int y) { rep1(i,1,n) f[i] = i; rep1(i,k+1,n) { int fa = ff(i); for (int j = 2*i;j <= n;j+=i) { int r1 = ff(j); f[r1] = fa; } } if (ff(x)==ff(y)) return "Possible"; else return "Impossible"; }};

转载于:https://www.cnblogs.com/AWCXV/p/7626822.html

你可能感兴趣的文章
一种新的人才流动形式
查看>>
HTTP基础04--web服务器
查看>>
DOM--4 响应用户操作和事件(2)
查看>>
ubuntu16.04下无线网卡无法正常连网
查看>>
linux下去掉pdf的密码(前提:知道密码)
查看>>
Aspose.word在asp.net mvc中如何使用的个人总结
查看>>
JS运行机制
查看>>
nohup 部署springboot 使用命令
查看>>
微信小程序开发之页面数据绑定
查看>>
SQL Server 2008 阻止保存要求重新创建表的更改问题的设置方法
查看>>
【笔记】实现一个简易的Promise
查看>>
SQL语法--DQL
查看>>
Sublime Text 3安装Latex
查看>>
笔谈kxmovie开源播放器库的使用
查看>>
SQLSERVER Truncate使用注意事项
查看>>
MACOS配置VIM成简单IDE傻瓜式操作
查看>>
第11月第31天 keyboardwillshow CGAffineTransformMakeTranslation
查看>>
jersey
查看>>
创建hbase-indexer出现 0 running
查看>>
LeetCode Total Hamming Distance
查看>>