数论及其应用——数论基础

  同其他专题一样,在数论专题当中我也开了一篇“基础”文章,其实严谨的来说也不能算基础,其目的在于记录一些有关数论方面的一些理论性或者说模型化不是很强的问题。例如找规律、简单的模拟题之类。

 

  那我们们来看这要一道问题。(Problem source : pku 2183)

 

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.
 
  题目大意:给出一个六位数,取出中间四位数,进行平方运算,然后取出结果的后六位,然后重复上述操作,直到出现了相同的六位数停止。需要你输出这之前进行了多少次操作,并且输出这个第二次出现的六位数,以及该数出现第一次而第二次中间发生的操作数,从问题描述的图示我们直观的理解其含义。
  编程实现:数理分析上几乎不存在难度,编程实现也只是需要简单的模型实现。
  参考代码如下。
 
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
int use[1000001];

int main()
{
     int x , y, i;
     scanf("%d",&x);
     memset(use , -1 , sizeof(use));
     use[x] = 0;
     for(i = 1;;i++)
     {
       x = x / 10 % 10000;
       x *= x;
       x %= 1000000;
       if(use[x] >= 0)  break;
       use[x] = i;
     }

     printf("%d %d %d\n",x,i-use[x],i);
}

 

转载于:https://www.cnblogs.com/rhythmic/p/5278703.html

数论基础及其应用 作者:沈忠华 编著 出版时间:2015年版 内容简介   《数论基础及其应用》为数学与密码学交叉学科的特色教材,内容包括整除理论、同余、连分数、同余方程、原根。《数论基础及其应用》以数论知识为主线,有机地融入数论应用(主要是在密码学中的应用)的内容,理论与应用的知识的广度和深度都适度。《数论基础及其应用》可作为数学与应用数学专业、信息与计算科学专业和信息安全专业的本科生基础教材,也可作为密码学与信息安全专业的研究生教材。 目录 前言 第1章整除理论 1.1带余数除法 1.2辗转相除法 1.3最大公约数的性质 1.4最小公倍数 1.5算术基本定理 第2章同余 2.1同余的基本性质 2.2计算星期几 2.3循环比赛 第3章简单密码 3.1仿射加密 3.2矩阵加密 第4章剩余系 4.1完全剩余系 4.2简化剩余系 4.3Euler定理,Fermat定理 4.4数论函数 第5章不定方程 5.1一次不定方程 5.2方程x2+y2=x2 第6章同余方程 6.1同余方程的基本概念 6.2孙子定理 6.3模ρα的同余方程 6.4素数模的同余方程 第7章公钥密码 7.1公钥密码系统 7.2RSA加密 第8章二次剩余 8.1素数模的二次同余方程 8.2Legendre符号,二次互反律 8.3Jacobi符号 第9章原根 9.1指数及其基本性质 9.2原根与指标 9.3伪素数 第10章实数的表示 10.1连分数的基本性质 10.2实数的连分数表示 10.3循环连分数 10.4实数的b进制表示 第11章平方和 11.1二平方之和 11.2四平方之和 附录
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值