关闭

Codeforces 388B Fox and Minimal path(构造最短路条数为N的图)

标签: 算法ACM图论Codeforces构造
381人阅读 评论(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];//邻接矩阵

void add_edge(int a,int b)//加边
{
    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;
    }
    //构造主链和辅链
    add_edge(1,3);
    add_edge(1,4);
    add_edge(1,5);
    add_edge(size*3,2);
    for(int i=1;i<size;++i)
    {
        add_edge(i*3,(i+1)*3);
        add_edge(i*3+1,(i+1)*3+1);
        add_edge(i*3+2,(i+1)*3+2);
        add_edge(i*3+1,(i+1)*3+2);
        add_edge(i*3+2,(i+1)*3+1);
    }
    int now=0;
    while(N>0)
    {
        ++now;
        if(N&1)//增加2^now条路径
        {
            if(now==size)
                add_edge(2,3*now+1);
            else add_edge(3*(now+1),3*now+1);
        }
        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网站的观点或立场
    个人资料
    • 访问:32410次
    • 积分:1880
    • 等级:
    • 排名:千里之外
    • 原创:160篇
    • 转载:2篇
    • 译文:0篇
    • 评论:32条
    文章分类
    最新评论