CodeForces 916C Jamie and Interesting Graph (构造)
题意:给定两个数,表示一个图的点数和边数,让你构造出一个图满足 1- n 的最短路是素数,并且最小生成树也是素数。
析:首先 1 - n 的最短路,非常好解决,直接 1 连 n 就好了,但是素数尽量选小的,选2,3,5,这样比较小的,然后再构造MST,可以给每个边都是 1,然后最后 n-2 连 n-1的时候,保证加起来是素数就好,然后剩下的边随便连,凑够边数就好,但是权值要尽量的大,但不要超过1e9,一开始没看到 1e9,写大了,1e8就够用了。
代码如下:
#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#include <numeric>
#define debug() puts("++++")
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
//#define all 1,n,1
#define FOR(i,x,n) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 1e8;
const LL LNF = 1e17;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 100 + 10;
const int maxm = 3e5 + 10;
const LL mod = 1e9 + 7LL;
const int dr[] = {-1, 1, 0, 0, 1, 1, -1, -1};
const int dc[] = {0, 0, 1, -1, 1, -1, 1, -1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
} bool is_prime(int x){
int t = sqrt(x + 0.5);
for(int i = 2; i <= t; ++i)
if(x % i == 0) return false;
return true;
} int main(){
scanf("%d %d", &n, &m);
if(n == 2){ printf("3 3\n1 2 3\n"); return 0; }
int sp = 3, mstp = m + 2;
for(int i = m + 2; !is_prime(i); mstp = ++i);
printf("%d %d\n", sp, mstp);
printf("%d %d %d\n", 1, n, 3);
int det = mstp - n;
m -= 2;
for(int i = 1; i <= n-3; ++i, --m) printf("%d %d 1\n", i, i + 1);
printf("%d %d %d\n", n-2, n-1, det);
if(m) printf("%d %d %d\n", n-1, n, INF);
--m;
for(int i = 1; i <= n && m > 0; ++i)
for(int j = i+2; j <= n && m > 0; ++j)
if(i != 1 || j != n) printf("%d %d %d\n", i, j, INF), --m;
return 0;
}
CodeForces 916C Jamie and Interesting Graph (构造)的更多相关文章
- Codeforces 916C - Jamie and Interesting Graph
916C - Jamie and Interesting Graph 思路:构造. 对于1到n最短路且素数,那么1到n之间连2 对于最小生成树,找一个稍微大点的素数(比1e5大)构造一个和为这个素数的 ...
- Jamie and Interesting Graph CodeForces - 916C
http://codeforces.com/problemset/problem/916/C 好尬的题啊... #include<cstdio> #include<algorithm ...
- 【Codeforces Round #457 (Div. 2) C】Jamie and Interesting Graph
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 找比n-1大的最小的素数x 1-2,2-3..(n-2)-(n-1)长度都为1 然后(n-1)-n长度为(x-(n-2)) 然后其他 ...
- CF916C Jamie and Interesting Graph
思路:构造 实现: #include <bits/stdc++.h> using namespace std; int main() { int n, m; cin >> n ...
- Codeforces Beta Round #9 (Div. 2 Only) E. Interesting Graph and Apples 构造题
E. Interesting Graph and Apples 题目连接: http://www.codeforces.com/contest/9/problem/E Description Hexa ...
- Codeforces 1109D. Sasha and Interesting Fact from Graph Theory
Codeforces 1109D. Sasha and Interesting Fact from Graph Theory 解题思路: 这题我根本不会做,是周指导带飞我. 首先对于当前已经有 \(m ...
- Codeforces 703D Mishka and Interesting sum 离线+树状数组
链接 Codeforces 703D Mishka and Interesting sum 题意 求区间内数字出现次数为偶数的数的异或和 思路 区间内直接异或的话得到的是出现次数为奇数的异或和,要得到 ...
- Codeforces 1109D Sasha and Interesting Fact from Graph Theory (看题解) 组合数学
Sasha and Interesting Fact from Graph Theory n 个 点形成 m 个有标号森林的方案数为 F(n, m) = m * n ^ {n - 1 - m} 然后就 ...
- Codeforces Global Round 4 Prime Graph CodeForces - 1178D (构造,结论)
Every person likes prime numbers. Alice is a person, thus she also shares the love for them. Bob wan ...
随机推荐
- stark组件之路由分发【模仿Django的admin】
一.先看下django的admin是如何进行路由分发的 1.先看下django的admin的url路径有哪些 其实很简单,假如有一个书籍表,那么每张表对应四个url,增.删.改.查 查看的url ht ...
- Bootstrap(4) 表单和图片
1.表单 基本格式,实现基本的表单样式 <form class="form-horizontal"> <div class="form-group&qu ...
- Composer 安装以及使用方法
Composer 是 PHP 的一个依赖管理工具.它允许你申明项目所依赖的代码库,它会在你的项目中为你安装他们. Linux 下安装 curl -sS https://getcomposer.org/ ...
- gson所需jar包
共需要四个jar包,分别为: gson-2.0.jar log4j-1.2.17.jar slf4j-api-1.7.10.jar slf4j-log4j12-1.7.10.jar 如果想要进行文件读 ...
- Java并发集合(二)-ConcurrentSkipListMap分析和使用
一.ConcurrentSkipListMap介绍 ConcurrentSkipListMap是线程安全的有序的哈希表,适用于高并发的场景.ConcurrentSkipListMap和TreeMap, ...
- 12.Mysql存储过程和函数
12.存储过程和函数12.1 什么是存储过程和函数存储过程和函数是事先经过编译并存储在数据库中的一段SQL语句的集合,调用存储过程和函数简化应用开发人员的工作,减少数据在数据库和应用服务器之间的传输, ...
- linux系统web站点设置-http基础设置
一.httpd2.2的组成: /etc/httpd:服务器的根目录 conf/httpd.conf,conf.d/*:配置文件 conf/magic:MIME的配置文件 logs:日志文件的存放路径, ...
- Codeforces 757B. Bash's Big Day GCD
B. Bash's Big Day time limit per test:2 seconds memory limit per test:512 megabytes input:standard i ...
- 电商类web原型制作分享——美丽说【附源文件】
美丽说是国内白领女性时尚消费品牌,精选上千家优质卖家供应商,为用户提供女装.女鞋.女包.配饰.美妆等品类的优质时尚商品. 此原型模板所用到的组件有搜索框.下拉菜单.输入框.选项卡等.交互动作有切换选项 ...
- python 读取xml
#!/usr/bin/python # -*- coding: UTF- -*- from xml.dom.minidom import parse import xml.dom.minidom # ...