HDU 2991 || SDUT 2394 Generate random numbers(简单数论)

Generate random numbers

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 114    Accepted Submission(s): 44

Problem Description

John von Neumann suggested in 1946 a method to create a sequence of pseudo-random numbers. His idea is known as the "middle-square"-method and works as follows: We choose an initial value a0, which has a decimal representation of length at most n. We then multiply the value a0 by itself, add leading zeros until we get a decimal representation of length 2 × n and take the middle n digits to form ai. This process is repeated for each ai with i>0. In this problem we use n = 4. 

Example 1: a0=5555, a02=30858025, a1=8580,... 

Example 2: a0=1111, a02=01234321, a1=2343,... 

Unfortunately, this random number generator is not very good. When started with an initial value it does not produce all other numbers with the same number of digits. 

Your task is to check for a given initial value a0 how many different numbers are produced. 

 

Input

The input contains several test cases. Each test case consists of one line containing a0 (0 < a0 < 10000). Numbers are possibly padded with leading zeros such that each number consists of exactly 4 digits. The input is terminated with a line containing the value 0. 

Output

For each test case, print a line containing the number of different values ai produced by this random number generator when started with the given value a0. Note that a0 should also be counted.

 

Sample Input

5555

0815

6239

0

 

Sample Output

32

17

111

 

Hint

 

Note that the third test case has the maximum number of different values among all possible inputs.

Source

2009/2010 Ulm Local Contest

Recommend

lcy

解题报告:这道题主要是题意不太好懂,今天内部测试的时候花了很长时间才看懂,哎!英语不行呀,这道题就是给我们四位数(形式上是四位,可以有前导零例如:0021),让它产生随机数,方法是让其平方后为形式上的八位数例如:0021平方后为00000441去中间的四位0004,依此类推,终究会有一个四位数和前面的一样,就是求这个数的位置;

代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int N = 1010;
int a[N], a2[N];//a[]存储的是“形式上”的四位数,a2[]存储的是“形式上”的八位数
int main()
{
int n, i, ans , j, flag;
while (scanf("%d", &n) != EOF && n)
{
memset(a, 0, sizeof(a));
memset(a2, 0, sizeof(a2));
a[0] = n;
for (i = 0; i < N; ++i)
{
a2[i] = a[i] * a[i];
a[i + 1] = a2[i] / 100 - (a2[i] / 1000000) * 10000; //取中间的四位数
}
flag = 0;
for (i = 0; i < N && !flag; ++i)
{
for (j = i + 1; j < N && !flag; ++j)
{
if (a[i] == a[j])//找到第一个四位数和之前的一样
{
flag = 1;
ans = j;//记录这个数的位置
}
}
}
printf("%d\n", ans);
}
return 0;
}



转载于:https://www.cnblogs.com/lidaojian/archive/2012/02/26/2368991.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值