UVA-11549(floyd判圈算法)
题意:
给一个整数k,每次平方后只能显示结果的前n位,问在这个过程中能得到的最大的数是多少;
思路:
floyd判圈算法;它的正确性建立在这得到的这些数是有限的,所以一定是一个循环,在这个循环的圈里面,一个快一个慢,同时出发最后一定会再次相遇,此时结束;
在这个过程中得到最大值;
AC代码:
#include <bits/stdc++.h>
/*
#include <vector>
#include <iostream>
#include <queue>
#include <cmath>
#include <map>
#include <cstring>
#include <algorithm>
#include <cstdio>
*/
using namespace std;
#define For(i,j,n) for(int i=j;i<=n;i++)
#define Riep(n) for(int i=1;i<=n;i++)
#define Riop(n) for(int i=0;i<n;i++)
#define Rjep(n) for(int j=1;j<=n;j++)
#define Rjop(n) for(int j=0;j<n;j++)
#define mst(ss,b) memset(ss,b,sizeof(ss));
typedef long long LL;
template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<''||CH>'';F= CH=='-',CH=getchar());
for(num=;CH>=''&&CH<='';num=num*+CH-'',CH=getchar());
F && (num=-num);
}
int stk[], tp;
template<class T> inline void print(T p) {
if(!p) { puts(""); return; }
while(p) stk[++ tp] = p%, p/=;
while(tp) putchar(stk[tp--] + '');
putchar('\n');
} const LL mod=1e9+;
const double PI=acos(-1.0);
const LL inf=1e18;
const int N=1e5+;
const int maxn=;
const double eps=1e-; LL n;
LL k;
int a[],cnt;
LL getnext(LL x)
{
cnt=;
x=x*x;
while(x)
{
a[++cnt]=x%;
x/=;
}
LL s=;
for(int i=cnt;i>cnt-n&&i>;i--)
{
s=s*+a[i];
}
return s;
} int main()
{
int t;
read(t);
while(t--)
{
read(n);read(k);
LL ans=k;
LL k1=k,k2=k;
while()
{
k1=getnext(k1);
ans=max(ans,k1);
k2=getnext(k2);
ans=max(ans,k2);
k2=getnext(k2);
ans=max(ans,k2);
if(k1==k2)break;
//cout<<k1<<" "<<k2<<endl;
}
cout<<ans<<"\n"; }
return ;
}
UVA-11549(floyd判圈算法)的更多相关文章
- Floyd判圈算法 UVA 11549 - Calculator Conundrum
题意:给定一个数k,每次计算k的平方,然后截取最高的n位,然后不断重复这两个步骤,问这样可以得到的最大的数是多少? Floyd判圈算法:这个算法用在循环问题中,例如这个题目中,在不断重复中,一定有一个 ...
- UVA 11549 CALCULATOR CONUNDRUM(Floyd判圈算法)
CALCULATOR CONUNDRUM Alice got a hold of an old calculator that can display n digits. She was bore ...
- UVA 11549 Calculator Conundrum (Floyd判圈算法)
题意:有个老式计算器,每次只能记住一个数字的前n位.现在输入一个整数k,然后反复平方,一直做下去,能得到的最大数是多少.例如,n=1,k=6,那么一次显示:6,3,9,1... 思路:这个题一定会出现 ...
- UVa 11549 计算器谜题(Floyd判圈算法)
https://vjudge.net/problem/UVA-11549 题意: 有一个老式计算器,只能显示n位数字,输入一个整数k,然后反复平方,如果溢出的话,计算器会显示结果的最高n位.如果一直这 ...
- SGU 455 Sequence analysis(Cycle detection,floyd判圈算法)
题目链接:http://acm.sgu.ru/problem.php?contest=0&problem=455 Due to the slow 'mod' and 'div' operati ...
- leetcode202(Floyd判圈算法(龟兔赛跑算法))
Write an algorithm to determine if a number is "happy". 写出一个算法确定一个数是不是快乐数. A happy number ...
- Floyd判圈算法
Floyd判圈算法 leetcode 上 编号为202 的happy number 问题,有点意思.happy number 的定义为: A happy number is a number defi ...
- Codeforces Gym 101252D&&floyd判圈算法学习笔记
一句话题意:x0=1,xi+1=(Axi+xi%B)%C,如果x序列中存在最早的两个相同的元素,输出第二次出现的位置,若在2e7内无解则输出-1. 题解:都不到100天就AFO了才来学这floyd判圈 ...
- Floyd 判圈算法
Floyd 判圈算法 摘自维基百科, LeetCode 上 141题 Linked List Cycle 用到这个, 觉得很有意思. 记录一下. 链接: https://zh.wikipedia.or ...
- Floyd判圈算法 Floyd Cycle Detection Algorithm
2018-01-13 20:55:56 Floyd判圈算法(Floyd Cycle Detection Algorithm),又称龟兔赛跑算法(Tortoise and Hare Algorithm) ...
随机推荐
- Laravel 静态资源管理
<link rel="stylesheet" href="{{ asset('bootstrap/css/bootstrap.min.css') }}" ...
- PAT (Advanced Level) 1086. Tree Traversals Again (25)
入栈顺序为先序遍历,出栈顺序为中序遍历. #include<cstdio> #include<cstring> #include<cmath> #include&l ...
- NOIPSB评测机+SB题DAY2
忍者钩爪 题目描述 小 Q 是一名酷爱钩爪的忍者, 最喜欢飞檐走壁的感觉, 有一天小 Q 发现一个练习使用钩 爪的好地方,决定在这里大显身手. 场景的天花板可以被描述为一个无穷长的数轴, 初始小 Q ...
- 数据库设计三范式(3NF)
问:当时你数据库是如何设计的? 答:当时是按照三范式规范设计的: 第一范式: 1:数据库的原子性,即保证数据库表的每一列都不可分割的 第二范式: 1:原子性,即保证数据库表的每一列都不可分割 2:表中 ...
- android 加一个按钮,退出程序
package com.example.yanlei.yl; import android.graphics.Color; import android.support.v7.app.AppCompa ...
- kafka-0.8.1.1总结
文件夹 一. 基础篇 1. 开篇说明 2. 概念说明 3. 配置说明 4. znode分类 5. kafka协议分类 6. Kafka线 ...
- 取汉子拼音首字母的C#方法
/// <summary> /// 获得一个字符串的汉语拼音码 /// </summary> /// <param name="strText"> ...
- insserv: warning: script 'lampp' missing LSB tags and overrides
https://ubuntuforums.org/showthread.php?t=2327011 1.方法一,编辑rc.loacl脚本 Ubuntu开机之后会执行/etc/rc.local文件中的脚 ...
- Linux温习(三)Linux文件和文件夹管理
关于Linux文件夹的几个常见概念 路径 对文件位置信息的描写叙述机制.是指从树型文件夹中的某个文件夹层次到其内某个文件的一条通路.分为相对路径和绝对路径: 工作文件夹 登入系统后.用户始终处于某个文 ...
- [Cypress] install, configure, and script Cypress for JavaScript web applications -- part1
Despite the fact that Cypress is an application that runs natively on your machine, you can install ...