The ant Welly now dedicates himself to urban infrastructure. He came to the kingdom of numbers and solicited an audience with the king. He recounted how he had built a happy path in the kingdom of happiness. The king affirmed Welly’s talent and hoped that this talent can help him find the best infinite fraction path before the anniversary. 
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的更多相关文章

  1. 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 ...

  2. 2017 ACM/ICPC 沈阳 I题 Little Boxes

    Little boxes on the hillside. Little boxes made of ticky-tacky. Little boxes. Little boxes. Little b ...

  3. 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 ...

  4. 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 ...

  5. 2017沈阳区域赛Infinite Fraction Path(BFS + 剪枝)

    Infinite Fraction Path Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java ...

  6. hdu6223 Infinite Fraction Path 2017沈阳区域赛G题 bfs加剪枝(好题)

    题目传送门 题目大意:给出n座城市,每个城市都有一个0到9的val,城市的编号是从0到n-1,从i位置出发,只能走到(i*i+1)%n这个位置,从任意起点开始,每走一步都会得到一个数字,走n-1步,会 ...

  7. 2017 ACM/ICPC Asia Regional Qingdao Online

    Apple Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submi ...

  8. 2017 ACM/ICPC Asia Regional Shenyang Online spfa+最长路

    transaction transaction transaction Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 132768/1 ...

  9. 2017 ACM ICPC Asia Regional - Daejeon

    2017 ACM ICPC Asia Regional - Daejeon Problem A Broadcast Stations 题目描述:给出一棵树,每一个点有一个辐射距离\(p_i\)(待确定 ...

随机推荐

  1. PHP---无限极分类数组处理

    $array = array(    0=>array('id'=>1,'uid'=>0,'menuname'=>'菜单1','url'=>0,'addtime'=> ...

  2. CSS复合选择器是什么?复合选择器是如何工作

    复合选择器介绍 复合选择器其实很好理解,说白了就跟我们生活中的有血缘关系家庭成员一样,通过标签或者class属性或id属性,去找对应的有血缘关系的某个选择器,具体的大家往下看哦. 如果是初学者对基本的 ...

  3. suseoj 1207: 大整数的乘法(java, 大数相乘, C/C++, 大数相乘)

    1207: 大整数的乘法 时间限制: 1 Sec  内存限制: 128 MB提交: 7  解决: 2[提交][状态][讨论版][命题人:liyuansong] 题目描述 求两个不超过200位的非负整数 ...

  4. hdu 1325 Is It A Tree? (树、node = edge + 1、入度 <= 1、空树)

    Is It A Tree?Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  5. nyoj 198-数数 (python, string[::-1])

    198-数数 内存限制:64MB 时间限制:3000ms 特判: No 通过数:16 提交数:25 难度:2 题目描述: 我们平时数数都是喜欢从左向右数的,但是我们的小白同学最近听说德国人数数和我们有 ...

  6. 一文教你快速读懂MQTT网关

    MQTT是一种发布(publish)/订阅(subscribe)协议,MQTT协议采用发布/订阅模式,所有的物联网终端都通过TCP连接到云端,云端通过主题的方式管理各个设备关注的通讯内容,负责将设备与 ...

  7. 深入理解Kafka必知必会(2)

    Kafka目前有哪些内部topic,它们都有什么特征?各自的作用又是什么? __consumer_offsets:作用是保存 Kafka 消费者的位移信息 __transaction_state:用来 ...

  8. 力扣(LeetCode)二进制求和 个人题解

    给定两个二进制字符串,返回他们的和(用二进制表示). 输入为非空字符串且只包含数字 1 和 0. 示例 1: 输入: a = "11", b = "1" 输出: ...

  9. django_1:配置文件

    工程下: settings.py(建议设置成如下) DATABASES                                   #数据库配置 DEBUG = True           ...

  10. Java基础知识总结之多线程

    1.基本概念 进程是程序的一次动态执行过程,是系统进行资源分配和调度运行的基本单位. 线程是进程的一个实体,它是比进程更小的的能够独立运行的基本单位.在引入线程的操作系统中,通常都是把进程作为分配资源 ...