Relatively Prime Graph(贪心+注意数据范围)

Graphad Prism6 Graphpad Prism 6 是个很好的作图软件,非常好用,简单易懂。做出来效果很好的啊 立即下载

Relatively Prime Graph

Let’s call an undirected graph G=(V,E) relatively prime if and only if for each edge (v,u)∈E GCD(v,u)=1 (the greatest common divisor of v and u is 1). If there is no edge between some pair of vertices v and u then the value of GCD(v,u) doesn’t matter. The vertices are numbered from 1 to |V|

.

Construct a relatively prime graph with n
vertices and m

edges such that it is connected and it contains neither self-loops nor multiple edges.

If there exists no valid graph with the given number of vertices and edges then output "Impossible".

If there are multiple answers then print any of them.

Input

The only line contains two integers n
and m (1≤n,m≤105) — the number of vertices and the number of edges.
Output

If there exists no valid graph with the given number of vertices and edges then output "Impossible".

Otherwise print the answer in the following format:

The first line should contain the word "Possible".

The i-th of the next m lines should contain the i-th edge (vi,ui) of the resulting graph (1≤vi,ui≤n,vi≠ui). For each pair (v,u) there can be no more pairs (v,u) or (u,v). The vertices are numbered from 1 to n

.

If there are multiple answers then print any of them.

Examples
Input

5 6

Output

Possible
2 5
3 2
5 1
3 4
4 1
5 4

Input

6 12

Output

Impossible

Note

Here is the representation of the graph from the first example: 

题意:给定n个点(1-n),让你构成一个图有m条边,使得每个边到两端点编号是互质的。
分析:一开始想如果暴力到话是O(n2n2log(n)),即枚举每个点,然后两辆求gcd,n是1e51e5会超时,就没写,以为有别的什么巧妙方法。实在没想出来搜了题解发现emmmmm….
数据里限定了m也是1e51e5级别的数,所以虽然是二重循环但是最多循环m次就停止了。
如果impossible【及贪心完造的边还没有m这么多】,那n一定很小,小到n2 log(n)n2 log(n)可以过;
如果possible,那一定n2n2还没有枚举完就造完m条边,直接break了。【因为n2n2枚举完造的边与m不是一个数量级的数】

code:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e5+10;
int u[maxn],v[maxn],edge;
int gcd(int a,int b){
    if(b == 0) return a;
    else return gcd(b,a%b);
}
int main(){
    int n,m;
    edge = 0;
    scanf("%d%d",&n,&m);
    if(m < n-1){
        printf("Impossible\n");
        return 0;
    }
    for(int i = 1; i <= n; i++){
        for(int j = i+1; j <= n; j++){
            if(edge == m) break;
            if(gcd(i,j) == 1){
                u[++edge] = i;
                v[edge] = j;
            }
        }
        if(edge == m) break;
    }
    if(edge == m){
        printf("Possible\n");
        for(int i = 1; i <= edge; i++){
            printf("%d %d\n",u[i],v[i]);
        }
    }
    else
        printf("Impossible\n");
    return 0;
}
解决windows10下无法安装.net framework 3.5(包括.net2.0和3.0) 入域电脑,win10系统中安装 .net framework 3.5(包括.net2.0和3.0) 阅读详情

相关推荐

NVIDIA DGX Spark (Ubuntu 24.04) 部署Isaac Sim与Isaac Lab:二进制安装与ROS2集成实战

本文详细介绍了在NVIDIA DGX Spark工作站(Ubuntu 24.04系统)上,通过预编译二进制包稳定部署Isaac Sim与Isaac Lab的完整实战流程。文章重点剖析了二进制安装相较于源码编译和pip安装的优势,有效规避了CUDA、PyTorch等关键组件的版本冲突陷阱,并逐步指导完成环境配置、Isaac Lab集成、Conda环境管理以及ROS2 Jazzy的集成,最终构建出完整的机器人仿真与强化学习开发平台。

kite3的博客 396

两个自然数互素(relatively prime)

<br />两个自然数是互素的当且仅当它们的最大公因子为1。<br />判断两个数是否为互素的问题是P问题。<br />方法1:遍历这两个数的所有可能的公因子(指数时间)<br />方法2:欧几里得算法

张林林|深蓝(Linlin Zhang,shenlan211314) 的专栏 6012

YOLO11实战:手把手教你优化钢材表面缺陷识别模型(基于NEU-DET数据集)

本文详细介绍了如何使用YOLO11优化钢材表面缺陷识别模型,基于NEU-DET数据集进行实战训练。从数据增强、模型架构优化到训练过程控制,全面解析提升钢材缺陷检测性能的关键技术,特别针对裂纹、划痕等典型缺陷提出针对性解决方案。

weixin_29081163的博客 379

[Codeforces 1178D]Prime Graph (思维+数学)

