Codeforces Round #228 (Div. 1) B
1 second
256 megabytes
standard input
standard output
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?
The first line contains a single integer k (1 ≤ k ≤ 109).
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 ton.
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.
2
4
NNYY
NNYY
YYNN
YYNN
9
8
NNYYYNNN
NNNNNYYY
YNNNNYYY
YNNNNYYY
YNNNNYYY
NYYYYNNN
NYYYYNNN
NYYYYNNN
1
2
NY
YN
In first example, there are 2 shortest paths: 1-3-2 and 1-4-2.
In second example, there are 9 shortest paths: 1-3-6-2, 1-3-7-2, 1-3-8-2, 1-4-6-2, 1-4-7-2, 1-4-8-2, 1-5-6-2, 1-5-7-2, 1-5-8-2.
整数N拆写成二进制。
如 15 = 2^3 + 2^2 + 2^1 +2^0 ;
#include <iostream>
#include <stdio.h>
#include <string>
#include <string.h>
#include <algorithm>
#include <stdlib.h>
#include <vector>
#include <set>
#include <math.h>
#include <map>
using namespace std;
typedef long long LL ; int pow2[] ;
vector<int> ans ;
vector<int> ::iterator it ;
map<int ,int>my_hash ;
int shortest ;
int id ;
const int S = ;
const int T = ;
bool grid[][] ; void make_line(int dot , int type){
vector < pair<int ,int> > me ;
vector < pair<int ,int> > ::iterator it ;
me.clear() ;
for(int i = ; i <= dot ; i++){
me.push_back(make_pair(id,id+)) ;
id += ;
}
if(type == && dot != shortest){
my_hash.clear() ;
for(int i = dot + ; i <= shortest ; i++){
me.push_back(make_pair(id,id)) ;
my_hash[i] = id ;
id += ;
}
}
it = me.begin() ;
grid[S][it->first] = grid[S][it->second] = ;
grid[it->first][S] = grid[it->second][S] = ;
if(type == && dot != shortest){
it = me.end() - ;
grid[it->first][T] = grid[it->second][T] = ;
grid[T][it->first] = grid[T][it->second] = ;
}
else {
it = me.end() - ;
int v = (dot == shortest ? T : my_hash[dot+]) ;
grid[it->first][v] = grid[it->second][v] = ;
grid[v][it->first] = grid[v][it->second] = ;
}
for(int i = ; i < me.size() ; i++){
int u1 = me[i-].first ;
int u2 = me[i-].second ;
int v1 = me[i].first ;
int v2 = me[i].second ;
grid[u1][v1] = grid[u1][v2] = ;
grid[u2][v1] = grid[u2][v2] = ; grid[v1][u1] = grid[v1][u2] = ;
grid[v2][u1] = grid[v2][u2] = ;
}
} void output(){
int n = id - , i , j ;
cout<<n<<endl ;
for(i = ; i <= n ; i++){
for(j = ; j <= n ; j++)
printf("%s",grid[i][j]?"Y":"N") ;
puts("") ;
}
} int main(){
int i , k ,sum = ;
pow2[] = ;
for(i = ; i <= ; i++)
pow2[i] = pow2[i-] * ;
cin>>k ;
if(k == ){
puts("") ;
puts("NY") ;
puts("YN") ;
return ;
}
ans.clear() ;
for(i = ; i >= ; i--){
if(k >= pow2[i]){
ans.push_back(i) ;
k -= pow2[i] ;
}
}
memset(grid,,sizeof(grid)) ;
shortest = *ans.begin() ;
id = ;
make_line(ans[ans.size()-] ,) ;
for(i = ans.size() - ; i >= ; i--)
make_line(ans[i] , ) ;
output() ;
return ;
}
Codeforces Round #228 (Div. 1) B的更多相关文章
- Codeforces Round #228 (Div. 2) C. Fox and Box Accumulation(贪心)
题目:http://codeforces.com/contest/389/problem/C 题意:给n个箱子,给n个箱子所能承受的重量,每个箱子的重量为1: 很简单的贪心,比赛的时候没想出来.... ...
- Codeforces Round #228 (Div. 1)
今天学长给我们挂了一套Div.1的题,难受,好难啊. Problem A: 题目大意:给你n个数字,让你叠成n堆,每个数字上面的数的个数不能超过这个数,如 3 上面最多放三个数字 问你,最少能放几堆. ...
- 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) B. Fox and Minimal path 构造
B. Fox and Minimal path 题目连接: http://codeforces.com/contest/388/problem/B Description Fox Ciel wants ...
- 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. 1) 388B Fox and Minimal path
链接:http://codeforces.com/problemset/problem/388/B [题意] 给出一个整数K,构造出刚好含有K条从1到2的最短路的图. [分析] 由于是要自己构造图,当 ...
- Codeforces Round #228 (Div. 2)
做codeforces以来题目最水的一次 A题: Fox and Number Game 题意:就是用一堆数字来回减,直到减到最小值为止,再把所有最小值加,求这个值 sol: 简单数论题目,直接求所有 ...
- 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> ...
随机推荐
- Hadoop1.2.1 全集群3节点安装-rpm安装
1.在三个节点上安装JDK RPM 2.在三个节点上安装HADOOP-1.2.1 RPM rpm方式安装和gz解压方式安装后的目录结构有些不同.安装好之后无需设置HADOOP_HOME环境变量 [ro ...
- MySql指令集
http://blog.csdn.net/cl05300629/article/details/9464007
- 《TCP/IP详解卷1:协议》第2章 链路层-读书笔记
章节回顾: <TCP/IP详解卷1:协议>第1章 概述-读书笔记 <TCP/IP详解卷1:协议>第2章 链路层-读书笔记 <TCP/IP详解卷1:协议>第3章 IP ...
- 网页版电子表格控件tmlxSpreadsheet免费下载地址
tmlxSpreadsheet 是一个由JavaScript 和 PHP 写成的电子表格控件(包含WP插件, Joomla插件等等).. 程序员可以容易的添加一个类似Excel功能的,可编辑的表格功能 ...
- jquery甘特图免费下载
Silverlight Gantt甘特图是一款非常丰富,可定制,轻量级和高性能的控件. 项目甘特图: 可视化层次的任务列表. 可移动和拖拽调整条形图 可视化时间编辑器 编辑任务依赖关系 调整任务进度条 ...
- 命令参数解析库JCommonder
1.JCommander 是一个非常小的Java 类库,用来解析命令行参数. 2.参数类型:可以是任意类型,但我使用的只有 List,String. @Parameter(name="-s& ...
- 【技术无关】GPS转北斗卫星定位 系统调研
前言 陆地交通运输是当前GPS卫星定位系统最大的应用领域,我省自08年实施卫星定位系统建设以来,在车辆监控和调度方面发挥了突出的作用:主要功能包括车辆跟踪.线路规划和导航.信息查询.交通指挥.紧急援助 ...
- sublime开发php必备工具集合(mac)
sublime开发php必备工具集合(Mac) 相关链接:http://benmatselby.github.io/sublime-phpcs/ 目标: 直接在sublime中运行php代码 按PSR ...
- 第三个Sprint冲刺第七天
讨论地点:宿舍 讨论成员:邵家文.李新.朱浩龙.陈俊金 讨论问题:做最后的工作
- 网络编程-socket
本节内容: 一:TCP/IP:Transmission Control Protocol/Internet Protocol 传输控制协议/因特网互联协议.即通讯协议.是主机接入互联网以及互联网中两台 ...