G - Infinite Fraction Path HDU - 6223

本文介绍了一种使用BFS算法解决寻找最大无限分数路径的问题,通过从最大数值节点开始,逐步构建路径,最终找到具有最大相关分数的路径。文章详细阐述了算法实现过程,包括节点的比较、剪枝策略以及数据类型的选用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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 (i2 + 1)%N.
A path beginning from the i-th city would pass through the cities u1,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[u1], D[u2], and so on.
The best infinite fraction path is the one with the largest relevant fraction
Input
The 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.
Output
For 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

  • 题意:给你一串数,从其种一个位置i依次向(i*i+1)%n拼接数,使最后拼接的那个最大

  • 解题思路:bfs+加剪枝.刚开始 从最大的作为入口,依次向后比较.下一层取最大的,同一层出现过的扔掉.注意,k*k会爆int(RE).

#include<bits/stdc++.h>
#define mk make_pair
using namespace std;
typedef long long ll;
//bool SUBMIT = 1;
const int inf = 150009;
char s[inf],ans[inf];
int n,vis[inf];
int main()
{
	//if(!SUBMIT)freopen("i.txt","r",stdin);else _;	
	int t;scanf("%d",&t);
	queue<int>q[2];
	for(int i=1;i<=t;i++){
		scanf("%d%s",&n,s);
		int g=0,m=0;
		for(int i=0;i<n;i++){m=max(m,s[i]-'0');vis[i]=-1;}
		for(int i=0;i<n;i++){if(s[i]-'0'==m)q[g&1].push(i);vis[i]=g;}
		ans[0]=m+'0';
		while(g<n){
			queue<int>t;m=0;
			while(!q[g&1].empty()){
				int k=q[g&1].front();q[g&1].pop();
				t.push(k);
				m=max(m,s[(1ll+1ll*k*k)%n]-'0');
			}	
			g++;
			while(!t.empty()){
				int k=t.front();t.pop();
				k=(1ll+1ll*k*k)%n;
				if(m==s[k]-'0'&&vis[k]<g){q[g&1].push(k),vis[k]=g;}
			}
			ans[g]=m+'0';
		}
		ans[n]='\0';
		printf("Case #%d: %s\n",i,ans);
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值