B. Fox and Minimal path
time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

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?

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 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.

Sample test(s)
input
2
output
4
NNYY
NNYY
YYNN
YYNN
input
9
output
8
NNYYYNNN
NNNNNYYY
YNNNNYYY
YNNNNYYY
YNNNNYYY
NYYYYNNN
NYYYYNNN
NYYYYNNN
input
1
output
2
NY
YN
Note

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的更多相关文章

  1. Codeforces Round #228 (Div. 2) C. Fox and Box Accumulation(贪心)

    题目:http://codeforces.com/contest/389/problem/C 题意:给n个箱子,给n个箱子所能承受的重量,每个箱子的重量为1: 很简单的贪心,比赛的时候没想出来.... ...

  2. Codeforces Round #228 (Div. 1)

    今天学长给我们挂了一套Div.1的题,难受,好难啊. Problem A: 题目大意:给你n个数字,让你叠成n堆,每个数字上面的数的个数不能超过这个数,如 3 上面最多放三个数字 问你,最少能放几堆. ...

  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 ...

  4. 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 ...

  5. 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 ...

  6. Codeforces Round #228 (Div. 1) 388B Fox and Minimal path

    链接:http://codeforces.com/problemset/problem/388/B [题意] 给出一个整数K,构造出刚好含有K条从1到2的最短路的图. [分析] 由于是要自己构造图,当 ...

  7. Codeforces Round #228 (Div. 2)

    做codeforces以来题目最水的一次 A题: Fox and Number Game 题意:就是用一堆数字来回减,直到减到最小值为止,再把所有最小值加,求这个值 sol: 简单数论题目,直接求所有 ...

  8. Codeforces Round #228 (Div. 2) B. Fox and Cross

    #include <iostream> #include <string> #include <vector> #include <algorithm> ...

  9. Codeforces Round #228 (Div. 2) A. Fox and Number Game

    #include <iostream> #include <algorithm> #include <vector> #include <numeric> ...

随机推荐

  1. jsoup 简介

    Java 程序在解析 HTML 文档时,相信大家都接触过 htmlparser 这个开源项目,我曾经在 IBM DW 上发表过两篇关于 htmlparser 的文章,分别是:从HTML中攫取你所需的信 ...

  2. Linux学习 : 裸板调试 之 使用MMU

    MMU(Memory Management Unit,内存管理单元),操作系统通过使用处理器的MMU功能实现以下:1)虚拟内存.有了虚拟内存,可以在处理器上运行比实际物理内存大的应用程序.为了使用虚拟 ...

  3. iOS 模拟器上录制Demo视频

    在App审核中会让用户上传一段简易的视频,那么如何制作改视频呢? 今天无意中在Cocochina 中看到 说利用QuickTime来制作,而QuickTime是操作系统自带的. 哦 My,God ! ...

  4. HBase with MapReduce (SummaryToFile)

    上一篇文章是实现统计hbase单元值出现的个数,并将结果存放到hbase的表中,本文是将结果存放到hdfs上.其中的map实现与前文一直,连接:http://www.cnblogs.com/ljy20 ...

  5. UIkit框架之UIDatePicker

    1.继承链:UIcontrol:UIview:UIResponder:NSOobject 2.和uidatepicker相关联的触发事件是 UIControlEventValueChanged,当使用 ...

  6. Android SnackBar使用方法

    SnackBar是 Android Support Library 22.2.0 里面新增提供的一个控件,类似于Toast的使用 使用方法 Snackbar snackbar = Snackbar.m ...

  7. 黑马程序员:Java编程_集合

    =========== ASP.Net+Android+IOS开发..Net培训.期待与您交流!=========== 面向对象语言对事物的体现都是以对象的形式,所以为了方便对多个对象的操作,就对对象 ...

  8. Spring 和SpringMVC 的父子容器关系

      Spring和SpringMVC作为Bean管理容器和MVC层的默认框架,已被众多WEB应用采用,而实际使用时,由于有了强大的注解功能,很多基于XML的配置方式已经被替代,但是在实际项目中,同时配 ...

  9. maven遇到的问题

    1.Missing artifact net.sf.json-lib:json-lib:jar:2.4:compile pom.xml原内容: <dependency> <group ...

  10. ooize节点的属性控制

    <workflow-app name="[WF-DEF-NAME]" xmlns="uri:oozie:workflow:[version]"> & ...