Codeforces 389D Fox and Minimal path "construction + binary thinking" good question!

Source: Internet
Author: User
Tags printf time limit

Transferred from: http://blog.csdn.net/mengxiang000000/article/details/53072307
B. Fox and Minimal Path
Time limit per test
1 second
Memory limit per test
Megabytes
Input
Standard input
Output
Standard output

Fox Ciel wants to write a task for a programming contest. The task is: given a simple undirected graph with n vertexes. Each IT edge has a unit length. You should calculate the number of shortest paths between vertex 1 and vertex 2. "

Same with some writers, she wants to make a example with some certain output:for example, her birthday or the number of Her boyfriend. Can-You-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 is ' N ' or ' Y '. If gij is ' Y ', then graph G has a edge connecting vertex I and Vertex J. Consider the graph vertexes is numbered from 1 t o N.

The graph must is undirected and simple:gii = ' N ' and gij = Gji must hold. And there must is 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 is 2 shortest paths:1-3-2 and 1-4-2.

In second example, there is 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.

Main topic:

Let you construct a diagram, containing n points, and ensure that the shortest number of bars from 1 to 2 is K, to find a feasible solution.

n must be less than or equal to 1000.

Ideas:

1, the beginning of the idea is to give the number of K-factor decomposition, but thought that if K is a large prime, then the result is not feasible, because the number of points of the problem is less than or equal to 1000.

2, and then consider, no matter how large a number, can be represented by a binary number, then we consider the input in the K first into binary number.

Like 21:10,101.

Then we consider to make it into a 2^0+2^2+2^4, corresponding to our very easy to get out 2^4 diagram:

Then we 2^0, directly from 3 to a shunt, as long as guaranteed from 1-3-shunt-2 of the length and from 1 to 2 the shortest possible length of the same can be:

So 2^2, we separated from 9 a tap (because there are 1 ways to go from 9 to 4, Then from 9 to 20, then more than 4 ways to go):
!

#include <cstdio> #include <cstring> using namespace std;
int f[1005][1005];
    int main () {int k;
    scanf ("%d", &k);
    int w[39],cnt=0;
        while (k) {w[cnt++]=k%2;
    k/=2;
    } f[1][3]=f[3][1]=1;
    int now=3;
        for (int i=1; i<cnt; i++) {f[now][now+1]=f[now+1][now]=1;
        F[now][now+2]=f[now+2][now]=1;
        F[now+1][now+3]=f[now+3][now+1]=1;
        F[now+2][now+3]=f[now+3][now+2]=1;
    now+=3;
    } int tmp=now+1;
    F[now][2]=f[2][now]=1;
        for (int i=0; i< (cnt-2) * * i++) {f[tmp][tmp+1]=f[tmp+1][tmp]=1;
    tmp++;
    } f[tmp][now]=f[now][tmp]=1;
    int pos=now+1;
    now=tmp;
    int t=1;
        for (int i=0; i<cnt; i++) {if (w[i]==1) {f[t*3][pos]=f[pos][t*3]=1;
        } t++;
    pos+=2;
    } printf ("%d\n", now); for (int i=1, i<=now; i++) {for (int j=1; j<=now; J + +) if (F[i][j]) printf ("Y");
        else printf ("N");
    Puts ("");
} return 0;
 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.