【TP SRM 703 div2 500】 GCDGraph
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的更多相关文章
- 【TP SRM 703 div2 250】AlternatingString
Problem Statement A string of zeros and ones is called an alternating string if no two adjacent char ...
- 【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 ...
- 【SRM 717 div2 B】LexmaxReplace
Problem Statement Alice has a string s of lowercase letters. The string is written on a wall. Alice ...
- 【SRM 717 div2 A】 NiceTable
Problem Statement You are given a vector t that describes a rectangular table of zeroes and ones. Ea ...
- 求拓扑排序的数量,例题 topcoder srm 654 div2 500
周赛时遇到的一道比较有意思的题目: Problem Statement There are N rooms in Maki's new house. The rooms are number ...
- Topcoder SRM 619 DIv2 500 --又是耻辱的一题
这题明明是一个简单的类似约瑟夫环的问题,但是由于细节问题迟迟不能得到正确结果,结果比赛完几分钟才改对..耻辱. 代码: #include <iostream> #include <c ...
- tc srm 636 div2 500
100的数据直接暴力就行,想多了... ac的代码: #include <iostream> #include <cstdio> #include <cstring> ...
- 【Codeforces #312 div2 A】Lala Land and Apple Trees
# [Codeforces #312 div2 A]Lala Land and Apple Trees 首先,此题的大意是在一条坐标轴上,有\(n\)个点,每个点的权值为\(a_{i}\),第一次从原 ...
- 【JAVA零基础入门系列】Day7 Java输入与输出
[JAVA零基础入门系列](已完结)导航目录 Day1 开发环境搭建 Day2 Java集成开发环境IDEA Day3 Java基本数据类型 Day4 变量与常量 Day5 Java中的运算符 Day ...
随机推荐
- android基于开源网络框架asychhttpclient,二次封装为通用网络请求组件
网络请求是全部App都不可缺少的功能,假设每次开发都重写一次网络请求或者将曾经的代码拷贝到新的App中,不是非常合理,出于此目的,我希望将整个网络请求框架独立出来,与业务逻辑分隔开,这样就能够避免每次 ...
- Irrlicht 3D Engine 笔记系列 之 教程5- User Interface
作者:i_dovelemon 日期:2014 / 12 / 18 来源:CSDN 主题:GUI 引言 今天.博主学习了第五个教程. 这个教程解说了怎样使用Irrlicht内置的一个基础模块.GUI模块 ...
- 命令行SVN的使用
1.检出svn co http://路径(目录或文件的全路径) [本地目录全路径] --username 用户名 --password 密码svn co svn://路径(目录或文件的全路径 ...
- 一个发邮件的demo 用golang
一个比较成熟的第三方包用来发邮件,可以带图片 和附件,项目地址 : github.com/go-gomail/gomail 一个发邮件的demo 用golang 文件目录树: -d:\test\goe ...
- 【Codeforces Round #452 (Div. 2) B】Months and Years
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 闰,平,平 平,闰,平 平,平,闰 平,平,平 4种情况都考虑到就好. 可能有重复的情况. 但是没关系啦. [代码] #includ ...
- http 500 Internal Server Error的错误 ajax请求SpringMVC后台中返回500 Internal Server Error
使用httprequester接口测试能返回数据,但是用ajax返回json格式的时候返回报500Internal Server Error. The server encountered an in ...
- 《开源公开课分享》:Java开源框架案例分享
缺乏高端技术人才?缺乏开发标准? 代码复用性低?技术风险难于把控? 招聘成本高?培训成本高? 假设想法不够雄伟,那么就会局限于细节:假设一開始就铺很大的摊子,将会失去控制: ...
- 从头认识java-17.4 具体解释同步(3)-对象锁
这一章节我们接着上一章节的问题,给出一个解决方式:对象锁. 1.什么是对象锁? 对象锁是指Java为临界区synchronized(Object)语句指定的对象进行加锁,对象锁是独占排他锁. 2.什么 ...
- zookeeper 配置文件说明(zoo.cfg)
clientPort # 客户端连接server的port,即对外服务port,一般设置为2181. dataDir # 存储快照文件snapshot的文件夹. 默认情况下.事 ...
- 【博客之星】CSDN2013博客之星--分析和预测
本文纯属个人见解,多有得罪啊! 具体结果,还是看最后CSDN给的结果吧! 昵称 名字 题材 质量 数量 知名度 预测 阳光岛主 杨刚 Python,Clojure,SAE 很高 346+ 很大 一定( ...