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. QuartZ.net 常用配置说明

    配置文件说明 app.config中的quartz部分 <quartz> <!-- configure Thread Pool--> <addkey="quar ...

  2. 分享原创powershell脚本小工具ctracert.ps1

    ----------[脚本介绍]----------- 脚本名称:ctracert.ps1软件名称:灰主牛 跟踪路由 归属地版 V1.0脚本作用:1跟踪路由.2显示归属地.(注意不带显示时间功能)脚本 ...

  3. 安卓手机上的python运行环境-qpython

    qpython是一个能让安卓手机运行和编写python的APP,到网上下载APK安装或者在GOOGLE PLAY搜索安装即可. 安装之后你可以你手机跑自己的python程序. qpython有两个大版 ...

  4. Android 自动生成表格

    Layout.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:a ...

  5. sql datetime操作

    Sql Server中的日期与时间函数 SQL中的时间函数非常有用,特别是在我们进行初始赋值.复杂查询的时候,就显得特别方便. 1.获得系统当前时间 select getdate()  2.DateN ...

  6. Windows Server 2008(R2)配置apache+php+mysql环境问题事项

    服务器环境:Windows 2008 R2 64位.apache,mysql,php都是32位. 1. 80端口的外网访问问题 表现:80端口本地可以访问,外网不能访问,换了8080端口也是一样,检查 ...

  7. Java语言的多态性

    用简单的话来描述Java:编译类型与运行类型不一致的时候就会出现多态! 下面一段代码可以用来描述Java多态 class BaseClass{ public String flag="父类的 ...

  8. C++ UFunction({FLAG}) 宏 FLAG 解释笔记

    1.BluePrintCallable  --蓝图可调用 但不可编辑 2.BlueprintImplementableEvent --函数体必须实现与Blueprint 但函数名必须生成与C++ .h ...

  9. 复健小CM

    系统 : Windows xp 程序 : Keygenme # 2 程序下载地址 :http://pan.baidu.com/s/1qYIk2HQ 要求 : 注册机编写 使用工具 : OD 可在“PE ...

  10. JavaScript中this和$(this)之间的区别以及extend的使用

    jQuery中this和$(this)之间的区别: this返回的是当前对象的html对象,而$(this)返回的是当前对象的jQuery对象 举个正确的Demo实例: $("#textbo ...