POJ - 2183 Bovine Math Geniuses
“模拟“题,运用哈希,不断地按照一定运算规律对一个结果进行计算,如果重复出现就停止并且输出该数。注意到仔细看题,这种题一定要细心!
POJ - 2183 Bovine Math Geniuses
Time Limit: 1000MS |
Memory Limit: 65536KB |
64bit IO Format: %I64d & %I64u |
Description
Farmer John loves to help the cows further their mathematical skills. He has promised them Hay-flavored ice cream if they can solve various mathematical problems.
He said to Bessie, "Choose a six digit integer, and tell me what it is. Then extract the middle four digits. Square them and discard digits at the top until you have another number six digits or shorter. Tell me the result."
Bessie, a mathematical genius in disguise, chose the six digit number 655554. "Moo: 6 5 5 5 5 4", she said. She then extracted the middle four digits: 5555 and squared them: 30858025. She kept only the bottom six digits: 858025. "Moo: 8 5 8 0 2 5", she replied to FJ.
FJ nodded wisely, acknowledging Bessie's prowess in arithmetic. "Now keep doing that until you encounter a number that repeats a number already seen," he requested.
Bessie decided she'd better create a table to keep everything straight:
Middle Middle Shrunk to
Num 4 digits square 6 or fewer
655554 5555 30858025 858025
858025 5802 33663204 663204
663204 6320 39942400 942400
942400 4240 17977600 977600
977600 7760 60217600 217600 <-+
217600 1760 3097600 97600 |
97600 9760 95257600 257600 |
257600 5760 33177600 177600 |
177600 7760 60217600 217600 --+
Bessie showed her table to FJ who smiled and produced a big dish of delicious hay ice cream. "That's right, Bessie," he praised. "The chain repeats in a loop of four numbers, of which the first encountered was 217600. The loop was detected after nine iterations."
Help the other cows win ice cream treats. Given a six digit number, calculate the total number of iterations to detect a loop, the first looping number encountered, and also the length of the loop.
FJ wondered if Bessie knew all the tricks. He had made a table to help her, but she never asked:
Middle Middle Shrunk to
Num 4 digits square 6 or fewer
200023 0002 4 4
4 0 0 0
0 0 0 0 [a self-loop]
whose results would be: three iterations to detect a loop, looping on 0, and a length of loop equal to 1.
Remember: Your program can use no more than 16MB of memory.
Input
* Line 1: A single six digit integer that is the start of the sequence testing.
Output
* Line 1: Three space-separated integers: the first number of a loop, the length of the loop, and the minimum number of iterations to detect the loop.
Sample Input
655554
Sample Output
217600 4 9
Source
#include<stdio.h>
#include<string.h>
#include<math.h>
#include<stdlib.h> int hash[];
char str[];
int main()
{
int n = , t = /*, len = 0*/, cnt = ;
while(~scanf("%d", &n)) {
memset(hash, , sizeof(hash));
t = n;
cnt = ;
while(true) {
if(hash[t]) {
printf("%d %d %d\n", t, cnt - hash[t] + , cnt);
break;
}
cnt++;
hash[t] = cnt;
t /= ;
t %= ;
t = t * t;
t %= ; }
}
return ;
}
POJ - 2183 Bovine Math Geniuses的更多相关文章
- POJ 3047 Bovine Birthday 日期定周求 泽勒公式
标题来源:POJ 3047 Bovine Birthday 意甲冠军:.. . 思考:式 适合于1582年(中国明朝万历十年)10月15日之后的情形 公式 w = y + y/4 + c/4 - 2* ...
- POJ 2389 Bull Math(水~Java -大数相乘)
题目链接:http://poj.org/problem?id=2389 题目大意: 大数相乘. 解题思路: java BigInteger类解决 o.0 AC Code: import java.ma ...
- poj 2389.Bull Math 解题报告
题目链接:http://poj.org/problem?id=2389 题目意思:就是大整数乘法. 题目中说每个整数不超过 40 位,是错的!!!要开大点,这里我开到100. 其实大整数乘法还是第一次 ...
- 数据结构——POJ 1686 Lazy Math Instructor 栈的应用
Description A math instructor is too lazy to grade a question in the exam papers in which students a ...
- POJ 1686 Lazy Math Instructor (模似题+栈的运用) 各种坑
Problem Description A math instructor is too lazy to grade a question in the exam papers in which st ...
- poj 1684 Lazy Math Instructor(字符串)
题目链接:http://poj.org/problem?id=1686 思路分析:该问题为表达式求值问题,对于字母使用浮点数替换即可,因为输入中的数字只能是单个digit. 代码如下: #includ ...
- POJ 1686 Lazy Math Instructor(栈)
原题目网址:http://poj.org/problem?id=1686 题目中文翻译: Description 数学教师懒得在考卷中给一个问题评分,因为这个问题中,学生会为所问的问题提出一个复杂的公 ...
- POJ 2183
模拟题 #include <iostream> #include <cstdio> #include <algorithm> using namespace std ...
- POJ 题目分类(转载)
Log 2016-3-21 网上找的POJ分类,来源已经不清楚了.百度能百度到一大把.贴一份在博客上,鞭策自己刷题,不能偷懒!! 初期: 一.基本算法: (1)枚举. (poj1753,poj2965 ...
随机推荐
- Git初使用
今天开始初次使用Git,Git作为一个使用广泛的分布式版本控制系统,我们有必要熟悉掌握. 这次主要是实现将本地上的“Hello World”的完整的项目文件提交到github上新建的代码库,主要过程如 ...
- [LintCode] House Robber 打家劫舍
You are a professional robber planning to rob houses along a street. Each house has a certain amount ...
- 普通工程转为mvn工程
不同类型的工程可以转为mvn工程, 只需要一个插件 You may need to install m2e-eclipse plugin in order to have this simple ut ...
- html中标签的含义及作用
链接:http://www.w3chtml.com/html/tag/div.html
- 对Oracle10g rac srvctl命令使用理解
srvctl命令是RAC维护中最常用到的命令,也最为复杂,使用这个命令可以操作CRS上的Database,Instance,ASM,Service.Listener和Node Application资 ...
- bzoj3991: [SDOI2015]寻宝游戏--DFS序+LCA+set动态维护
之前貌似在hdu还是poj上写过这道题. #include<stdio.h> #include<string.h> #include<algorithm> #inc ...
- zju(5)LED控制实验
1.实验目的 1.学习和掌握如何将一个驱动程序添加到Kconfig,编译到内核. 二.实验内容 1.编写EduKit-IV试验箱Linux操作系统下LED灯的驱动: 2.编写EduKit-IV试验箱L ...
- linux中添加ftp用户,并设置相应的权限
在linux中添加ftp用户,并设置相应的权限,操作步骤如下: 1.环境:ftp为vsftp.被限制用户名为test.被限制路径为/home/test 2.建用户:在root用户下: useradd ...
- BizTalk开发系列(十三) Schema设计之值约束
XML Schema 的作用是定义 XML 文档的合法构建模块.在开发过程中有时需要对XML文档做精确的约束.以保证XMl数据的准确性. 今天我们以一个班级Sample来讲探讨一下如何在开发BizTa ...
- 关于JAVA中的String的使用与连接(转)
JAVA中的String连接性能 Java中的String是一个非常特殊的类,使它特殊的一个主要原因是:String是不可变的(immutable). String的不可变性是Ja ...