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> ...
随机推荐
- python mysql 更新和插入数据无效
注意,在删除和增加后必须执行conn.commit()才有效,否则操作无效.
- net 的单元测试 初学
1. 都要以一个方法 这样的去测试 2. 利用工具 Install-Package Moq -Version 4.0 (最高的是4.5 4.0适用于自己项目的版本) Moq.dll 进行测试 ...
- MS Sql server 2008 学习笔记
数据库中常用的概念 Sql本身是一个服务器,没有界面,Management Studio 只是一个SQL Server管理工具而已,不是服务器. Sql server 在管理工具下面的服务SQL S ...
- Sql 注意点
1. Set.Select赋值 使用SELECT语句来替代SET命令的主要优点是:可以在一个操作内同时给多个变量赋值.执行下面的SELECT语句,通过SELECT语句赋值的变量就可以用于任何操作了. ...
- Python SocketServer源码分析
1 XXXServer 1.1 BaseSever 提供基础的循环等待请求的处理框架.使用serve_forever启动服务,使用shutdown停止.同时提供了一些可自行扩展的方 ...
- 对编写html代码的几点儿小建议
1.DOCTYPE说明:告诉浏览器要使用哪种规范来解释该文档内容: <!DOCTYPE html PUBLIC "-W3//DTD//XHTML 1.0 Transitional// ...
- Linux的sed命令
一.初识sed 在部署openstack的过程中,会接触到大量的sed命令,比如 # Bind MySQL service to all network interfaces.sed -i 's/12 ...
- BZOJ 4034 BIT & Dfs序
调了恒久突然发现输出优化忘记带负号了.. 就是差分树状数组维护Dfs序即可. #include <iostream> #include <cstring> #include & ...
- windows 下mysql每日定时备份的几种方法
第一种:新建批处理文件 backup.dat,里面输入以下代码: 代码如下 复制代码 net stop mysql xcopy "C:/Program Files/MySQL/MySQL ...
- 用javaScript实现 登陆记住密码功能。
一.先写一个存取 cookie的方法. function getCookie(cookiename) { var result; var mycookie = document.cookie; var ...