# Codeforces 388B Fox and Minimal path（构造最短路条数为N的图）

507人阅读 评论(0)

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

Examples
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，构造一个最短路径数为N的图，以邻接矩阵的形式输出。

开始想的是因数分解，不过这样对于非常大的质数会超过1000个点的限制。后来在吃饭的时候灵光一闪想到的正确的方法(￣▽￣)

任何一个数都可以用2进制表示，而且1e9也才二十几位而已。所以我们就可以把N拆成2^a+2^b+2^c…。于是我们就可以构造一条主链，表示主要路径，一条辅链，表示2^x。如图

因为主链已经表示了1，所以我们要把N-=1，然后对N进行转化为二进制数，如果第i位为1就把主链第i+1层和辅链第i层的一个节点连接，相当于增加了2^I条路径。这样就可以得到满足题目要求的图了。

AC代码：

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int maxn=1000+3;
int N,size;//需要路径数、图的层数
bool G[maxn][maxn];//邻接矩阵

{
G[a][b]=G[b][a]=true;
}

int main()
{
scanf("%d",&N);
--N;
int tmp=N;
while(tmp>0)
{
++size;
tmp/=2;
}
if(size==0)//N==1时特判
{
printf("2\nNY\nYN\n");
return 0;
}
//构造主链和辅链
for(int i=1;i<size;++i)
{
}
int now=0;
while(N>0)
{
++now;
if(N&1)//增加2^now条路径
{
if(now==size)
}
N/=2;
}
size=size*3+2;
printf("%d\n",size);
for(int i=1;i<=size;++i)//输出邻接矩阵
{
for(int j=1;j<=size;++j)
putchar(G[i][j]?'Y':'N');
putchar('\n');
}

return 0;
}

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：52338次
• 积分：2243
• 等级：
• 排名：第19375名
• 原创：176篇
• 转载：2篇
• 译文：0篇
• 评论：32条
文章分类
阅读排行
最新评论