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 <bits/stdc++.h>
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 second typedef pair<int,int> pii;
typedef pair<LL,LL> 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";
}
};

【TP SRM 703 div2 500】 GCDGraph的更多相关文章

  1. 【TP SRM 703 div2 250】AlternatingString

    Problem Statement A string of zeros and ones is called an alternating string if no two adjacent char ...

  2. 【SRM 717 DIV2 C】DerangementsDiv2

    Problem Statement You are given two ints: n and m. Let D be the number of permutations of the set {1 ...

  3. 【SRM 717 div2 B】LexmaxReplace

    Problem Statement Alice has a string s of lowercase letters. The string is written on a wall. Alice ...

  4. 【SRM 717 div2 A】 NiceTable

    Problem Statement You are given a vector t that describes a rectangular table of zeroes and ones. Ea ...

  5. 求拓扑排序的数量,例题 topcoder srm 654 div2 500

    周赛时遇到的一道比较有意思的题目: Problem Statement      There are N rooms in Maki's new house. The rooms are number ...

  6. Topcoder SRM 619 DIv2 500 --又是耻辱的一题

    这题明明是一个简单的类似约瑟夫环的问题,但是由于细节问题迟迟不能得到正确结果,结果比赛完几分钟才改对..耻辱. 代码: #include <iostream> #include <c ...

  7. tc srm 636 div2 500

    100的数据直接暴力就行,想多了... ac的代码: #include <iostream> #include <cstdio> #include <cstring> ...

  8. 【Codeforces #312 div2 A】Lala Land and Apple Trees

    # [Codeforces #312 div2 A]Lala Land and Apple Trees 首先,此题的大意是在一条坐标轴上,有\(n\)个点,每个点的权值为\(a_{i}\),第一次从原 ...

  9. 【JAVA零基础入门系列】Day7 Java输入与输出

    [JAVA零基础入门系列](已完结)导航目录 Day1 开发环境搭建 Day2 Java集成开发环境IDEA Day3 Java基本数据类型 Day4 变量与常量 Day5 Java中的运算符 Day ...

随机推荐

  1. 洛谷 P1105 平台

    P1105 平台 题目描述 空间中有一些平台.给出每个平台的位置,请你计算从每一个平台的边缘落下之后会落到哪一个平台上.注意,如果某两个平台的某个两边缘横坐标相同,物体从上面那个平台落下之后将不会落在 ...

  2. js39---组合模式,查找遍历树

    /** *有这样一个需求 *有一个学校有2个班(一班,二班) *每个班级分2个小组(一班一组,一班二组,二班一组,二班二组) *学校计算机教室有限,每一个小组分着来上课. *考试的时候大家一起考 *请 ...

  3. worktools-不同分辨率下图片移植

    1.下载需要移植的平台代码 1)查看手机需要的项目平台信息:adb shell getprop | gerp flavor     ----->mt6732_m561_p2_kangjia_cc ...

  4. android图像处理系列之四--给图片添加边框(上)

    图片处理时,有时需要为图片加一些边框,下面介绍一种为图片添加简单边框的方法. 基本思路是:将边框图片裁剪成八张小图片(图片大小最好一致,不然后面处理会很麻烦),分别对应左上角,左边,左下角,下边,右下 ...

  5. 1.26 Python知识进阶 - 继承

    继承 继承(Inheritance)是面向对象的程序设计中代码重要的主要方法.继承是允许使用现有类的功能,并在无需重新改写原来的类的情况下,对这些功能进行扩展.继承可以避免代码复制和相关的代码维护等问 ...

  6. 轻松学习Linux之Shell的常用过滤器

    下载高清视频: http://down.51cto.com/data/157818    大小3:MB      时长: 7分钟 更多内容见: Linux爱好者的圣诞大餐-轻松学习Linux系列多媒体 ...

  7. 内核中的宏定义__init、__initdata和__exit、__exitdata

    __init.__initdata和__exit.__exitdata的定义位于<kernel/include/linux/init.h> /* These are for everybo ...

  8. 《WPF》Expander控件简单美化

    示例图: Expander控件功能很常见, 一般用于系统左侧的菜单收缩面板. 1.主要的组成 一个头部(header) 和 一个 内容(content) 组成. <Expander Expand ...

  9. 51NOD——T 1079 中国剩余定理

    http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1079 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难 ...

  10. Scala入门到精通——第二十二节 高级类型 (一)

    作者:摇摆少年梦 视频地址:http://www.xuetuwuyou.com/course/12 本节主要内容 this.type使用 类型投影 结构类型 复合类型 1. this.type使用 c ...