Codeforces389D(SummerTrainingDay01-J)
D. Fox and Minimal path
Fox Ciel wants to write a task for a programming contest. The task is: "You are given a simple undirected graph with nvertexes. 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.
题意:求正好有k条最短路的图的邻接矩阵。
思路:二进制思想构图。
//2017-10-15
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; const int N = ;
bool a[N][N]; int main()
{
long long k;
while(cin>>k){
int n = ;
while((<<n) <= k)
n++;
if(n)n--;
int step = n*+;
int num = (<<n);
memset(a, , sizeof(a));
n = *n+;
a[][] = ;
for(int i = ; i <= n; i++){
if(i% == )a[i][i-] = a[i][i-] = ;
else if((i-)% == )a[i][i-] = ;
else if((i-)% == )a[i][i-] = ;
}
a[n][] = ;
for(int i = n+; i < n+step; i++)
a[i][i+] = ;
a[n+step-][] = ;
int tmp = k - num, ptr = ;
while(tmp){
int fg = (&tmp);
tmp>>=;
if(fg)a[(ptr+)/*][ptr+n] = ;
ptr+=;
}
if(k != num)n+=(step-);
cout<<n<<endl;
for(int i = ; i <= n; i++){
for(int j = ; j <= n; j++)
if(a[i][j] || a[j][i])
cout<<'Y';
else cout<<'N';
cout<<endl;
}
} return ;
}
Codeforces389D(SummerTrainingDay01-J)的更多相关文章
- 【Java并发编程实战】-----“J.U.C”:CAS操作
CAS,即Compare and Swap,中文翻译为"比较并交换". 对于JUC包中,CAS理论是实现整个java并发包的基石.从整体来看,concurrent包的实现示意图如下 ...
- 【Java并发编程实战】-----“J.U.C”:Exchanger
前面介绍了三个同步辅助类:CyclicBarrier.Barrier.Phaser,这篇博客介绍最后一个:Exchanger.JDK API是这样介绍的:可以在对中对元素进行配对和交换的线程的同步点. ...
- 【Java并发编程实战】-----“J.U.C”:CountDownlatch
上篇博文([Java并发编程实战]-----"J.U.C":CyclicBarrier)LZ介绍了CyclicBarrier.CyclicBarrier所描述的是"允许一 ...
- 【Java并发编程实战】-----“J.U.C”:CyclicBarrier
在上篇博客([Java并发编程实战]-----"J.U.C":Semaphore)中,LZ介绍了Semaphore,下面LZ介绍CyclicBarrier.在JDK API中是这么 ...
- 【Java并发编程实战】-----“J.U.C”:ReentrantReadWriteLock
ReentrantLock实现了标准的互斥操作,也就是说在某一时刻只有有一个线程持有锁.ReentrantLock采用这种独占的保守锁直接,在一定程度上减低了吞吐量.在这种情况下任何的"读/ ...
- JAVA并发编程J.U.C学习总结
前言 学习了一段时间J.U.C,打算做个小结,个人感觉总结还是非常重要,要不然总感觉知识点零零散散的. 有错误也欢迎指正,大家共同进步: 另外,转载请注明链接,写篇文章不容易啊,http://www. ...
- Android Studio解决未识别Java文件(出现红J)问题
1.问题:java文件出现了红J的问题,正常情况下应该是显示蓝色的C标识. 2.解决方案:切换到project视图下,找到app这个module里的build.gradle,在android结构里插入 ...
- //给定N个整数序列{A1,A2,A3...An},求函数f(i,j)=(k=i~j)Ak的求和
//给定N个整数序列{A1,A2,A3...An},求函数f(i,j)=(k=i~j)Ak的求和 # include<stdio.h> void main() { ,sum1; ]={,- ...
- 面试题:给定数组a,找到最大的j-i, 使a[j]>a[i]
第一种方法: 用两重循环对每对点都试一下,然后取最大值即可,时间复杂度为O(n2) #include <iostream> #include <algorithm> using ...
- 关于i和j
算法课无聊随手写了段c代码,发现了个问题,就要下课了,先记一下 for(int i = 0; i < 100; i ++) for(int j = 0; j < 100000; j ++) ...
随机推荐
- 谈谈npm依赖管理
引言 现在的前端开发几乎都离不开nodejs的包管理器npm,比如前端在搭建本地开发服务以及打包编译前端代码等都会用到.在前端开发过程中,经常用到npm install来安装所需的依赖,至于其中的技术 ...
- 原来你离BAT只有一步之遥
ladies and乡亲们 喜迎全民嗨购双11 i春秋准备搞一波大优惠 优惠力度有多大 跨店凑单满400-50? 指定商品199减100? 史无钜惠 不凑单 不指定 一次直降9000元 原价:2580 ...
- 【sql注入】浅谈JSP安全开发之SQL注入
[sql注入]浅谈JSP安全开发之SQL注入 本文转自:i春秋社区 前言不管是用什么语言编写WEB应用程序,他们都或多或少有一些地方存在漏洞.如果你想知道漏洞的运行原理,和防御方案,那么请看完本篇文章 ...
- Vmware12 u盘启动系统
先插U盘,再打开虚拟机 创建一个虚拟机 点击添加硬盘,下一步 先择SCSI,下一步 选择如图所示,有可能是1,也可能是2,这项就是U盘 设置完毕后,打开虚拟机,赶紧按F2,ESC 或者F12,这个是看 ...
- 每天学点SpringCloud(七):路由器和过滤器-Zuul
为什么要使用Zuul 先来看一下下方这个图 假如现在我们具有四个微服务,分别是用户.订单.支付.催收微服务,它们的调用方式分别是使用http.restful.thrift.kafka.这个时候如果我们 ...
- 第82节:Java中的学生管理系统
第82节:Java中的学生管理系统 学生管理系统的删除功能 删除,点击超链接,点击弹出对话框式是否进行删除,如果确定,就删除,超链接执行的是js方法,在js里访问,跳转servlet,,servlet ...
- 微信小程序中的app.js-清除缓存
微信小程序中的app.js 关于小程序app.js生命周期的介绍 App(Object) App() 函数用来注册一个小程序.接受一个 Object 参数,其指定小程序的生命周期回调等. App() ...
- 基于.NET框架的消息通信组件ZMQ资料汇编-总目录
ZMQ是一个比较轻量级的消息通信组件,引用官方的说法: “ZMQ (以下 ZeroMQ 简称 ZMQ)是一个简单好用的传输层,像框架一样的一个 socket library,他使得 Socket 编程 ...
- Ubuntu 16.04 系统无法挂载u盘的问题
Ubuntu系统无法挂载U盘设备,提示错误为:mount:未知文件系统类型“exfat”.这是因为Ubuntu默认情况下是不允许挂载U盘的,想在Ubuntu系统下挂载U盘,就要用下面的方法了. sud ...
- web前端异步数据交互(长连接)
Workers异步任务 开始(注册): divobjx=document.getElementsByTagName("div")[0]; var workdong=new Work ...