B. Fox and Minimal path

题目连接:

http://codeforces.com/contest/388/problem/B

Description

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

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 Input

2

Sample Output

4

NNYY

NNYY

YYNN

YYNN

Hint

题意

你需要构造一个图,使得1到2的最短路恰好有k条

题解:

拆成二进制,比如9 = 20+23

二进制的最短路就非常好构造,就2*2*2这样去构造就好了

然而这样裸的构造的话,点数可能会超过1000个点

所以你再压缩一下点的个数,复用一下就好了

代码

#include<bits/stdc++.h>
using namespace std;
const int maxn = 3200;
int mp[maxn][maxn];
int k,bit[32],tot=3,max_len;
int getid()
{
return tot++;
}
int main()
{
scanf("%d",&k);
int id1=1,id2=1,id3=getid();
while(k)
{
if(k%2)mp[id1][id3]=1,mp[id2][id3]=1;
k/=2;if(k==0)break;
int id4=getid(),id5=getid(),id6=getid();
mp[id1][id4]=1,mp[id1][id5]=1;
mp[id2][id4]=1,mp[id2][id5]=1;
mp[id3][id6]=1;
id1=id4,id2=id5,id3=id6;
}
mp[id3][2]=1;
int Max = getid()-1;
printf("%d\n",Max);
for(int i=1;i<=Max;i++,cout<<endl)for(int j=1;j<=Max;j++)
if(mp[i][j]||mp[j][i])printf("Y");else printf("N");
}

Codeforces Round #228 (Div. 1) B. Fox and Minimal path 构造的更多相关文章

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

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

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

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

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

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

  5. Codeforces Round #228 (Div. 2) C. Fox and Box Accumulation

    C. Fox and Box Accumulation time limit per test 1 second memory limit per test 256 megabytes input s ...

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

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

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

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

  8. Codeforces Round #228 (Div. 2)

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

  9. Codeforces Round #228 (Div. 1) B

    B. Fox and Minimal path time limit per test 1 second memory limit per test 256 megabytes input stand ...

随机推荐

  1. mipi 调试经验【转】

    转自:http://blog.csdn.net/g_salamander/article/details/9163455 版权声明:本文为博主原创文章,未经博主允许不得转载. 以下是最近几个月在调试 ...

  2. Linux下如何在进程中获取虚拟地址对应的物理地址【转】

    转自:http://blog.csdn.net/kongkongkkk/article/details/74366200 如果让你编写一个程序,来获取虚拟地址对应的物理地址..你会试着操作MMU吗.. ...

  3. 【驱动】USB驱动·入门【转】

    转自:http://www.cnblogs.com/lcw/p/3159371.html Preface USB是目前最流行的系统总线之一.随着计算机周围硬件的不断扩展,各种设备使用不同的总线接口,导 ...

  4. 金蝶K3,名称或代码在系统中已被使用,由于数据移动,未能继续以NOLOCK方式扫描

    使用金蝶K3时出现:名称或代码在系统中已被使用:错误代码:3604(E14H)source:Microsoft OLE DB provider for SQL SERVERDetail:由于数据移动, ...

  5. C++中关于位域的概念

    原文来自于http://topic.csdn.net/t/20060801/11/4918904.html中的回复 位域 有些信息在存储时,并不需要占用一个完整的字节,   而只需占几个或一个二进制位 ...

  6. python操作mysql(pymysql + sqlalchemy)

    pymysql pymsql是Python中操作MySQL的模块,其使用方法和MySQLdb几乎相同. 下载安装 pip3 install pymysql 使用操作 1.执行sql #!/usr/bi ...

  7. css-css背景

    CSS 允许应用纯色作为背景,也允许使用背景图像创建相当复杂的效果 一:背景色background-color 属性 p {background-color: gray;} 二:背景图像 backgr ...

  8. No.7 selenium学习之路之Alert弹窗

    Alert弹窗 弹窗是用工具选不到的~ 切换到alert driver.switch_to_alert() 新的语法:driver.switch_to.alert 注:新的语法不需要后面加括号 打印a ...

  9. oracle一些笔记

    1.字符串类型字段 区分大小写 where table_name = 'MIDDLE' 2.execute immediate '' bulk collect into v_xxx_tab 3.列别名 ...

  10. CVE-2010-0249 极光

    传说中的极光漏洞 Microsoft Internet Explorer非法事件操作内存破坏漏洞 Microsoft Internet Explorer是微软Windows操作系统中默认捆绑的WEB浏 ...