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

原创 2016年08月31日 16:39:44
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;
}


版权声明:转载请注明出处:http://blog.csdn.net/yasola,谢谢

相关文章推荐

CodeForces 388B Fox and Minimal path

做了4天ICM  感觉脑子都坏掉了…  CF落下了2场的题  赶紧追 >. 题意: 最多用1000个点  画出一幅图  要求满足1节点到2节点的最短路条数为k  输出这幅图的邻接矩阵 思路: ...

Codeforces Round #228 Div1 B Fox and Minimal path

这道题的意思大致就是给你一个数

Codeforces 389D Fox and Minimal path【构造+二进制思维】好题!

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

CodeForces 388A Fox and Box Accumulation(贪心)

Description Fox Ciel has n boxes in her room. They have the same size and weight, but they might ha...

Codeforces_388C_Fox and Card Game(博弈、贪心、排序)

Fox and Card Game time limit per test 1 second memory limit per test 256 megabytes ...

CodeForces 388C Fox and Card Game 解题报告

D - Fox and Card Game Fox Ciel is playing a card game with her friend Fox Jiro. There are n piles ...

CodeForces 388C Fox and Card Game

题意: n堆卡片  每堆si张  两个人博弈拿卡片  他们都采用最优方式  先手只能拿某堆的顶  后手只能拿某堆的底  问最后比分 思路: 纯考脑子题  想通无难度… 想象两种情况: ...

CodeForces 388A Fox and Box Accumulation (模拟)

CodeForces 388A Fox and Box Accumulation (模拟)

Codeforces Round #290 (Div. 2) - C. Fox And Names(最短路)

题目: http://codeforces.com/problemset/problem/545/E 题意: n个点m条边的无向图,求出从1->n的最短路,使得其路径上标记为0的路径最少,除了这条最...

Codeforces 510B:Fox And Two Dots(DFS变形+技巧)

B. Fox And Two Dots time limit per test 2 seconds memory limit per test 256 megabytes ...
  • zugofn
  • zugofn
  • 2016年08月14日 23:58
  • 136
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:Codeforces 388B Fox and Minimal path(构造最短路条数为N的图)
举报原因:
原因补充:

(最多只允许输入30个字)