Codeforces 1178D (思维+数学) 题面 给出正整数n(不一定是质数),构造一个边数为质数的无向连通图(无自环重边),且图的每个节点的度数为质数 分析 我们先构造一个环,每个点的度数都是2。但由于n不一定是质数,我们还需要再加k条边。然后对于\(i \in [1,k]\),我们加边(i,i+n/2)。当\(k\leq \frac{n}{2}\)的时候,只会把一些点的度数由2...

weixin_30551963的博客 360

Scratch离线版部署指南:解决资源缺失与页面显示异常问题

本文提供了一份详尽的Scratch离线版部署指南,旨在解决网络依赖导致的资源缺失与页面显示异常问题。通过从基础文件获取、自动化资源抓取到生产环境部署的完整流程,帮助教育工作者和开发者在无网络环境下搭建功能完整的本地Scratch编辑器,确保编程教学的顺利进行。

wine的专栏 1261

Solidworks二次开发(C# .NET API):从零构建装配体与虚拟零件

本文详细介绍了使用C#进行Solidworks二次开发,从零构建装配体与虚拟零件的完整流程。通过.NET API实现设计自动化,涵盖环境搭建、核心对象模型、虚拟零件创建与外部化保存等关键技术,帮助工程师将重复性建模工作程序化,大幅提升设计效率与标准化水平。

alice7model的博客 607

Prime Graph(思维 + 数学)

题目链接:http://codeforces.com/problemset/problem/1178/D   Every person likes prime numbers. Alice is a person, thus she also shares the love for them. Bob wanted to give her an affectionate gift but cou...

桃夭丶的博客 521

基于WebGL架构的3D可视化平台—平面图导航(一)

前言 利用CampusBuilder来搭建自己的虚拟世界过程有这样一个问题:如何快速聚焦到虚拟场景的某一位置。当然我们可以创建几个按钮对应查找我们需要去的位置(参照物)并聚焦,但是按钮并不是很炫酷也不能很好的反馈给我们一些信息。接下来我们就用平面导航图来解决这一问题。 第一步:使用CampusBuilder搭建模拟场景,CampusBuilder操作简单,分分钟就可以上手。 ...

nioooom的博客 4162

D. Relatively Prime Graph(构造+数论)

https://codeforces.com/problemset/problem/1009/D 思路: 特殊点从1开始往后每个点都连,然后2,3.... 实际上等价于每个i和前面中与自己互质的连。那么就是欧拉函数。每个i就是欧拉函数前缀和。 于是暴力即可。 #include<iostream> #include<vector> #include<queue> #include<cstring> #include<cmath> #i

代码笔记本 284

人形机器人开源资源大盘点(四):硬件开源平台

开源人形机器人平台推动技术民主化 本期聚焦四大开源人形机器人平台:斯坦福的ToddlerBot以6000美元低成本实现30自由度运动,支持仿真到现实的零样本迁移;法国Reachy2被HuggingFace收购,融合开源AI与硬件,适配医疗、工业等场景;国内OriginMan基于ROS2全开源,集成大模型能力,支持深度改造;OAN框架则为经典NAO机器人注入开源活力,扩展交互功能。这些平台通过不同路径降低研发门槛,推动人形机器人技术走向开放共享的未来。

Studying…… 370

第十届MathorCup高校数学建模挑战赛-B题:养老服务床位需求预测与运营模式研究

当养老床位数增加时存在的商机,提出了相应的养老服务床位的运营商业模式。就业的可能性,建立多目标规划模型,尽可能同时满足三方面的要求。务床位的需求、制定合理的养老服务床位发展规划,是构建和谐社会、幸福社会。问题一的床位需求与市场类型的分类结果,查找关于国内养老院的相关数据,探。求,又能持续发展养老事业,同时还能促进社会就业的养老服务床位运营的商业。养老服务床位数的增加能够带来的就业方面因素的影响,并对这些因素进行一个。你们的模型及其结论为科学依据,对政府管理部门针对养老床位规划提出合理的。

getusushu的博客 1139

Relatively Prime Graph (暴力+GCD)

Let's call an undirected graph G=(V,E) relatively prime if and only if for each edge (v,u)∈E  GCD(v,u)=1 (the greatest common divisor of v and u is 1). If there is no edge between some pair of vertice...

码农的一亩三分地 457

VC中sqlite的使用

在VC中对sqlite的使用,实现数据库大批数据的快速插入,读取数据库中的数据在ListCtrl中显示

Codeforces 1009D:Relatively Prime Graph

D. Relatively Prime Graphtime limit per test2 secondsmemory limit per test256 megabytesinputstandard inputoutputstandard outputLet's call an undirected graph G=(V,E)G=(V,E) relatively prime if and onl...

执念 506

IDEA的使用指南

IDEA基于java的使用手册,可以最快速的上手,在使用中不断的丰富自己的使用技巧.

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

D. Relatively Prime Graph

Let's call an undirected graphG=(V,E)G=(V,E)relatively primeif and only if for each edge(v,u)∈E(v,u)∈EGCD(v,u)=1GCD(v,u)=1(the greatest common divisor ofvvanduuis11). If there is no edge...

weixin_30653023的博客 164

计算机控制技术及应用

《计算机控制技术及应用》通过总结作者的科研成果和吸收国内外的先进理论、方法和技术,以计算机控制系统的网络化、开放化、智能化和集成化发展趋势为主线索,主要介绍计算机控制系统的基本概念和计算机控制系统的典型形式,计算机控制系统的数学描述与基本分析方法,以及数字PID控制器、最少拍控制系统、达林算法、状态变量设计法等设计方法及其各种改进算法。重点突出计算机控制系统输入输出通道接口的设计方法和典型控制系统的设计案例与实现方法等。

Educational Codeforces Round 47 (Rated for Div. 2) D ---- Relatively Prime Graph

暴力出奇迹,暴力枚举所有的组合如果满足了n个就ok,判断图是否可以联通,边数够不够m个,判断是否有答案。#include &lt;bits/stdc++.h&gt; using namespace std; const int maxn = 1e5 + 10; int gcd(int n,int m) {return m == 0 ? n : gcd(m,n%m);} struct node{ i...

CCCCTong的博客 633

Codeforces ~ 1009D ~ Relatively Prime Graph (构造)

题意构造一个n个点m条边的图,要求:图联通,GCD(u,v)=1,u~v才可以建边。题解由欧拉函数表φ(n)可得,573以内的互素的对数就已经超过1e5了,所以我们暴力枚举即可。#include &lt;bits/stdc++.h&gt; using namespace std; int n, m; vector&lt;pair&lt;int, int&gt; &gt; ans; int main...

张松超的博客 615

Codeforces Global Round 4 - 1178D. Prime Graph(构造+切比雪夫定理)

题目链接:https://codeforces.com/contest/1178/problem/D 题意:给出个点,要求构成一个简单图,使得边的总数是素数,并且每个点的度数也是素数。 思路:如果是素数直接把这些点连成一个环即可,如果不是质数,那么从开始,在圆环中加入的边。这样一定会连条边,而且每一个点的度数不是2就是3。根据伯特兰-切比雪夫定理:之间一定有一个素数,所以...

sugarbliss 604

Relatively Prime Graph (暴力)

Let's call an undirected graph G=(V,E) relatively prime if and only if for each edge (v,u)∈E GCD(v,u)=1 (the greatest common divisor of v and u is 1). If there is no edge between some pair of vertices v and u then the value of GCD(v,u) doesn't matter. Th..

C_Dreamy的博客 455

B - Relatively Prime Graph -CodeForces - 1009D-csdn博客

B - Relatively Prime Graph -CodeForces - 1009D-csdn博客 Let’s call an undirected graph G=(V,E)relatively prime if and only if for each edge (v,u)∈E GCD(...

i-Curve的博客 211

Relatively Prime Pairs

Description: You are given a set of all integers from ll to rr inclusive, l&lt;rl&lt;r, (r−l+1)≤3⋅105 and (r−l) is always odd. You want to split these numbers into exactly (r−l+1)/2 pairs in such a wa...

qq_40486952的博客 313

注意数据类型的范围

求Sn=1!+2!+3!+4!+5!+…+n!之值,其中n是一个数字(n不超过20)。 一道很简单的算法题,总是运行错误,我的代码是 import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int n= sc.nextInt(); int Sn=0;

zrllllll的博客 140

[K分图判定]长安大学第二届ACM校赛题解 K-partite Graph

描述 We are all familiar with bipartite graph, actually it can be extended to multipartite graph.If vertices of an undirected graph G can be divided into exactly k(k≥2) non-empty sets, and for each

你那么爱笑,家里人知道吗? 2614

蓝桥杯——立方尾不变(注意数据范围)

有些数字的立方的末尾正好是该数字本身。 比如:1,4,5,6,9,24,25,.... 请你计算一下,在10000以内的数字中(指该数字,并非它立方后的数值),符合这个特征的正整数一共有多少个。 请提交该整数,不要填写任何多余的内容。 思路: 这个题目很简单就是按部就班的来就行了,就是在处理比较大的数据是可能会超出int 的范围,这样做出的答案是27,缺少了几个 所以就需要在算平方...

red_red_red的博客 1186

C - 注意数据范围

题目Theatre Square in the capital city of Berland has a rectangular shape with the size n × m meters. On the occasion of the city’s anniversary, a decision was taken to pave the Square with square granit

low_beeman的博客 498

Codeforces 1009D Relatively Prime Graph

Let's call an undirected graph G=(V,E) relatively prime if and only if for each edge (v,u)∈E  GCD(v,u)=1 (the greatest common divisor of v and u is 1). If there is no edge between some pair of vertice...

包子不卖包子 263
上一篇: Battle ships HDU - 5093(二分图最大匹配)
下一篇: Atlantis HDU - 1542(线段树+扫描线入门)
Guuuuuu老师儿
博客等级 码龄9年 259粉丝 718原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值