codeforces 1009D Relatively Prime Graph【欧拉函数】
题目:戳这里
题意:要求构成有n个点,m条边的无向图,满足每条边上的两点互质。
解题思路:
显然1~n这n个点能构成边的条数,就是2~n欧拉函数之和(x的欧拉函数值代表小于x且与x互质的数的个数。
因此m>n-1 && m <= sum成立则可以构成无向图。
接着求出1e5以内的欧拉函数,求和可以发现前1000项的欧拉值就已经远远大于1e5。
所以m条边直接两层循环暴力即可。
附本人代码:
1 #include <bits/stdc++.h>
2 typedef long long ll;
3 const int maxn = 1e5+10;
4 const ll inf = 1e18;
5 const ll mod = 1e9+7;
6 using namespace std;
7 ll cnt[maxn];
8 ll euler[maxn];
9 void geteuler() {
10 memset(euler, 0, sizeof(euler));
11 euler[1] = 1;
12 for(ll i = 2; i < maxn; ++i) {
13 if(!euler[i]) {
14 for(ll j = i; j < maxn; j+=i) {
15 if(!euler[j]) euler[j] = j;
16 euler[j] = euler[j]/i * (i - 1ll);
17 }
18 }
19 }
20 }
21 ll gcd(ll a, ll b) {return b?gcd(b,a%b):a;}
22 int main(){
23 ll n, m;
24 ll sum = 0;
25 scanf("%lld %lld", &n, &m);
26 geteuler();
27 for(ll i = 2; i <= n; ++i) {
28 sum += euler[i];
29 }
30
31 // printf("%lld\n", sum);
32 if(sum < m || m < n - 1) {
33 puts("Impossible");
34 return 0;
35 }
36 puts("Possible");
37 for(ll i = 1; i <= n; ++i) {
38 for(ll j = i + 1; j <= n; ++j) {
39 if(gcd(i,j)==1) {
40 printf("%lld %lld\n", i, j);
41 --m;
42 if(!m) return 0;
43 }
44 }
45 }
46 return 0;
47 }
codeforces 1009D Relatively Prime Graph【欧拉函数】的更多相关文章
- Codeforces 906D Power Tower(欧拉函数 + 欧拉公式)
题目链接 Power Tower 题意 给定一个序列,每次给定$l, r$ 求$w_{l}^{w_{l+1}^{w_{l+2}^{...^{w_{r}}}}}$ 对m取模的值 根据这个公式 每次 ...
- CodeForces - 1009D Relatively Prime Graph
题面在这里! 直接暴力找点对就行了,可以证明gcd=1是比较密集的,所以复杂度略大于 O(N log N) #include<bits/stdc++.h> #define ll long ...
- Codeforces 1114F Please, another Queries on Array? [线段树,欧拉函数]
Codeforces 洛谷:咕咕咕 CF少有的大数据结构题. 思路 考虑一些欧拉函数的性质: \[ \varphi(p)=p-1\\ \varphi(p^k)=p^{k-1}\times (p-1)= ...
- Codeforces Round #538 (Div. 2) F 欧拉函数 + 区间修改线段树
https://codeforces.com/contest/1114/problem/F 欧拉函数 + 区间更新线段树 题意 对一个序列(n<=4e5,a[i]<=300)两种操作: 1 ...
- CodeForces - 645F:Cowslip Collections (组合数&&欧拉函数)
In an attempt to make peace with the Mischievious Mess Makers, Bessie and Farmer John are planning t ...
- Codeforces 871D Paths (欧拉函数 + 结论)
题目链接 Round #440 Div 1 Problem D 题意 把每个数看成一个点,如果$gcd(x, y) \neq 1$,则在$x$和$y$之间连一条长度为$1$的无向边. ...
- Codeforces 1114F(欧拉函数、线段树)
AC通道 要点 欧拉函数对于素数有一些性质,考虑将输入数据唯一分解后进行素数下的处理. 对于素数\(p\)有:\(\phi(p^k)=p^{k-1}*(p-1)=p^k*\frac{p-1}{p}\) ...
- Please, another Queries on Array?(Codeforces Round #538 (Div. 2)F+线段树+欧拉函数+bitset)
题目链接 传送门 题面 思路 设\(x=\prod\limits_{i=l}^{r}a_i\)=\(\prod\limits_{i=1}^{n}p_i^{c_i}\) 由欧拉函数是积性函数得: \[ ...
- Codeforces 776E: The Holmes Children (数论 欧拉函数)
题目链接 先看题目中给的函数f(n)和g(n) 对于f(n),若自然数对(x,y)满足 x+y=n,且gcd(x,y)=1,则这样的数对对数为f(n) 证明f(n)=phi(n) 设有命题 对任意自然 ...
随机推荐
- MSDOS(MBR)和GPT磁盘分区表
MBR和GPT分区 MBR分区:以磁盘的第一个扇区(512byte)记录分区表,其中,446byte存储开机管理程序(MBR 主要开机记录),64byte用于存放分区表 分区实际上是对分区表的修改 M ...
- [从源码学设计]蚂蚁金服SOFARegistry之配置信息
[从源码学设计]蚂蚁金服SOFARegistry之配置信息 目录 [从源码学设计]蚂蚁金服SOFARegistry之配置信息 0x00 摘要 0x01 业务范畴 1.1 配置作用 1.2 学习方向 0 ...
- EXPORT和IMPORT使用示例
1 report ztestprog. 2 data:begin of itab1 occurs 0, 3 ff(10), 4 end of itab1. 5 data:itab2 like itab ...
- Dubbo的设计理念原来就藏在这三张图中
Dubbo在众多的微服务框架中脱颖而出,占据RPC服务框架的半壁江山,非常具有普适性,熟练掌握 Dubbo的应用技巧后深刻理解其内部实现原理,让大家能更好的掌控工作,助力职场,特别能让大家在面试中脱颖 ...
- SQL Server 日志收缩方法
在日常运维中,有时会遇到"The transaction log for database 'xxxx' is full due to 'ACTIVE_TRANSACTION'." ...
- 使用EFCore连接Oracle数据库时出现的问题
问题 System.TypeLoadException: Method 'Create' in type 'Oracle.EntityFrameworkCore.Infrastructure.Inte ...
- jquery 数据查询
jquery 数据查询 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> & ...
- Infrastructure as Code 行为驱动开发指南 https://www.ibm.com/developerworks/cn/devops/d-bbd-guide-iac/index.html
Infrastructure as Code 行为驱动开发指南 https://www.ibm.com/developerworks/cn/devops/d-bbd-guide-iac/index.h ...
- Go GC: Latency Problem Solved
https://talks.golang.org/2015/go-gc.pdf https://www.oschina.net/translate/go-gc-solving-the-latency- ...
- 系列trick - 随机
系列trick - 随机 不断更新中,欢迎来提供idea 随机的字符串 出现次数 \(\ge 2\) 的子串期望长度是 \(\log n\) 两个随机串的期望LCP,LCSuf,LCSub长度是 \( ...