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> ...
随机推荐
- Java in a Nutshell学习笔记
1, bytecode永远是大段 2,其它语言要在java里运行,要么实现类似于javac的编译器,把该语言解释成为class文件.要么,直接重新实现JVM,直接解释该语言3,Java和C++区别: ...
- 字符串中带有emoji表情处理
1:先删除字符然后解析当前字符再显示 edit.addTextChangedListener(new TextWatcher() { @Override public void beforeTextC ...
- Android请求网络权限
1,新建一个项目,在AndroidManiifest中添加 <uses-permission android:name="android.permission.INTERNET&quo ...
- C++ 构造与析构函数
这两个概念并不对等,构造函数可以完全控制成员构造过程(通过初始化列表),析构函数准确说应该叫析构之前被调用的函数 一般不应该手动调用析构函数:栈区对象会自动析构,堆区也是在delete的时候析构 有一 ...
- 模仿MFC封装Windows API
.... 最后添加了两个按钮,分别处理每个按钮的单击事件时,走了弯路,本来想的是在QButton中重写OnLButtonDown方法,但是,无法区分是那个按钮.参考这篇文章: http://zhida ...
- 详解<a>标签
相信对于学前端的人而言<a>标签并不陌生,我们先把他所有的属性列出来 一.主要属性 一般来说,a标签有着四种状态,分别是link,hover,active,visited,接下来我会仔细讲 ...
- Jquery和Javascript 实际项目中写法基础 (1)
一.JS 是什么,jquery 是什么 就不说明了,直接说一般使用是怎么样的 <!DOCTYPE html> <html> <head> <meta cha ...
- ActiveXObject函数详解
什么是 ActiveX 控件? ActiveX 控件广泛用于 Internet.它们可以通过提供视频.动画内容等来增加浏览的乐趣.不过,这些程序可能出问题或者向您提供不需要的内容.在某些情况下,这些程 ...
- iOS-Git 所有资料
查看git所有资料参考这个网站:http://git.oschina.net/progit/
- 正向代理与反向代理的区别【Nginx读书笔记】
正向代理的概念 正向代理,也就是传说中的代理,他的工作原理就像一个跳板, 简单的说, 我是一个用户,我访问不了某网站,但是我能访问一个代理服务器 这个代理服务器呢,他能访问那个我不能访问的网站 于是我 ...