“模拟“题,运用哈希,不断地按照一定运算规律对一个结果进行计算,如果重复出现就停止并且输出该数。注意到仔细看题,这种题一定要细心!

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

USACO 2003 U S Open Orange

[Submit]   [Go Back]   [Status]

 #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的更多相关文章

  1. POJ 3047 Bovine Birthday 日期定周求 泽勒公式

    标题来源:POJ 3047 Bovine Birthday 意甲冠军:.. . 思考:式 适合于1582年(中国明朝万历十年)10月15日之后的情形 公式 w = y + y/4 + c/4 - 2* ...

  2. POJ 2389 Bull Math(水~Java -大数相乘)

    题目链接:http://poj.org/problem?id=2389 题目大意: 大数相乘. 解题思路: java BigInteger类解决 o.0 AC Code: import java.ma ...

  3. poj 2389.Bull Math 解题报告

    题目链接:http://poj.org/problem?id=2389 题目意思:就是大整数乘法. 题目中说每个整数不超过 40 位,是错的!!!要开大点,这里我开到100. 其实大整数乘法还是第一次 ...

  4. 数据结构——POJ 1686 Lazy Math Instructor 栈的应用

    Description A math instructor is too lazy to grade a question in the exam papers in which students a ...

  5. POJ 1686 Lazy Math Instructor (模似题+栈的运用) 各种坑

    Problem Description A math instructor is too lazy to grade a question in the exam papers in which st ...

  6. poj 1684 Lazy Math Instructor(字符串)

    题目链接:http://poj.org/problem?id=1686 思路分析:该问题为表达式求值问题,对于字母使用浮点数替换即可,因为输入中的数字只能是单个digit. 代码如下: #includ ...

  7. POJ 1686 Lazy Math Instructor(栈)

    原题目网址:http://poj.org/problem?id=1686 题目中文翻译: Description 数学教师懒得在考卷中给一个问题评分,因为这个问题中,学生会为所问的问题提出一个复杂的公 ...

  8. POJ 2183

    模拟题 #include <iostream> #include <cstdio> #include <algorithm> using namespace std ...

  9. POJ 题目分类(转载)

    Log 2016-3-21 网上找的POJ分类,来源已经不清楚了.百度能百度到一大把.贴一份在博客上,鞭策自己刷题,不能偷懒!! 初期: 一.基本算法: (1)枚举. (poj1753,poj2965 ...

随机推荐

  1. 做为一名dba你应该知道这些数据恢复

    1.将备份数据   拉取到本地虚拟机上 进行恢复(千万不要把数据直接恢复到生产中,除非迫不得已!!)   2.在本地虚拟机上恢复之后,导出需要恢复的数据.   3.在本地虚拟机上恢复做恢复测试.   ...

  2. u盘安装windows系统

    使用老毛桃为例: 电脑下载老毛桃到自己电脑,插入U盘,制作U盘为启动盘. 四种安装方法: 1.win7能够使用:(win中包含iso的解压文件)解压ISO ----〉 restart win7 --- ...

  3. SpringMVC如何接收application/json内容编码类型的参数?

    在上代码之前,有必要先说说@ResquestBody注解的含义: 1.官方解释如下: Annotation indicating a method parameter should be bound ...

  4. VC++6.0编译器标记的那些内存值

    栈内存初始值 0xcccccccc 和-858993460.   二者是一样的, 一个是16进制, 另一个是10进制

  5. 杭电ACM 1201

    #include <stdio.h> int func(int year){ if ( year % 400 == 0 || (year % 4 == 0 &&year % ...

  6. 论url

    http://www.cnblogs.com/yexiaochai/p/3650379.css文件中 background: (../images/logo.png); http://www.cnbl ...

  7. 基于php5.6 php.ini详解

    PHP中auto_prepend_file与auto_append_file的用法 第一种方法:在所有页面的顶部与底部都加入require语句.例如:?123require('header.php') ...

  8. Javacard 解释器怎样在API类库中找到源文件调用的类、方法或者静态域?

    申明:本篇非本人原创,是在阅读各种论文文献之后,对论文文献的一种梳理. 主要参考文献为: ------------------------------------------------------- ...

  9. Android如何连接MySQL数据库

    Android开发中,大多数连接到远程MySQL数据库的方法是加入特定的Service到代码中.由于MySQL通常是和PHP一起使用的,最简单以及最常见的方法是写PHP脚本管理数据连接,以及从Andr ...

  10. Opensuse enable sound and mic card

    Install application pavucontrol Run pavucontrol You will see the configuration about sound card and ...