1698. Square Country 5

Time limit: 2.0 second
Memory limit: 64 MB
The first arithmetical operation taught to the children of the Square country is the calculation of squares of positive integers. At the first lesson the children are provided with “easy” numbers, calculating a square of which can be done by writing a few digits in front of them (i.e. 76 is an easy number because 762 = 5776). Of course, the numbers cannot contain leading zeroes. The task shouldn't be too difficult, so the easy numbers shouldn't contain more than n digits. How many different easy numbers can teachers prepare for the first lesson?

Input

The only input line contains an integer n (1 ≤ n ≤ 2000), the maximal length of the easy number the children can be provided with.

Output

Output the number of different easy numbers consisting of at most n digits.

Sample

input output
1
3

题意:

方块国的孩子们学到的第一个算术运算是正整数的平方计算.孩子们首先学的是那些"简单"数字的平方计算.所谓简单数字就是那些只需要在原数后加几个数字就可得到原数平方的数字(例如,76是简单数字,因为762=5776).当然,所有`数字都不能包含前导0.平方运算不能太麻烦,所以简单数字都不能超过n位.老师们总共可以准备多少简单数字?

输入

只有一个数字n(1<=n<=2000),表示给孩子们的简单数字的最大长度.

输出

输出不超过n位的简单数字个数.

[编辑]样例

[编辑]样例输入

1

[编辑]样例输出

3

思路:

进行记忆化搜索,利用大数乘的方法,寻找自守数;

前道知识:

如果某个数的平方的末尾几位数等于这个数,那么就称这个数为自守数。
显然,5和6是一位自守数(5x5=25 6x6=36)
25x25=625 76x76=5776,所以25和76是两位自守数。
自守数有一个特性,以他为后几位的两个数相乘,乘积的后几位仍是这个自守数。因为5时自守数,所以以5为个位数的两个数相乘,乘积的个位仍然是5;76是自守数,所以以76为后两位数的两个数相乘,其结果的后两位仍是76,如176x576=101376。
虽然0和1的平方的个位数仍然是0和1,但是他们太“平凡”了,研究他们没有意义,所以不算自守数。
三位自守数是625和376,四位自守数是0625和9376,五位自守数是90625和09376......
我们可以看到,(n+1)位的自守数出自n位的自守数。由此得出,如果知道n位的自守数a,那么(n+1)位的自守数应当由a前面加上一个数构成。
实际上,简化一下,还能发现如下规律:
5+6=11
25+76=101
625+376=1001
......
所以,两个n位自守数,他们的和等于10^n+1
 
代码:
 #include <iostream>
#include <string>
#include<cstring>
#include <queue>
#include <vector>
#include <map>
#include <algorithm>
#include <cmath>
#include <cstdio> using namespace std;
int n, ans;
int a[];
int b[]; bool check(int k)
{//计算平方后的第k+1位的数值;
b[k] = ;
for (int i = ; i <= k; ++i)
b[k] += a[i] * a[k - i];
if (k)
b[k] += b[k - ] / ;
return b[k] % == a[k];
} void dfs(int k)
{
if (k >= n)
return;
for (int i=;i>=;--i)
{
a[k] = i;
if (check(k))
{
if (a[k])
{//第k+1位数不是0,表示产生了新的自守数;
ans++;
}
dfs(k + );
}
}
} int main()
{
scanf("%d", &n);
dfs();
printf("%d\n", ans);
}

ural 1698. Square Country 5(记忆化搜索)的更多相关文章

  1. URAL 1698. Square Country 5(记忆化搜索)

    题目链接 题意 : 自守数的定义:如果某个数的平方的末尾几位数等于这个数,那么就称这个数为自守数.例如5*5=25,则5就是自守数.让你求不超过n位的自守数有多少 思路 : 实际上,自守数还有两个性质 ...

  2. URAL 1501. Sense of Beauty(记忆化搜索)

    题目链接 本来暴力写个TLE了,加上记忆化就A了. #include <cstring> #include <cstdio> #include <string> # ...

  3. URAL 1152. False Mirrors (记忆化搜索 状压DP)

    题目链接 题意 : 每一颗子弹破坏了三个邻近的阳台.(第N个阳台是与第1个相邻)射击后后的生存的怪物都对主角造成伤害- 如此,直到所有的怪物被消灭,求怎样射击才能受到最少伤害. 思路 : 状压,数据不 ...

  4. 记忆化搜索(DP+DFS) URAL 1183 Brackets Sequence

    题目传送门 /* 记忆化搜索(DP+DFS):dp[i][j] 表示第i到第j个字符,最少要加多少个括号 dp[x][x] = 1 一定要加一个括号:dp[x][y] = 0, x > y; 当 ...

  5. 记忆化搜索(DFS+DP) URAL 1223 Chernobyl’ Eagle on a Roof

    题目传送门 /* 记忆化搜索(DFS+DP):dp[x][y] 表示x个蛋,在y楼扔后所需要的实验次数 ans = min (ans, max (dp[x][y-i], dp[x-1][i-1]) + ...

  6. 记忆化搜索(DFS+DP) URAL 1501 Sense of Beauty

    题目传送门 /* 题意:给了两堆牌,每次从首部取出一张牌,按颜色分配到两个新堆,分配过程两新堆的总数差不大于1 记忆化搜索(DFS+DP):我们思考如果我们将连续的两个操作看成一个集体操作,那么这个操 ...

  7. hdu 4856 Tunnels (记忆化搜索)

    Tunnels Time Limit: 3000/1500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Su ...

  8. CodeForces 173C Spiral Maximum 记忆化搜索 滚动数组优化

    Spiral Maximum 题目连接: http://codeforces.com/problemset/problem/173/C Description Let's consider a k × ...

  9. hdu 1078 FatMouse and Cheese (dfs+记忆化搜索)

    pid=1078">FatMouse and Cheese Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/ ...

随机推荐

  1. [HMLY]2.CocoaPods详解----进阶

    作者:wangzz 原文地址:http://blog.csdn.net/wzzvictory/article/details/19178709 转载请注明出处   一.podfile.lock文件   ...

  2. auDemo

    Option Explicit Private Const MODULE_NAME = "auDemo.WSC" Dim oEccomOperation,dbDemo Set oE ...

  3. JS---控制键盘事件

    键盘事件汇总: 1.onkeydown 键盘按下时触发; 2.onkeyup  键盘按下后抬起触发的事件  3.onkeypress 这个事件在用户按下并放开任何字母数字键时发生(不常用) keyCo ...

  4. 一步步优化JVM二:JVM部署模型和JVM Runtime

    选择JVM部署模型    JVM部署模型的选择总体来说就是决定应用是部署在单个JVM实例还是多个JVM实例上(这里简单举例说明一下JVM实例,比如:我们常用eclipse开发,启动一个eclipse就 ...

  5. Leetcode 解题报告

    347. Top K Frequent Elements Given a non-empty array of integers, return the k most frequent element ...

  6. ASP.NET中ListBox控件的使用

    文章来源:http://www.cnblogs.com/fengzheng126/archive/2012/04/10/2441551.html ListBox控件属性介绍: SelectIndex: ...

  7. jquery中的serialize

    jquery中的serialize对serializeArray进行了封装,serializeArray源码中定义将disabled的过滤掉,所以提交后值为null 解决方式是:在提交的时候,讲dis ...

  8. A标签-一个按钮样式

    该文件引用jquery-1.11.3.js库 <!doctype html> <html> <head> <meta charset="UTF-8& ...

  9. ntp升级

    1. 系统与软件版本 1.1 系统版本 CentOS6.5 x86_64 1.2 ntpd软件版本 ntp-4.2.8p9.tar.gz 1.3 下载地址 官方下载地址:http://support. ...

  10. C++设计模式-singleton单例模式_new

      class nocopyable { protected: nocopyable(){}; virtual ~nocopyable(){}; nocopyable(const nocopyable ...