Codeforces Round #228 (Div. 1) B. Fox and Minimal path 构造
B. Fox and Minimal path
题目连接:
http://codeforces.com/contest/388/problem/B
Description
Fox Ciel wants to write a task for a programming contest. The task is: "You are given a simple undirected graph with n vertexes. Each its edge has unit length. You should calculate the number of shortest paths between vertex 1 and vertex 2."
Same with some writers, she wants to make an example with some certain output: for example, her birthday or the number of her boyfriend. Can you help her to make a test case with answer equal exactly to k?
Input
The first line contains a single integer k (1 ≤ k ≤ 109).
Output
You should output a graph G with n vertexes (2 ≤ n ≤ 1000). There must be exactly k shortest paths between vertex 1 and vertex 2 of the graph.
The first line must contain an integer n. Then adjacency matrix G with n rows and n columns must follow. Each element of the matrix must be 'N' or 'Y'. If Gij is 'Y', then graph G has a edge connecting vertex i and vertex j. Consider the graph vertexes are numbered from 1 to n.
The graph must be undirected and simple: Gii = 'N' and Gij = Gji must hold. And there must be at least one path between vertex 1 and vertex 2. It's guaranteed that the answer exists. If there multiple correct answers, you can output any of them.
Sample Input
2
Sample Output
4
NNYY
NNYY
YYNN
YYNN
Hint
题意
你需要构造一个图,使得1到2的最短路恰好有k条
题解:
拆成二进制,比如9 = 20+23
二进制的最短路就非常好构造,就2*2*2这样去构造就好了
然而这样裸的构造的话,点数可能会超过1000个点
所以你再压缩一下点的个数,复用一下就好了
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 3200;
int mp[maxn][maxn];
int k,bit[32],tot=3,max_len;
int getid()
{
return tot++;
}
int main()
{
scanf("%d",&k);
int id1=1,id2=1,id3=getid();
while(k)
{
if(k%2)mp[id1][id3]=1,mp[id2][id3]=1;
k/=2;if(k==0)break;
int id4=getid(),id5=getid(),id6=getid();
mp[id1][id4]=1,mp[id1][id5]=1;
mp[id2][id4]=1,mp[id2][id5]=1;
mp[id3][id6]=1;
id1=id4,id2=id5,id3=id6;
}
mp[id3][2]=1;
int Max = getid()-1;
printf("%d\n",Max);
for(int i=1;i<=Max;i++,cout<<endl)for(int j=1;j<=Max;j++)
if(mp[i][j]||mp[j][i])printf("Y");else printf("N");
}
Codeforces Round #228 (Div. 1) B. Fox and Minimal path 构造的更多相关文章
- Codeforces Round #228 (Div. 1) 388B Fox and Minimal path
链接:http://codeforces.com/problemset/problem/388/B [题意] 给出一个整数K,构造出刚好含有K条从1到2的最短路的图. [分析] 由于是要自己构造图,当 ...
- Codeforces Round #228 (Div. 1) C. Fox and Card Game 博弈
C. Fox and Card Game 题目连接: http://codeforces.com/contest/388/problem/C Description Fox Ciel is playi ...
- Codeforces Round #228 (Div. 1) A. Fox and Box Accumulation 贪心
A. Fox and Box Accumulation 题目连接: http://codeforces.com/contest/388/problem/A Description Fox Ciel h ...
- Codeforces Round #228 (Div. 2) C. Fox and Box Accumulation(贪心)
题目:http://codeforces.com/contest/389/problem/C 题意:给n个箱子,给n个箱子所能承受的重量,每个箱子的重量为1: 很简单的贪心,比赛的时候没想出来.... ...
- Codeforces Round #228 (Div. 2) C. Fox and Box Accumulation
C. Fox and Box Accumulation time limit per test 1 second memory limit per test 256 megabytes input s ...
- Codeforces Round #228 (Div. 2) B. Fox and Cross
#include <iostream> #include <string> #include <vector> #include <algorithm> ...
- Codeforces Round #228 (Div. 2) A. Fox and Number Game
#include <iostream> #include <algorithm> #include <vector> #include <numeric> ...
- Codeforces Round #228 (Div. 2)
做codeforces以来题目最水的一次 A题: Fox and Number Game 题意:就是用一堆数字来回减,直到减到最小值为止,再把所有最小值加,求这个值 sol: 简单数论题目,直接求所有 ...
- Codeforces Round #228 (Div. 1) B
B. Fox and Minimal path time limit per test 1 second memory limit per test 256 megabytes input stand ...
随机推荐
- 跳出python的各种坑(1)
2017-11-1915:38:17 一定要跳出python的各种坑,一开始遇到的好多思维上的认知错误,因为刚开始学习,对python是个什么都不清楚,所以记录一下自己遇到的各种坑.不用担心自己遇到的 ...
- javascript多投事件的处理 (转)
出处 http://blog.csdn.net/dead_of_winter/article/details/1646367 尽管ecma标准指定了addEventListener这样的方法来实现事件 ...
- 2->集群架构主机优化流程
集群架构优化流程: 有道笔记分享链接
- 左列動態添加菜單Repeater
前台代碼: <%@ Page Language="C#" AutoEventWireup="true" CodeFile="left.aspx. ...
- matlab设定mex接驳的C/C++编译器
使用C/C++编写核心算法,使用matlab调用算法.做上层封装,通常是提升效率并提供易用性的一个不错的选择. mex需要设定接驳的C/C++编译器,官方文档在这里:https://ww2.mathw ...
- Jquery Datatable添加复选框,实现批量操作。
最近一段时间,一直在写前端的东西,自己也不擅长,最近也有所长进,把工作中用到的一些前端知识整理一下,下次用到就不用再找了.这次主要是在datatable中添加复选框,然后实现批量操作的功能.因为是公司 ...
- hdu 3951 硬币围成一圈(博弈)
n个硬币围成一个环 每次只能取1-K个硬币 最后取完者胜 假如5个硬币 每次取1-2个情况1 先手取1个 后手取剩下4个中间2个 破坏了连续 虽然最后剩2个,但先手只能取一个 然后后再取一个 后手胜 ...
- python 字符串截断
>>> s = '%20and%201=2%20union%20select%201,group_concat%28table_name%29,3,4,5,6,7,8,9,10,11 ...
- poj-1251-最小生成树
title: poj-1251-最小生成树 date: 2018-11-20 16:38:14 tags: acm 刷题 categories: ACM-最小生成树 概述 前段时间数据结构的课上提到了 ...
- CentOS6启动流程(含详细流程图)
参考:Linux启动流程和grub详解(作者:好笔记运维) 为什么把这位的参考放在前面,主要是这位大佬的流程图太详细了.虽说不一定要了解这么详细,但还是很佩服啊.不多说,上图(在新标签中打开图片) 下 ...