2017 ACM/ICPC 沈阳 G题 Infinite Fraction Path
The kingdom has N cities numbered from 0 to N - 1 and you are given an array D[0 ... N - 1] of decimal digits (0 ≤ D[i] ≤ 9, D[i] is an integer). The destination of the only one-way road start from the i-th city is the city labelled (i2i2 + 1)%N.
A path beginning from the i-th city would pass through the cities u1,u2,u3u1,u2,u3, and so on consecutively. The path constructs a real number A[i], called the relevant fraction such that the integer part of it is equal to zero and its fractional part is an infinite decimal fraction with digits D[i], D[u1u1], D[u2u2], and so on.
The best infinite fraction path is the one with the largest relevant fraction
InputThe input contains multiple test cases and the first line provides an integer up to 100 indicating to the total numberof test cases.
For each test case, the first line contains the integer N (1 ≤ N ≤ 150000). The second line contains an array ofdigits D, given without spaces.
The summation of N is smaller than 2000000.
OutputFor each test case, you should output the label of the case first. Then you are to output exactly N characters which are the first N digits of the fractional part of the largest relevant fraction.
Sample Input
4
3
149
5
12345
7
3214567
9
261025520
Sample Output
Case #1: 999
Case #2: 53123
Case #3: 7166666
Case #4: 615015015
题解:
若想数字较大,那么这个数字的最高位越大越好,再是次高位,再次次高…那么我们可以先找出这些点中权值最大的点作为起始点放入队列中,然后一步步做广度优先搜索,依次排除组成数字较小的起始点。最后剩下的那个点就是答案。
但是这个肯定超时,复杂度最大为O(n2)O(n2),仔细想想会发现这个图有很多特点:图由许多单向链和环构成;有的链连接到了其他链的中间;所有链的末尾肯定连接一个环。而超时的原因肯定是许多点都被重复搜索多次了。这里可能有一个点被多个起始点搜索过(链的分支出);一个点被一个起始点一直搜索(环)。
于是朝这个方向优化:
1.同一层(step)里,我们只要那些当前权值最大的点对应的起始点。
2.对于链的分支而言:假如初始点AA搜索到一个已经被点BB搜索过的点,那么初始点BB就不用继续搜索了(想想为什么),BB就可以被移出队列。
参考代码:
#include<bits/stdc++.h>
#define CLR(a,b) memset(a,b,sizeof(a))
using namespace std;
typedef long long ll;
const int inf=0x3f3f3f3f;
const int maxn=;
int Max,vis[maxn],tot;
char a[maxn],ans[maxn];
ll n;
struct Node{
int step;
ll pos;
Node() {}
Node(int step,ll pos):step(step),pos(pos) {}
};
queue<Node >q;
inline void bfs()
{
Node s;
while(!q.empty())
{
s=q.front();q.pop();
if(s.step==n) continue;
if(a[s.pos]==ans[s.step])
{
if(vis[s.pos]==s.step) continue;
vis[s.pos]=s.step;
s.pos= (s.pos * s.pos + ) % n;
s.step++;
if(a[s.pos]>=ans[s.step])
{
ans[s.step]=a[s.pos];
q.push(s);
}
}
}
}
int main()
{
int T;
cin>>T;
for(int cas=;cas<=T;++cas)
{
while(!q.empty()) q.pop();
tot=;
scanf("%lld",&n);
Max=;
scanf("%s",a);
for(int i=; i<n; i++) Max=max(Max,(int)a[i]);
for(int i=; i<n; i++) if(a[i]==Max) q.push(Node(,i));
CLR(ans,-); CLR(vis,-);
ans[]=Max;
bfs();
printf("Case #%d: ",cas);
ans[n+]='\0';
printf("%s\n",ans+);
}
return ;
}
2017 ACM/ICPC 沈阳 G题 Infinite Fraction Path的更多相关文章
- 2017 ACM/ICPC 沈阳 K题 Rabbits
Here N (N ≥ 3) rabbits are playing by the river. They are playing on a number line, each occupying a ...
- 2017 ACM/ICPC 沈阳 I题 Little Boxes
Little boxes on the hillside. Little boxes made of ticky-tacky. Little boxes. Little boxes. Little b ...
- 2017 ACM/ICPC 沈阳 F题 Heron and his triangle
A triangle is a Heron’s triangle if it satisfies that the side lengths of it are consecutive integer ...
- 2017 ACM/ICPC 沈阳 L题 Tree
Consider a un-rooted tree T which is not the biological significance of tree or plant, but a tree as ...
- 2017沈阳区域赛Infinite Fraction Path(BFS + 剪枝)
Infinite Fraction Path Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 262144/262144 K (Java ...
- hdu6223 Infinite Fraction Path 2017沈阳区域赛G题 bfs加剪枝(好题)
题目传送门 题目大意:给出n座城市,每个城市都有一个0到9的val,城市的编号是从0到n-1,从i位置出发,只能走到(i*i+1)%n这个位置,从任意起点开始,每走一步都会得到一个数字,走n-1步,会 ...
- 2017 ACM/ICPC Asia Regional Qingdao Online
Apple Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 65535/32768 K (Java/Others)Total Submi ...
- 2017 ACM/ICPC Asia Regional Shenyang Online spfa+最长路
transaction transaction transaction Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 132768/1 ...
- 2017 ACM ICPC Asia Regional - Daejeon
2017 ACM ICPC Asia Regional - Daejeon Problem A Broadcast Stations 题目描述:给出一棵树,每一个点有一个辐射距离\(p_i\)(待确定 ...
随机推荐
- @resource和@autowired的区别是什么-CSDN论坛-CSDN.NET-中国最大的IT技术社区 - Google Chrome
@Resource的作用相当于@Autowired,只不过@Autowired按byType自动注入,而@Resource默认按 byName自动注入罢了.@Resource有两个属性是比较重要的,分 ...
- Hazel,自动整理文件,让你的 Mac 井井有条
原文地址 https://sspai.com/post/35225 让我们从实际需求出发,看看问题出在哪里,并在此基础上认识和学习使用 Hazel. 电脑随着使用时间的增长,其中的文件也在疯狂的增长, ...
- 【Linux系列】Centos 7安装以及网络配置(一)
目的 本文主要介绍以下两点: 一. 如何在Oracle VM VirtualBox安装centos(已有VirtualBox) 二. 如何在内网里实现虚拟机访问外网.物理主机以及物理主机访问虚拟机 一 ...
- windows系统cmd命令行窗口查看端口占用情况
# 查看所有在用端口 netstat -ano # 查看指定端口 netstat -ano | findstr 8899 # 结束该进程:taskkill /f /t /im javaw.exe:或者 ...
- spark集群搭建(三台虚拟机)——系统环境搭建(1)
!!!该系列使用三台虚拟机搭建一个完整的spark集群,集群环境如下: virtualBox5.2.Ubuntu14.04.securecrt7.3.6_x64英文版(连接虚拟机) jdk1.7.0. ...
- windows下大数据开发环境搭建(1)——Hadoop环境搭建
所需环境 jdk 8 Hadoop下载 http://hadoop.apache.org/releases.html 配置环境变量 HADOOP_HOME: C:\hadoop-2.7.7 Path: ...
- x86汇编分页模式实验 --《ORANGE'S一个操作系统的实现》中 pmtest8.asm解析
序言(废话) : 在看书的过程中发现一开始不是很能理解pmtest8的目的,以及书上说得很抽象..于是在自己阅读过源代码后,将一些自己的心得写在这里. 正文 : 讲解顺序依然按照书上贴代码的顺序来.但 ...
- SpringBoot 源码解析 (八)----- Spring Boot 精髓:事务源码解析
本篇来讲一下SpringBoot是怎么自动开启事务的,我们先来回顾一下以前SSM中是如何使用事务的 SSM使用事务 导入JDBC依赖包 众所周知,凡是需要跟数据库打交道的,基本上都要添加jdbc的依赖 ...
- Elasticsearch从入门到放弃:文档CRUD要牢记
在Elasticsearch中,文档(document)是所有可搜索数据的最小单位.它被序列化成JSON存储在Elasticsearch中.每个文档都会有一个唯一ID,这个ID你可以自己指定或者交给E ...
- 用PHP实现一个简易版文件上传功能(超详细讲解)
1. php简化版的图片上传(没有各种验证) 1 2 3 4 <form action="" enctype="multipart/form-data" ...