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. ActiveMQ学习总结(7)——ActiveMQ使用场景

    MQ简介: MQ全称为Message Queue, 消息队列(MQ)是一种应用程序对应用程序的通信方法.应用程序通过写和检索出入列队的针对应用程序的数据(消息)来通信,而无需专用连接来链接它们.消息传 ...

  2. Android——解决port占用问题导致的模拟器无法识别

    遇到一个问题:昨天模拟器工作还正常,今天eclipse就识别不了了.后来发现是360手机助手占用了5555port造成的,我就纳闷了,平时这个也不是自己主动启动.今天就启动了.废话不多说,就几个步骤就 ...

  3. 教你win7解除阻止程序运行怎么操作

    教你win7解除阻止程序运行怎么操作 来源:http://www.windows7en.com/jiaocheng/27594.html 有时候我下载的软件,被win7系统禁止了运行了时软件不能使用, ...

  4. Flume的可靠性

    Flume的可靠性 当节点出现故障时,日志能够被传送到其他节点上而不会丢失. Flume提供了三种级别的可靠性保障,从强到弱依次分别为:end-to- end(收到数据agent首先将event写到磁 ...

  5. 升级你的Linux日志系统

    650) this.width=650;" onclick='window.open("http://blog.51cto.com/viewpic.php?refimg=" ...

  6. weblogic安装(无界面静默安装)

    一.环境准备 1. 用户准备 Generic通用版weblogic不能用ROOT用户安装,如无其他用户需先创建用户,创建用户步骤此处略过 2. 下载weblogic 在官网下载weblogic,将下载 ...

  7. wps如何输入连续的长破折号

    最近在写论文, 想输入破折号,结果是— — 这个破折号中间是有缝隙的, 如何变成没有缝隙. 第一步,选中: 第二步,右击选择字体 第三步,放大(只加一个破折号,然后放大到200%) 不知道有没有人,像 ...

  8. NOIP2016 天天爱跑步(线段树/桶)

    题目描述 小c同学认为跑步非常有趣,于是决定制作一款叫做<天天爱跑步>的游戏.天天爱跑步是一个养成类游戏,需要 玩家每天按时上线,完成打卡任务. 这个游戏的地图可以看作一一棵包含 N个结点 ...

  9. Robot Framework 自动化测试

    Robot Framework 自动化测试 RIDE 是 Robot Framework 测试数据的编辑器.它使测试用例的创建.运行.测试项目的组织可以在图形界面下完成. 通过 RIDE 去学习和使用 ...

  10. CISP/CISA 每日一题 20

    CISSP 每日一题(答) What methods can be used to protectmobile devices such as a smartphone? Encryption,GPS ...